src/ldt/ldt/ldt_utils/utils.py
author verrierj
Tue, 31 Jan 2012 14:38:21 +0100
changeset 488 403f92f24144
parent 482 c802e00c7131
child 504 32a878a71a80
permissions -rw-r--r--
Bugfixes in search page + update settings metadataplayer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
114
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
     1
from copy import deepcopy
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
     2
from django.conf import settings
182
b7add86c2772 Resized modal windows #3 working on Firefox, Chrome, IE8 + update jQuery
verrierj
parents: 179
diff changeset
     3
from ldt.indexation import get_searcher, get_results_list
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
     4
from django.utils.translation import ugettext as _
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
     5
from StringIO import StringIO
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
     6
import datetime
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import django.core.urlresolvers
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
     8
import lxml.etree
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import urllib
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    10
import uuid
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
__BOOLEAN_DICT = {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    'false':False,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    'true':True,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    '0':False,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    '1':True,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    't': True,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    'f':False
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
}
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    21
def reduce_text_node(element_node, xpath_str=None):
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    22
    node_list = []
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    23
    if xpath_str is not None:
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    24
        node_list = element_node.xpath(xpath_str, smart_strings=False)
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    25
    else:
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    26
        node_list = [element_node.text]
63
93325a5d61f0 organize format and import
ymh <ymh.work@gmail.com>
parents: 54
diff changeset
    27
    return reduce(lambda t, s: t + s, node_list , "")
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
def boolean_convert(bool):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    if bool is None:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        return False
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    if bool is True or bool is False:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        return bool
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    key = str(bool).lower()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    return __BOOLEAN_DICT.get(key, False)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
def generate_uuid():
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    return unicode(uuid.uuid1())
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
class LdtSearch(object):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    def query(self, field, query):
95
8e57d39f660d correct indexation problem
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    43
        indexSearcher = get_searcher()
176
a88714473302 Added a page to display search results
verrierj
parents: 167
diff changeset
    44
        hits = get_results_list(field, query)
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        res = []
176
a88714473302 Added a page to display search results
verrierj
parents: 167
diff changeset
    47
        for hit in hits:
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            doc = indexSearcher.doc(hit.doc)
97
10f69a5bd9e1 correct propagation of project id on indexation
ymh <ymh.work@gmail.com>
parents: 95
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"), "project_id":doc.get("project_id")})
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        indexSearcher.close()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        return res
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
167
fe00e7302efe Change class and functions names to follow PEP8 formatting standards
verrierj
parents: 134
diff changeset
    53
    def query_all(self, query):        
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        return self.query("all", query)
176
a88714473302 Added a page to display search results
verrierj
parents: 167
diff changeset
    55
        
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
class LdtUtils(object):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    
167
fe00e7302efe Change class and functions names to follow PEP8 formatting standards
verrierj
parents: 134
diff changeset
    59
    def generate_ldt(self, contentList, title=u"", author=u"IRI Web", web_url=u"", startSegment=None, projects=None):
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        iri = lxml.etree.Element(u'iri')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        doc = lxml.etree.ElementTree(iri)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        project = lxml.etree.SubElement(iri, u'project')
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
    65
        project.set(u"id", generate_uuid())
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
        project.set(u"title", unicode(title))
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
        project.set(u"user", author)
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
        project.set(u"abstract", u"")
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        medias = lxml.etree.SubElement(iri, u"medias")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        for content in contentList:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
            videopath = unicode(settings.STREAM_URL)
115
d2cbc4c647dc correct utils.py for search to work.
cavaliet
parents: 114
diff changeset
    73
            if content.videopath != None :
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
                videopath = unicode(content.videopath)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            media = lxml.etree.SubElement(medias, "media")
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
            media.set(u"id", content.iri_id)
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
            media.set(u"src", content.iri_url(web_url))
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
            media.set(u"video", videopath)
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
            media.set(u"pict", u"")
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
            media.set(u"extra", u"")
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    
114
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    82
        if projects is None:
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    83
            projects = []
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    84
        annotations_nodes = {}
115
d2cbc4c647dc correct utils.py for search to work.
cavaliet
parents: 114
diff changeset
    85
        for project in projects:
114
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    86
            ldtdoc = lxml.etree.fromstring(project.ldt.encode("utf-8"))
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    87
            res = ldtdoc.xpath("/iri/annotations/content")
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    88
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    89
            for content in res:
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    90
                contentid = content.get("id")
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    91
                if annotations_nodes.has_key(contentid):
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    92
                    contentnode = annotations_nodes[contentid]
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    93
                else:
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    94
                    contentnode = {"id":contentid, "ensembles":[]}
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    95
                    annotations_nodes[contentid] = contentnode
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    96
                for ens in content:
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    97
                    if ens.tag.endswith("ensemble"):
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    98
                        contentnode["ensembles"].append(deepcopy(ens))
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
    99
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
   100
        annotations = lxml.etree.SubElement(iri, "annotations")
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
   101
        if len(annotations_nodes) > 0:
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
   102
            for content in contentList:
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
   103
                if content.iri_id in annotations_nodes:
115
d2cbc4c647dc correct utils.py for search to work.
cavaliet
parents: 114
diff changeset
   104
                    contentnode = annotations_nodes[content.iri_id]
114
279f1782c184 correct generate ldt
ymh <ymh.work@gmail.com>
parents: 113
diff changeset
   105
                    if contentnode is not None:
115
d2cbc4c647dc correct utils.py for search to work.
cavaliet
parents: 114
diff changeset
   106
                        content_node = lxml.etree.SubElement(annotations, "content")
d2cbc4c647dc correct utils.py for search to work.
cavaliet
parents: 114
diff changeset
   107
                        content_node.set("id", contentnode["id"])
d2cbc4c647dc correct utils.py for search to work.
cavaliet
parents: 114
diff changeset
   108
                        content_node.text = u""
d2cbc4c647dc correct utils.py for search to work.
cavaliet
parents: 114
diff changeset
   109
                        for ens in contentnode["ensembles"]:
d2cbc4c647dc correct utils.py for search to work.
cavaliet
parents: 114
diff changeset
   110
                            content_node.append(ens)
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        displays = lxml.etree.SubElement(iri, "displays")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        if len(contentList) > 0:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            display = lxml.etree.SubElement(displays, "display")
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
            display.set(u"id", u"0")
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
            display.set(u"title", u"generated")
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
            display.set(u"idsel", contentList[0].iri_id)
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
            display.set(u"tc", u"0")
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
            for content in contentList:
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
                contentd = lxml.etree.SubElement(display, "content")
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   122
                contentd.set(u"id", content.iri_id)
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
                filepath = urllib.urlopen(content.iri_url())
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
            
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
                udoc = lxml.etree.parse(filepath)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
                res = udoc.xpath("/iri/body/ensembles/ensemble/decoupage")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
                for decoupagenode in res:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                    decoupage_id = decoupagenode.get(u"id")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
                    ensemble_id = decoupagenode.getparent().get(u"id")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
                    decoupage_id = decoupagenode.get(u"id")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
                    ensemble_id = decoupagenode.getparent().get(u"id")
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   132
                    decoupage = lxml.etree.SubElement(contentd, "decoupage")
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   133
                    decoupage.set(u"id", decoupage_id)
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   134
                    decoupage.set(u"idens", ensemble_id)
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
            if startSegment is not None:
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   136
                activeSegment = lxml.etree.SubElement(display, "activeSegment")
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   137
                idas = lxml.etree.SubElement(activeSegment, "id")
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   138
                idas.set(u"idctt", startSegment["idcontent"])
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   139
                idas.set(u"idens" , startSegment["idgroup"])
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
                idas.set(u"idcut", startSegment["idcutting"])
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
                idas.set(u"idseg", startSegment["idsegment"])
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
    
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   143
        lxml.etree.SubElement(iri, "edits")
112
9886ab183b09 add permalink + corrcetion config with static and media path. update urls
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   144
        return doc
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
        
167
fe00e7302efe Change class and functions names to follow PEP8 formatting standards
verrierj
parents: 134
diff changeset
   146
    def generate_init(self, url, method, search=None):
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
                
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
        iri = lxml.etree.Element('iri')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
 
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   150
        elementFiles = lxml.etree.SubElement(iri, 'files')    
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
        elementInit = lxml.etree.SubElement(elementFiles, 'init')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
        elementfile = lxml.etree.SubElement(elementInit, 'file')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
            
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   154
        elementfile.set('src', settings.WEB_URL + django.core.urlresolvers.reverse(method, args=url))
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   155
        elementfile.set('src', django.core.urlresolvers.reverse(method, args=url))
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        if(search):
69
784ebba76424 enhance new media/content creation with url : external stream, local stream using STREAM_URL or youtube http.
cavaliet
parents: 63
diff changeset
   157
            elementfile.set("segsel", settings.WEB_URL + django.core.urlresolvers.reverse(search, args=url))
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   159
        lxml.etree.SubElement(elementFiles, 'recent')
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   160
        lxml.etree.SubElement(elementFiles, 'library')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
        return iri 
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   165
class LdtAnnotation:
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   166
    
336
24eea54ed5f9 Anonymous user can add annotations
verrierj
parents: 332
diff changeset
   167
    def __init__(self, project, force_save=False):
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   168
        self.project = project
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   169
        self.parser = lxml.etree.XMLParser(remove_blank_text=True)
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   170
        self.ldtdoc = lxml.etree.parse(StringIO(project.ldt.encode("utf-8")), self.parser)
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   171
        self.to_add = True
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   172
        
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   173
    #   add( a['media'], a['type'],  a['type_title, a[data], '', a['tags'], begin, dur, author, date)
388
454bd3bd6ffd Debug api for annotation creation in embed player.
cavaliet
parents: 344
diff changeset
   174
    def add(self, media, cutting_id, cutting_title, title, text, tags_list, begin, dur, author, date, view_id="0", color="2194379"):
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   175
        """
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   176
        Add an annotation to a project. begin and dur must be strings. Default color is yellow.
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   177
        """
424
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 406
diff changeset
   178
        
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 406
diff changeset
   179
        if dur < 0:
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 406
diff changeset
   180
            self.to_add = False
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 406
diff changeset
   181
            return False
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 406
diff changeset
   182
            
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   183
        # We check if the project references the media.
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   184
        path_media = self.ldtdoc.xpath('/iri/medias/media[@id="%s"]' % media)
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   185
        if len(path_media) == 0:
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   186
            self.to_add = False
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   187
            return False
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   188
        
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   189
        # We get the content node
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   190
        path_annotations = self.ldtdoc.xpath('/iri/annotations')[0]
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   191
        path_content = path_annotations.xpath('content[@id="%s"]' % media)
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   192
        # If the content node does not exist, we create it
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   193
        if len(path_content) == 0:
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   194
            path_content = lxml.etree.SubElement(path_annotations, 'content')
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   195
            path_content.set('id', media)
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   196
            path_content = [path_content]
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   197
        
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   198
        # We generate the cutting id if necessary
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   199
        if cutting_id is None or cutting_id=="" :
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   200
            cutting_id = 'c_' + generate_uuid()
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   201
        
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   202
        # We get the ensemble node
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   203
        path_ensemble = path_content[0].xpath('ensemble[decoupage[@id="%s"]]' % cutting_id)
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   204
        if len(path_ensemble) == 0:
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   205
            # If the ensemble node does not exist, we create it
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   206
            path_ensemble = lxml.etree.SubElement(path_content[0], 'ensemble')
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   207
            path_ensemble.set('id', 'g_' + generate_uuid())
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   208
            path_ensemble.set('title', _('Personal cutting'))
388
454bd3bd6ffd Debug api for annotation creation in embed player.
cavaliet
parents: 344
diff changeset
   209
            path_ensemble.set('author', 'IRI')
454bd3bd6ffd Debug api for annotation creation in embed player.
cavaliet
parents: 344
diff changeset
   210
            path_ensemble.set('abstract', 'IRI')
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   211
            path_ensemble = [path_ensemble]
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   212
        #else:
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   213
        #    path_ensemble = path_content[0].xpath('ensemble')
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   214
        
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   215
            
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   216
        # We get the elements node in the good decoupage node
199
3d919c59b46f Fixed view bug in annotation handler
verrierj
parents: 196
diff changeset
   217
        ensemble_id = path_ensemble[0].get('id')
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   218
        decoupage_elements = path_ensemble[0].xpath('decoupage[@id="%s"]/elements' % cutting_id)
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   219
        if len(decoupage_elements) == 0:
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   220
            # If the decoupage node does not exist, we create it and its elements node 
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   221
            decoupage = lxml.etree.SubElement(path_ensemble[0], 'decoupage')
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   222
            #cutting_id = "c_" + generate_uuid()
199
3d919c59b46f Fixed view bug in annotation handler
verrierj
parents: 196
diff changeset
   223
            decoupage.set('id', cutting_id)
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   224
            decoupage.set('author', author)
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   225
            decoupage_title = lxml.etree.SubElement(decoupage, 'title')
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   226
            decoupage_title.text = cutting_title
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   227
            lxml.etree.SubElement(decoupage, 'abstract')
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   228
            decoupage_elements = lxml.etree.SubElement(decoupage, 'elements')
199
3d919c59b46f Fixed view bug in annotation handler
verrierj
parents: 196
diff changeset
   229
            decoupage_elements = [decoupage_elements]
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   230
        #else:
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   231
        #    cutting_id = path_ensemble[0].xpath('decoupage[title="%s"]' % cutting_title)[0].get('id')     
199
3d919c59b46f Fixed view bug in annotation handler
verrierj
parents: 196
diff changeset
   232
        
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   233
        # We add the cutting to the view
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   234
        if view_id is not None and view_id!="" :
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   235
            path_view = self.ldtdoc.xpath('/iri/displays/display[@id="%s"]' % view_id)
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   236
            if len(path_view) == 0:
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   237
                path_view = self.ldtdoc.xpath('/iri/displays/display[@title="Init view"]')
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   238
            else :
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   239
                content_display = path_view[0].xpath('content[@id="%s"]' % media)
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   240
                if len(content_display) == 0:
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   241
                    content_display = lxml.etree.SubElement(path_view[0], 'content')
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   242
                    content_display.set('id', media)
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   243
                    content_display = [content_display]
488
403f92f24144 Bugfixes in search page + update settings metadataplayer
verrierj
parents: 482
diff changeset
   244
                    # We add the decoupage node to the content node
403f92f24144 Bugfixes in search page + update settings metadataplayer
verrierj
parents: 482
diff changeset
   245
                    dec = lxml.etree.SubElement(content_display[0], 'decoupage')
403f92f24144 Bugfixes in search page + update settings metadataplayer
verrierj
parents: 482
diff changeset
   246
                    dec.set('idens', ensemble_id)
403f92f24144 Bugfixes in search page + update settings metadataplayer
verrierj
parents: 482
diff changeset
   247
                    dec.set('id', cutting_id)
403f92f24144 Bugfixes in search page + update settings metadataplayer
verrierj
parents: 482
diff changeset
   248
                    dec.set('tagsSelect', '')
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   249
        
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   250
        # We add the annotation/element node
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   251
        element = lxml.etree.SubElement(decoupage_elements[0], 'element')
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   252
        id_annotation = 's_' + generate_uuid()
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   253
        element.set('id', id_annotation)
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   254
        element.set('begin', begin)
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   255
        element.set('dur', dur)
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   256
        element.set('author', author)
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   257
        element.set('date', date)
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   258
        element.set('color', color)
337
496bd82719e5 Little correction on project API.
cavaliet
parents: 336
diff changeset
   259
        element.set('src', "")
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   260
        abstract = lxml.etree.SubElement(element, 'abstract')
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   261
        abstract.text = text
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   262
        title_node = lxml.etree.SubElement(element, 'title')
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   263
        title_node.text = title
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   264
        audio = lxml.etree.SubElement(element, 'audio')
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   265
        audio.set('source', '')
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   266
        tags = lxml.etree.SubElement(element, 'tags')
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   267
                                
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   268
        polemics = self.get_polemic_syntax(title)
482
c802e00c7131 Lot of bugfixes
verrierj
parents: 481
diff changeset
   269
        
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   270
        if polemics:
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   271
            meta = lxml.etree.SubElement(element, 'meta')
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   272
            polemics_node = lxml.etree.SubElement(meta, 'polemics')
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   273
            
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   274
            for polemic in polemics:
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   275
                polemic_node = lxml.etree.SubElement(polemics_node, 'polemic')
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   276
                polemic_node.text = polemic
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   277
        
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   278
        for tag in tags_list:
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   279
            tag_node = lxml.etree.SubElement(tags, 'tag')
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   280
            tag_node.text = tag
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   281
        
388
454bd3bd6ffd Debug api for annotation creation in embed player.
cavaliet
parents: 344
diff changeset
   282
        return cutting_id, id_annotation
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   283
    
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   284
    def save(self):
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   285
        if self.to_add:
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   286
            self.project.ldt = lxml.etree.tostring(self.ldtdoc, pretty_print=True)
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 223
diff changeset
   287
            #assert False, " TIBO SAVE " + self.project.ldt
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   288
            self.project.save()
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   289
       
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   290
    def get_polemic_syntax(self, text):
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   291
        polemics = []
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   292
        if '++' in text:
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   293
            polemics.append('OK')
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   294
        if '--' in text:
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   295
            polemics.append('KO')
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   296
        if '==' in text:
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   297
            polemics.append('REF')
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   298
        if '??' in text:
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   299
            polemics.append('Q')
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   300
        return polemics   
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   301
    
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   302
    def __del__(self):
481
cba0c297bc88 Polemic syntax can be used in API
verrierj
parents: 480
diff changeset
   303
       self.save()
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   305
def create_ldt(project, user, cuttings=[]):
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
    """create xml"""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
    contentList = project.contents.all()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
    # create a dom
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
    iri = lxml.etree.Element('iri')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
    doc = lxml.etree.ElementTree(iri)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
    #node project
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
    elementProject = lxml.etree.SubElement(iri, 'project')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
    
179
03007a7c14ca Corrected bug in description display
verrierj
parents: 176
diff changeset
   318
    elementProject.set('abstract', project.description)
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
    elementProject.set('title', project.title)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
    elementProject.set('user', user.username)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
    elementProject.set('id', project.ldt_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
    #node medias
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
    elementMedias = lxml.etree.SubElement(iri, 'medias')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
    idsel = None      
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
    for content in contentList:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
        if not idsel:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
            idsel = content.iri_id
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
        elementMedia = lxml.etree.SubElement(elementMedias, 'media')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
        elementMedia.set('id', content.iri_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
        elementMedia.set('src', content.iri_url())
69
784ebba76424 enhance new media/content creation with url : external stream, local stream using STREAM_URL or youtube http.
cavaliet
parents: 63
diff changeset
   333
        
784ebba76424 enhance new media/content creation with url : external stream, local stream using STREAM_URL or youtube http.
cavaliet
parents: 63
diff changeset
   334
        if content.videopath != None :
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
            elementMedia.set('video', content.videopath)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
        else:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
            elementMedia.set('video', settings.STREAM_URL)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
        elementMedia.set('pict', "")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
        elementMedia.set('extra', "")
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   340
        
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
    if not idsel:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
        idsel = ""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
    #node annotations
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   345
    annotations = lxml.etree.SubElement(iri, 'annotations')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
    #node displays
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
    elementDisplays = lxml.etree.SubElement(iri, 'displays')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
    elementDisplay = lxml.etree.SubElement(elementDisplays, 'display')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
    elementDisplay.set('id', '0')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
    elementDisplay.set('title', 'Init view')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
    elementDisplay.set('idsel', idsel)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
    elementDisplay.set('tc', '0')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
    elementDisplay.set('zoom', '0')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
    elementDisplay.set('scroll', '0')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
    elementDisplay.set('infoBAB', '')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
    #node content
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
    for content in contentList:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
        elementContent = lxml.etree.SubElement(elementDisplay, 'content')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
        elementContent.set('id', content.iri_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
        if not 'http' in content.iriurl:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
        #eg: "iiiielizabethrosse/ENMI08-III_elizabethrosse.iri"
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
            url = content.iri_url()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
        else:
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
            url = content.iriurl
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
        file = urllib.urlopen(url)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
        doc = lxml.etree.parse(file)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
        res = doc.xpath("/iri/body/ensembles/ensemble/decoupage")        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
        #node decoupage
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
        for decoupagenode in res:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
            decoupage_id = decoupagenode.get(u"id")
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
            parent = decoupagenode.getparent()
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
            ensemble_id = parent.get(u"id")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
            elementDecoupage = lxml.etree.SubElement(elementContent, 'decoupage')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
            elementDecoupage.set('idens', ensemble_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
            elementDecoupage.set('id', decoupage_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
    #node edits
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   383
    lxml.etree.SubElement(iri, 'edits')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
    
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   385
    # add cuttings to the first content if necessary
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   386
    if cuttings and contentList:
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   387
        content = contentList[0]
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   388
        
406
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   389
        ensemble_id = "g_%s" % generate_uuid()
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   390
        cuttings_id = []
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   391
        
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   392
        content_node = lxml.etree.SubElement(annotations, 'content')
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   393
        content_node.set('id', content.iri_id)
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   394
        ensemble = lxml.etree.SubElement(content_node, 'ensemble')
480
5cc5afa71d7e Minor bugs
verrierj
parents: 424
diff changeset
   395
        ensemble.set('title', _('Personal cutting'))
406
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   396
        ensemble.set('id', ensemble_id)
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   397
        ensemble.set('author', 'undefined')
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   398
        ensemble.set('abstract', '')
392
65c1898141da Front projects are created by admins
verrierj
parents: 383
diff changeset
   399
        ensemble.set('idProject', project.ldt_id)
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   400
        
406
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   401
        #Add cuttings to tag annotations
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   402
        for cutting in cuttings:
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   403
            cutting_node = lxml.etree.SubElement(ensemble, 'decoupage')
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   404
            cutting_node.set('author', 'perso')
406
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   405
            cutting_id = "c_%s" % generate_uuid()
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   406
            cuttings_id.append(cutting_id)
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   407
            cutting_node.set('id', cutting_id)
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   408
            title = lxml.etree.SubElement(cutting_node, 'title')
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   409
            title.text = cutting
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   410
            lxml.etree.SubElement(cutting_node, 'abstract')
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   411
            lxml.etree.SubElement(cutting_node, 'elements')
406
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   412
            
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   413
        #Add cuttings to tag displays
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   414
        content_node = elementDisplays.xpath('display/content[@id=\'%s\']' % content.iri_id)[0]
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   415
        for cutting_id in cuttings_id:
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   416
            cutting_node = lxml.etree.SubElement(content_node, 'decoupage')
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   417
            cutting_node.set('idens', ensemble_id)
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   418
            cutting_node.set('id', cutting_id)
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   419
            cutting_node.set('tagSelect', '')            
c0c4cd87daf2 Add new cuttings of front project to tag displays + minor bugs
verrierj
parents: 394
diff changeset
   420
        
383
a99ea8eb8b9a Front projects are created with each new content
verrierj
parents: 344
diff changeset
   421
    
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
    #write dom in Project.ldt 
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
    project.ldt = lxml.etree.tostring(iri, pretty_print=True)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
    #save Project
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
    project.save()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
    return project        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
def copy_ldt(project, new_project, user):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
    new_project.ldt_id = str(uuid.uuid1())
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   432
    new_project.created_by = user.username
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   433
    new_project.changed_by = user.username
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
    new_project.state = 1
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
    """create xml"""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
    ldt = lxml.etree.fromstring(project.ldt.encode("utf-8"))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
    res = ldt.xpath("/iri/project")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
    for elementProject in res:
182
b7add86c2772 Resized modal windows #3 working on Firefox, Chrome, IE8 + update jQuery
verrierj
parents: 179
diff changeset
   442
        elementProject.set('abstract', project.get_description())
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
        elementProject.set('title', new_project.title)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
        elementProject.set('user', user.username)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
        elementProject.set('id', new_project.ldt_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
    new_project.ldt = lxml.etree.tostring(ldt, pretty_print=True)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
    #save Project
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
    new_project.save()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
    return new_project
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
def create_empty_iri(file, content, username):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
    iri = lxml.etree.Element('iri')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
    doc = lxml.etree.ElementTree(iri)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
    head = lxml.etree.SubElement(iri, 'head')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
    meta_id = lxml.etree.SubElement(head, 'meta')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
    meta_id.set(u'name', u'id')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
    meta_id.set(u'content', unicode(content.iri_id))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
    meta_title = lxml.etree.SubElement(head, 'meta')
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   463
    meta_title.set(u'name', u'title')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
    meta_title.set(u'content', unicode(content.title))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
    meta_abstract = lxml.etree.SubElement(head, 'meta')
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   466
    meta_abstract.set(u'name', u'abstract')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
    meta_abstract.set(u'content', unicode(content.description))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
    meta_author = lxml.etree.SubElement(head, 'meta')
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   469
    meta_author.set(u'name', u'author')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
    meta_author.set(u'content', unicode(username))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
    meta_contributor = lxml.etree.SubElement(head, 'meta')
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   472
    meta_contributor.set(u'name', u'contributor')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
    meta_contributor.set(u'content', unicode(username))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
    meta_date = lxml.etree.SubElement(head, 'meta')
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   475
    meta_date.set(u'name', u'date')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
    meta_date.set(u'content', unicode(datetime.date.today().isoformat()))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
    meta_copyright = lxml.etree.SubElement(head, 'meta')
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   478
    meta_copyright.set(u'name', u'copyright')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
    meta_copyright.set(u'content', u'IRI')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
    meta_type = lxml.etree.SubElement(head, 'meta')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
    meta_type.set(u'name', u'type')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
    meta_type.set(u'content', u'video')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
    body = lxml.etree.SubElement(iri, 'body')
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   485
    lxml.etree.SubElement(body, 'ensembles')
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   486
    lxml.etree.SubElement(body, 'links')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
    medias = lxml.etree.SubElement(body, 'medias')
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
    media_video = lxml.etree.SubElement(medias, 'media')
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   491
    media_video.set(u'id', u'video')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
    video = lxml.etree.SubElement(media_video, 'video')
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   493
    video.set(u'src', unicode(content.stream_src))
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   494
    video.set(u'id', unicode(content.iri_id))
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   495
    video.set(u'dur', unicode(content.duration))
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   496
    video.set(u'begin', u'0')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
    media_tool = lxml.etree.SubElement(medias, 'media')
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   499
    media_tool.set(u'id', u'tool')
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   500
    lxml.etree.SubElement(media_tool, 'tool')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   502
    lxml.etree.SubElement(body, 'display')
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
    doc.write(file, pretty_print=True)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
def update_iri(filepath, content, username):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
    # open xml
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
    doc = lxml.etree.parse(filepath)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
    res = doc.xpath("/iri/head/meta")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
    # update meta
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
    for meta_node in res:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
        meta_name = meta_node.get("name")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
        content_attr = None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
        if meta_name == u'id':
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
            content_attr = unicode(content.iri_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
        elif meta_name == u'title':
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
            content_attr = unicode(content.title)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
        elif meta_name == u'abstract':
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
            content_attr = unicode(content.description)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
        elif meta_name == u'contributor':
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
            content_attr = unicode(username)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
        elif meta_name == u"date":
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
            content_attr = unicode(datetime.date.today().isoformat())
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
        if content_attr is not None:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
            meta_node.set(u"content", content_attr)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
    res = doc.xpath("/iri/body/medias/media[@id='video']/video")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
    if len(res) > 0:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
        video_node = res[0]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
        video_node.set(u'src', unicode(content.stream_src))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
        video_node.set(u'dur', unicode(content.duration))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
        video_node.set(u'id', unicode(content.iri_id))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
    # update video
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
    f = open(filepath, "w")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
    try:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
        doc.write(f, encoding="UTF-8", pretty_print=True, xml_declaration=True)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
    finally:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
        f.close()
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 182
diff changeset
   545
        
223
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   546
def clean_description(description):
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   547
    """ Remove html tags added by flash if necessary """
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   548
    new_desc = u''
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   549
    begin_str = "KERNING=\"0\">"  
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   550
                
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   551
    for chunk in description.split("<TEXTFORMAT"):
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   552
        begin = chunk.find(begin_str) + len(begin_str)
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   553
        end = chunk.find("</FONT")
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   554
    
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   555
        if begin > 0 and end > 0:
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   556
            new_desc = new_desc +  chunk[begin:end] + "<br />"
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   557
        
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   558
    if new_desc:
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   559
        return new_desc
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   560
    return None         
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   561
        
31cb29055591 HTML tags deletion in project description works with multiple paragraphs
verrierj
parents: 216
diff changeset
   562