web/ldt/ldt_utils/utils.py
author ymh <ymh.work@gmail.com>
Wed, 27 Oct 2010 19:51:59 +0200
changeset 116 811b34fd1bc0
parent 85 3b70d84e661a
child 137 d363fa6232b2
permissions -rw-r--r--
correct empty content correction
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)
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    39
    
0
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
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    47
    def queryAll(self, query):        
0
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
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    54
        iri = lxml.etree.Element(u'iri')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    55
        doc = lxml.etree.ElementTree(iri)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    56
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    57
        project = lxml.etree.SubElement(iri, u'project')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    58
        project.set(u"id",unicode(str(uuid.uuid1())))
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
    59
        project.set(u"title",unicode(title))
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    60
        project.set(u"user",author)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    61
        project.set(u"abstract",u"")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    62
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    63
        medias = lxml.etree.SubElement(iri, u"medias")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    64
        for content in contentList:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    65
            videopath = unicode(settings.STREAM_URL)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    66
            if content.videopath :
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    67
                videopath = unicode(content.videopath)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    68
            media = lxml.etree.SubElement(medias, "media")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    69
            media.set(u"id",content.iri_id)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    70
            media.set(u"src",content.iri_url(web_url))
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    71
            media.set(u"video",videopath)
80
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"")
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    74
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    75
            if contributions is None:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    76
                contributions = []
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    77
            annotations_nodes = {}
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    78
            for contrib in contributions:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    79
                ldtdoc = lxml.etree.fromstring(contrib.ldtproject.ldt.encode("utf-8"))
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    80
                res = ldtdoc.xpath("/iri/annotations/content")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    81
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    82
                for content in res:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    83
                    contentid = content.get("id")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    84
                    if annotations_nodes.has_key(contentid):
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    85
                        contentnode = annotations_nodes[contentid]
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    86
                    else:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    87
                        contentnode = {"id":contentid, "ensembles":[]}
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    88
                        annotations_nodes[contentid]=contentnode
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    89
                    for ens in content.childNodes:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    90
                        if ens.tag.endswith("ensemble"):
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    91
                            contentnode["ensembles"].append(ens.tag)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    92
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    93
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    94
            if len(annotations_nodes) > 0:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    95
                annotations = lxml.etree.SubElement(iri, "annotations")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    96
                for content in contentList:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    97
                    if content.content_base.iri_id in annotations_nodes:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    98
                        contentnode = annotations_nodes[content.content_base.iri_id]
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
    99
                        if contentnode is not None:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   100
                            if len(contentnode["ensembles"])>0:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   101
                                content = lxml.etree.SubElement(annotation, "content")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   102
                                content.set("id",contentnode["id"])
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   103
                                content.text = u""
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   104
                            else:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   105
                                content = lxml.etree.SubElement(annotation, "content")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   106
                                content.set("id",contentnode["id"])
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   107
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   108
            else:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   109
                annotations = lxml.etree.SubElement(iri, "annotations")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   110
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   111
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   112
        displays = lxml.etree.SubElement(iri, "displays")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   113
        if len(contentList) > 0:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   114
            display = lxml.etree.SubElement(displays, "display")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   115
            display.set(u"id",u"0")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   116
            display.set(u"title",u"generated")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   117
            display.set(u"idsel",contentList[0].iri_id)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
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:
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   120
                contentd = lxml.etree.SubElement(display,"content")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
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())
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   123
            
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   124
                udoc = lxml.etree.parse(filepath)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
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:
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   127
                    decoupage_id = decoupagenode.get(u"id")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   128
                    ensemble_id = decoupagenode.getparent().get(u"id")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   129
                    decoupage_id = decoupagenode.get(u"id")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   130
                    ensemble_id = decoupagenode.getparent().get(u"id")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   131
                    decoupage = lxml.etree.SubElement(contentd,"decoupage")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   132
                    decoupage.set(u"id",decoupage_id)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
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:
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   135
                activeSegment = lxml.etree.SubElement(display,"activeSegment")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   136
                idas = lxml.etree.SubElement(activeSegment,"id")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   137
                idas.set(u"idctt",startSegment["idcontent"])
80
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"])
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   141
    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   142
        edits = lxml.etree.SubElement(iri, "edits")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   143
        
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   144
        doc.write(file, pretty_print=True)
80
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):
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   148
                
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   149
        iri = lxml.etree.Element('iri')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   150
        impl = lxml.etree.ElementTree(iri)
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   151
 
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   152
        elementFiles = lxml.etree.SubElement(iri,'files')    
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   153
        elementInit = lxml.etree.SubElement(elementFiles, 'init')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   154
        elementfile = lxml.etree.SubElement(elementInit, 'file')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
            
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   156
        elementfile.set('src',settings.WEB_URL + django.core.urlresolvers.reverse(method, args=url))
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   157
        elementfile.set('display', '1')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   158
        if(search):
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   159
            elementfile.set("segsel",settings.WEB_URL + django.core.urlresolvers.reverse(search, args=url))   
80
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, */
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   163
        elementfile.set('video', settings.STREAM_URL)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   164
        elementfile.set('pict', "")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   165
        elementfile.set('extra', "")    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   166
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   167
        elementRecent = lxml.etree.SubElement(elementFiles, 'recent')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
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
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   173
        elementUser = lxml.etree.SubElement(iri, 'user')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   174
        elementUser.set('name', username)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
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):
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   182
    """create xml"""
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   183
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   185
    contentList = project.contents.all()
0
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
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   206
        elementMedia = lxml.etree.SubElement(elementMedias, 'media')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   207
        elementMedia.set('id', content.iri_id)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   208
        elementMedia.set('src', content.iri_url())
80
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 !="":
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   211
            elementMedia.set('video', content.videopath)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
        else:
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   213
            elementMedia.set('video', settings.STREAM_URL)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   214
        elementMedia.set('pict', "")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   215
        elementMedia.set('extra', "")
80
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:
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   237
        elementContent = lxml.etree.SubElement(elementDisplay, 'content')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   238
        elementContent.set('id', content.iri_id)
80
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)
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   246
        doc = lxml.etree.parse(file)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   247
        res = doc.xpath("/iri/body/ensembles/ensemble/decoupage")        
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   248
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   249
        #node decoupage
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
        for decoupagenode in res:
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   251
            decoupage_id = decoupagenode.get(u"id")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   252
            parent= decoupagenode.getparent()
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   253
            ensemble_id = parent.get(u"id")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   254
            elementDecoupage = lxml.etree.SubElement(elementContent, 'decoupage')
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   255
            elementDecoupage.set('idens', ensemble_id)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   256
            elementDecoupage.set('id', decoupage_id)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   258
    #node edits
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   259
    elementEdits = lxml.etree.SubElement(iri, 'edits')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   261
    #write dom in Project.ldt 
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   262
    project.ldt = lxml.etree.tostring(iri, pretty_print=True)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   263
    
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
    #save Project
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
    project.save()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
    return project        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
def copy_ldt(project, new_project, user):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
    new_project.ldt_id = str(uuid.uuid1())
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
    new_project.created_by=user.username
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
    new_project.changed_by=user.username
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
    new_project.state = 1
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
    contentList=project.contents.all()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
    """create xml"""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   279
    ldt = lxml.etree.fromstring(project.ldt.encode("utf-8"))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   280
    res = ldt.xpath("/iri/project")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
    for elementProject in res:
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   282
        elementProject.set('abstract', "")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   283
        elementProject.set('title', new_project.title)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   284
        elementProject.set('user', user.username)
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   285
        elementProject.set('id', new_project.ldt_id)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
        
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   287
    new_project.ldt = lxml.etree.tostring(ldt, pretty_print=True)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   288
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
    #save Project
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
    new_project.save()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
    return new_project
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
def create_empty_iri(file, content, username):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
    
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   295
    iri = lxml.etree.Element('iri')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   296
    doc = lxml.etree.ElementTree(iri)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   298
    head = lxml.etree.SubElement(iri, 'head')
116
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   299
    meta_id = lxml.etree.SubElement(head, 'meta')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   300
    meta_id.set(u'name', u'id')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   301
    meta_id.set(u'content', unicode(content.iri_id))
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   302
    meta_title = lxml.etree.SubElement(head, 'meta')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   303
    meta_title.set(u'name',u'title')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   304
    meta_title.set(u'content', unicode(content.title))
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   305
    meta_abstract = lxml.etree.SubElement(head, 'meta')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   306
    meta_abstract.set(u'name',u'abstract')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   307
    meta_abstract.set(u'content', unicode(content.description))
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   308
    meta_author = lxml.etree.SubElement(head, 'meta')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   309
    meta_author.set(u'name',u'author')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   310
    meta_author.set(u'content', unicode(username))
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   311
    meta_contributor = lxml.etree.SubElement(head, 'meta')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   312
    meta_contributor.set(u'name',u'contributor')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   313
    meta_contributor.set(u'content', unicode(username))
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   314
    meta_date = lxml.etree.SubElement(head, 'meta')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   315
    meta_date.set(u'name',u'date')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   316
    meta_date.set(u'content', unicode(datetime.date.today().isoformat()))
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   317
    meta_copyright = lxml.etree.SubElement(head, 'meta')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   318
    meta_copyright.set(u'name',u'copyright')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   319
    meta_copyright.set(u'content', u'IRI')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   320
    meta_type = lxml.etree.SubElement(head, 'meta')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   321
    meta_type.set(u'name', u'type')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   322
    meta_type.set(u'content', u'video')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   324
    body = lxml.etree.SubElement(iri, 'body')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   325
    ensembles = lxml.etree.SubElement(body, 'ensembles')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   326
    links = lxml.etree.SubElement(body, 'links')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   327
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   328
    medias = lxml.etree.SubElement(body, 'medias')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
116
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   330
    media_video = lxml.etree.SubElement(medias, 'media')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   331
    media_video.set(u'id',u'video')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   332
    video = lxml.etree.SubElement(media_video, 'video')
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   333
    video.set(u'src',unicode(content.stream_src))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   334
    video.set(u'id',unicode(content.iri_id))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   335
    video.set(u'dur',unicode(content.duration))
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   336
    video.set(u'begin',u'0')
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
116
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   338
    media_tool = lxml.etree.SubElement(medias, 'media')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   339
    media_tool.set(u'id',u'tool')
811b34fd1bc0 correct empty content correction
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   340
    tool = lxml.etree.SubElement(media_tool, 'tool')
80
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   341
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   342
    display = lxml.etree.SubElement(body, 'display')
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   343
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   344
    doc.write(file, pretty_print=True)
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   345
1e7732f40eee Migrated to lxml and added tests
wakimd
parents: 71
diff changeset
   346
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   347
def update_iri(filepath, content, username):
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   348
    
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
    # open xml
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   350
    doc = lxml.etree.parse(filepath)
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
    
62
39b2dab4f939 separate contet and media
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
   352
    res = doc.xpath("/iri/head/meta")
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   353
    # update meta
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   354
    
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
    for meta_node in res:
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   356
        meta_name = meta_node.get("name")
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
        content_attr = None
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   358
        if meta_name == u'id':
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   359
            content_attr = unicode(content.iri_id)
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
        elif meta_name == u'title':
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
            content_attr = unicode(content.title)
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
        elif meta_name == u'abstract':
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
            content_attr = unicode(content.description)
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
        elif meta_name == u'contributor':
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
            content_attr = unicode(username)
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
        elif meta_name == u"date":
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
            content_attr = unicode(datetime.date.today().isoformat())
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
        if content_attr is not None:
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   369
            meta_node.set(u"content", content_attr)
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   371
    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
   372
    
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
    if len(res) > 0:
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
        video_node = res[0]
71
fc1210bbb854 correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
   375
        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
   376
        video_node.set(u'dur', unicode(content.duration))
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   377
        video_node.set(u'id', unicode(content.iri_id))
5
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
    # update video
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
    
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
    f = open(filepath, "w")
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
    try:
32
eac14c3ae625 introduce lxml en update some code
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   382
        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
   383
    finally:
d42bb045f7d1 apply changes from eulalie
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   384
        f.close()