web/ldt/ldt_utils/utils.py
author wakimd
Tue, 05 Oct 2010 18:19:44 +0200
changeset 91 9c83809fda01
parent 80 1e7732f40eee
child 85 3b70d84e661a
permissions -rw-r--r--
migration static elements
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import lucene
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from ldt.ldt_utils import STORE
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from ldt.ldt_utils import ANALYZER
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import uuid
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import django.core.urlresolvers
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from django.conf import settings
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import urllib
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import datetime
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     9
import lxml.etree
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
40
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    11
__BOOLEAN_DICT = {
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    12
    'false':False,
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    13
    'true':True,
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    14
    '0':False,
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    15
    '1':True,
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    16
    't': True,
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    17
    'f':False
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    18
}
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    19
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    20
def boolean_convert(bool):
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    21
    if bool is None:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    22
        return False
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    23
    if bool is True or bool is False:
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    24
        return bool
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    25
    key = str(bool).lower()
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    26
    return __BOOLEAN_DICT.get(key, False)
509e30b9f5c9 basic contents and projects display
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
    27
41
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
    28
def generate_uuid():
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
    29
    return unicode(uuid.uuid1())
a5719dcb742a add content creation
ymh <ymh.work@gmail.com>
parents: 40
diff changeset
    30
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
class LdtSearch(object):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    def query(self, field, query):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        indexSearcher = lucene.IndexSearcher(STORE)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        queryParser = lucene.QueryParser(lucene.Version.LUCENE_30, field, lucene.FrenchAnalyzer(lucene.Version.LUCENE_30))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        queryParser.setDefaultOperator(lucene.QueryParser.Operator.AND)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        queryObj = queryParser.parse(query)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        hits = indexSearcher.search(queryObj, settings.LDT_MAX_SEARCH_NUMBER)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
	
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        res = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        for hit in hits.scoreDocs:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
            doc = indexSearcher.doc(hit.doc)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            res.append({"iri_id":doc.get("iri_id"),"ensemble_id":doc.get("ensemble_id"),"decoupage_id":doc.get("decoupage_id"), "element_id":doc.get("element_id")})
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        indexSearcher.close()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        return res
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    def queryAll(self, query):		
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        return self.query("all", query)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
class LdtUtils(object):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    def generateLdt(self, contentList, file, title = u"", author=u"IRI Web", web_url=u"", media_url="", startSegment = None, contributions=None):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    54
	iri = lxml.etree.Element(u'iri')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    55
	doc = lxml.etree.ElementTree(iri)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    56
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    57
	project = lxml.etree.SubElement(iri, u'project')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    58
	project.set(u"id",unicode(str(uuid.uuid1())))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    59
        project.set(u"title",unicode(title))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    60
	project.set(u"user",author)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    61
	project.set(u"abstract",u"")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    62
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    63
	medias = lxml.etree.SubElement(iri, u"medias")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    64
	for content in contentList:
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    65
	    videopath = unicode(settings.STREAM_URL)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    66
	    if content.videopath :
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    67
		videopath = unicode(content.videopath)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    68
	    media = lxml.etree.SubElement(medias, "media")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    69
	    media.set(u"id",content.iri_id)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    70
	    media.set(u"src",content.iri_url(web_url))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    71
	    media.set(u"video",videopath)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    72
            media.set(u"pict",u"")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    73
            media.set(u"extra",u"")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        if contributions is None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
            contributions = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        annotations_nodes = {}
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        for contrib in contributions:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    79
	    ldtdoc = lxml.etree.fromstring(contrib.ldtproject.ldt.encode("utf-8"))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    80
	    res = ldtdoc.xpath("/iri/annotations/content")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    81
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            for content in res:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    83
		contentid = content.get("id")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
                if annotations_nodes.has_key(contentid):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
                    contentnode = annotations_nodes[contentid]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
                else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
                    contentnode = {"id":contentid, "ensembles":[]}
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
                    annotations_nodes[contentid]=contentnode
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
                for ens in content.childNodes:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    90
		    if ens.tag.endswith("ensemble"):
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    91
			contentnode["ensembles"].append(ens.tag)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    92
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        if len(annotations_nodes) > 0:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    95
	    annotations = lxml.etree.SubElement(iri, "annotations")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
            for content in contentList:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
                if content.content_base.iri_id in annotations_nodes:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
                    contentnode = annotations_nodes[content.content_base.iri_id]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
                    if contentnode is not None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
                        if len(contentnode["ensembles"])>0:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   101
			    content = lxml.etree.SubElement(annotation, "content")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   102
			    content.set("id",contentnode["id"])
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   103
                            content.text = u""
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
                        else:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   105
			    content = lxml.etree.SubElement(annotation, "content")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   106
			    content.set("id",contentnode["id"])
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   107
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        else:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   109
	    annotations = lxml.etree.SubElement(iri, "annotations")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   112
	displays = lxml.etree.SubElement(iri, "displays")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        if len(contentList) > 0:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   114
	    display = lxml.etree.SubElement(displays, "display")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   115
	    display.set(u"id",u"0")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   116
	    display.set(u"title",u"generated")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   117
	    display.set(u"idsel",contentList[0].iri_id)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   118
	    display.set(u"tc",u"0")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
            for content in contentList:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   120
		contentd = lxml.etree.SubElement(display,"content")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   121
		contentd.set(u"id",content.iri_id)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
                filepath = urllib.urlopen(content.iri_url())
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   123
		
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   124
		udoc = lxml.etree.parse(filepath)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   125
		res = udoc.xpath("/iri/body/ensembles/ensemble/decoupage")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
                for decoupagenode in res:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
                    decoupage_id = decoupagenode.getAttribute(u"id")
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                    ensemble_id = decoupagenode.parentNode.getAttribute(u"id")
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   129
		    decoupage_id = decoupagenode.get(u"id")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   130
		    ensemble_id = decoupagenode.getparent().get(u"id")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   131
		    decoupage = lxml.etree.SubElement(content,"decoupage")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   132
		    decoupage.set(u"id",decoupage_id)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   133
		    decoupage.set(u"idens",ensemble_id)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
            if startSegment is not None:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   135
		activeSegment = lxml.etree.SubElement(display,"activeSegment")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   136
		idas = lxml.etree.SubElement(activeSegment,"id")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   137
		idas.set(u"idctt",startSegment["idcontent"])
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   138
                idas.set(u"idens" ,startSegment["idgroup"])
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   139
                idas.set(u"idcut",startSegment["idcutting"])
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   140
                idas.set(u"idseg",startSegment["idsegment"])
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   142
	edits = lxml.etree.SubElement(iri, "edits")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   143
	
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   144
	doc.write(file, pretty_print=True)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   145
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
    def generateInit(self, url, method, search=None):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
        
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   149
	iri = lxml.etree.Element('iri')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   150
	impl = lxml.etree.ElementTree(iri)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   151
 
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   152
	elementFiles = lxml.etree.SubElement(iri,'files')    
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   153
	elementInit = lxml.etree.SubElement(elementFiles, 'init')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   154
	elementfile = lxml.etree.SubElement(elementInit, 'file')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
            
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   156
	elementfile.set('src',settings.WEB_URL + django.core.urlresolvers.reverse(method, args=url))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   157
	elementfile.set('display', '1')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   158
	if(search):
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   159
	    elementfile.set("segsel",settings.WEB_URL + django.core.urlresolvers.reverse(search, args=url))   
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   160
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
        # /*chemin video : tant que le serveur de media n'est pas up, */
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   163
	elementfile.set('video', settings.STREAM_URL)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   164
	elementfile.set('pict', "")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   165
	elementfile.set('extra', "")    
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   166
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   167
    	elementRecent = lxml.etree.SubElement(elementFiles, 'recent')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   168
	elementLibrary = lxml.etree.SubElement(elementFiles, 'library')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
        username = ''
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
        id = ''
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   172
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   173
	elementUser = lxml.etree.SubElement(iri, 'user')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   174
	elementUser.set('name', username)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   175
	elementUser.set('id', id)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
        
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   177
        return iri 
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   178
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   179
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
def create_ldt(project, user):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   183
    contentList = project.contents.all()
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    """create xml"""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
    # create a dom
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   188
    iri = lxml.etree.Element('iri')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   189
    doc = lxml.etree.ElementTree(iri)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   190
    
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
    #node project
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   192
    elementProject = lxml.etree.SubElement(iri, 'project')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   194
    elementProject.set('abstract', "")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   195
    elementProject.set('title', project.title)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   196
    elementProject.set('user', user.username)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   197
    elementProject.set('id', project.ldt_id)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   198
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
    #node medias
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   200
    elementMedias = lxml.etree.SubElement(iri, 'medias')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
    idsel = None      
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
    for content in contentList:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
        if not idsel:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
            idsel = content.iri_id
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   206
	elementMedia = lxml.etree.SubElement(elementMedias, 'media')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   207
	elementMedia.set('id', content.iri_id)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   208
	elementMedia.set('src', content.iri_url())
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   209
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
        if content.videopath and content.videopath !="":
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   211
	    elementMedia.set('video', content.videopath)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
        else:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   213
	    elementMedia.set('video', settings.STREAM_URL)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   214
	elementMedia.set('pict', "")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   215
	elementMedia.set('extra', "")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   216
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
    if not idsel:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
        idsel = ""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    #node annotations
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   221
    elementAnnotations = lxml.etree.SubElement(iri, 'annotations')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   222
    
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
    #node displays
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   224
    elementDisplays = lxml.etree.SubElement(iri, 'displays')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   225
    elementDisplay = lxml.etree.SubElement(elementDisplays, 'display')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   226
    elementDisplay.set('id', '0')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   227
    elementDisplay.set('title', 'Init view')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   228
    elementDisplay.set('idsel', idsel)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   229
    elementDisplay.set('tc', '0')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   230
    elementDisplay.set('zoom', '0')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   231
    elementDisplay.set('scroll', '0')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   232
    elementDisplay.set('infoBAB', '')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   233
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   234
    
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
    #node content
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
    for content in contentList:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   237
	elementContent = lxml.etree.SubElement(elementDisplay, 'content')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   238
	elementContent.set('id', content.iri_id)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   239
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
        if not 'http' in content.iriurl:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
        #eg: "iiiielizabethrosse/ENMI08-III_elizabethrosse.iri"
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
            url = content.iri_url()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
        else:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
            url =content.iriurl
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
        file = urllib.urlopen(url)
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   246
	doc = lxml.etree.parse(file)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   247
	res = doc.xpath("/iri/body/ensembles/ensemble/decoupage")        
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   248
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   249
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   250
	#node decoupage
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
        for decoupagenode in res:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   252
	    decoupage_id = decoupagenode.attrib(u"id")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   253
	    parent= decoupagenode.getparent()
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   254
	    ensemble_id = parent.attrib(u"id")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   255
	    elementDecoupage = etree.SubElement(elementContent, 'decoupage')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   256
	    elementDecoupage.set('idens', ensemble_id)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   257
	    elementDecoupage.set('id', decoupage_id)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   259
    #node edits
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   260
    elementEdits = lxml.etree.SubElement(iri, 'edits')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   262
    #write dom in Project.ldt 
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   263
    project.ldt = lxml.etree.tostring(iri, pretty_print=True)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   264
    
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
    #save Project
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
    project.save()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
    return project        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
def copy_ldt(project, new_project, user):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
    new_project.ldt_id = str(uuid.uuid1())
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
    new_project.created_by=user.username
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
    new_project.changed_by=user.username
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
    new_project.state = 1
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
    contentList=project.contents.all()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
    """create xml"""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   280
    ldt = lxml.etree.fromstring(project.ldt.encode("utf-8"))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   281
    res = ldt.xpath("/iri/project")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
    for elementProject in res:
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   283
	elementProject.set('abstract', "")
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   284
	elementProject.set('title', new_project.title)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   285
	elementProject.set('user', user.username)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   286
	elementProject.set('id', new_project.ldt_id)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
        
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   288
    new_project.ldt = lxml.etree.tostring(ldt, pretty_print=True)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   289
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
    #save Project
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
    new_project.save()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
    return new_project
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
def create_empty_iri(file, content, username):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   296
    iri = lxml.etree.Element('iri')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   297
    doc = lxml.etree.ElementTree(iri)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   299
    head = lxml.etree.SubElement(iri, 'head')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   300
    meta1 = lxml.etree.SubElement(head, 'meta')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   301
    meta1.set(u'name', u'id')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   302
    meta1.set(u'content', unicode(content.iri_id))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   303
    meta2 = lxml.etree.SubElement(head, 'meta')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   304
    meta2.set(u'name',u'title')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   305
    meta2.set(u'content', unicode(content.title))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   306
    meta3 = lxml.etree.SubElement(head, 'meta')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   307
    meta3.set(u'name',u'abstract')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   308
    meta3.set(u'content', unicode(content.description))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   309
    meta4 = lxml.etree.SubElement(head, 'meta')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   310
    meta4.set(u'name',u'author')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   311
    meta4.set(u'content', unicode(username))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   312
    meta5 = lxml.etree.SubElement(head, 'meta')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   313
    meta5.set(u'name',u'contributor')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   314
    meta5.set(u'content', unicode(username))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   315
    meta6 = lxml.etree.SubElement(head, 'meta')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   316
    meta6.set(u'name',u'date')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   317
    meta6.set(u'content', unicode(datetime.date.today().isoformat()))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   318
    meta7 = lxml.etree.SubElement(head, 'meta')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   319
    meta7.set(u'name',u'copyright')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   320
    meta7.set(u'content', u'IRI')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   321
    meta8 = lxml.etree.SubElement(head, 'meta')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   322
    meta8.set(u'name', u'type')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   323
    meta8.set(u'content', u'video')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   325
    body = lxml.etree.SubElement(iri, 'body')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   326
    ensembles = lxml.etree.SubElement(body, 'ensembles')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   327
    links = lxml.etree.SubElement(body, 'links')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   328
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   329
    medias = lxml.etree.SubElement(body, 'medias')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   331
    media1 = lxml.etree.SubElement(medias, 'media')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   332
    media1.set(u'id',u'video')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   333
    video = lxml.etree.SubElement(media1, 'video')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   334
    video.set(u'src',unicode(content.stream_src))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   335
    video.set(u'id',unicode(content.iri_id))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   336
    video.set(u'dur',unicode(content.duration))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   337
    video.set(u'begin',u'0')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   339
    media2 = lxml.etree.SubElement(medias, 'media')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   340
    media2.set(u'id',u'tool')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   341
    tool = lxml.etree.SubElement(media2, 'tool')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   342
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   343
    display = lxml.etree.SubElement(body, 'display')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   344
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   345
    doc.write(file, pretty_print=True)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   346
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   347
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   348
def update_iri(filepath, content, username):
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
    
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   350
    # open xml
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   351
    doc = lxml.etree.parse(filepath)
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   352
    
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   353
    res = doc.xpath("/iri/head/meta")
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   354
    # update meta
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
    
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   356
    for meta_node in res:
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   357
        meta_name = meta_node.get("name")
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   358
        content_attr = None
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   359
        if meta_name == u'id':
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
            content_attr = unicode(content.iri_id)
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
        elif meta_name == u'title':
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
            content_attr = unicode(content.title)
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
        elif meta_name == u'abstract':
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
            content_attr = unicode(content.description)
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
        elif meta_name == u'contributor':
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
            content_attr = unicode(username)
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
        elif meta_name == u"date":
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
            content_attr = unicode(datetime.date.today().isoformat())
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
        if content_attr is not None:
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   370
            meta_node.set(u"content", content_attr)
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   372
    res = doc.xpath("/iri/body/medias/media[@id='video']/video")
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
    
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
    if len(res) > 0:
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
        video_node = res[0]
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
   376
        video_node.set(u'src', unicode(content.stream_src))
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   377
        video_node.set(u'dur', unicode(content.duration))
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   378
        video_node.set(u'id', unicode(content.iri_id))
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
    # update video
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
    
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
    f = open(filepath, "w")
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   382
    try:
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   383
        doc.write(f, encoding="UTF-8", pretty_print=True, xml_declaration=True)
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   384
    finally:
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   385
        f.close()