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