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