| author | verrierj |
| Tue, 10 Jan 2012 16:09:28 +0100 | |
| changeset 344 | 76724cebff95 |
| parent 340 | 5f919a978f50 |
| parent 337 | 496bd82719e5 |
| child 383 | a99ea8eb8b9a |
| child 388 | 454bd3bd6ffd |
| permissions | -rw-r--r-- |
| 114 | 1 |
from copy import deepcopy |
| 19 | 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 | 6 |
import datetime |
| 0 | 7 |
import django.core.urlresolvers |
| 19 | 8 |
import lxml.etree |
| 0 | 9 |
import urllib |
| 19 | 10 |
import uuid |
| 0 | 11 |
|
12 |
__BOOLEAN_DICT = { |
|
13 |
'false':False, |
|
14 |
'true':True, |
|
15 |
'0':False, |
|
16 |
'1':True, |
|
17 |
't': True, |
|
18 |
'f':False |
|
19 |
} |
|
20 |
||
| 19 | 21 |
def reduce_text_node(element_node, xpath_str=None): |
22 |
node_list = [] |
|
23 |
if xpath_str is not None: |
|
24 |
node_list = element_node.xpath(xpath_str, smart_strings=False) |
|
25 |
else: |
|
26 |
node_list = [element_node.text] |
|
| 63 | 27 |
return reduce(lambda t, s: t + s, node_list , "") |
| 0 | 28 |
|
29 |
def boolean_convert(bool): |
|
30 |
if bool is None: |
|
31 |
return False |
|
32 |
if bool is True or bool is False: |
|
33 |
return bool |
|
34 |
key = str(bool).lower() |
|
35 |
return __BOOLEAN_DICT.get(key, False) |
|
36 |
||
37 |
def generate_uuid(): |
|
38 |
return unicode(uuid.uuid1()) |
|
39 |
||
40 |
class LdtSearch(object): |
|
41 |
||
42 |
def query(self, field, query): |
|
| 95 | 43 |
indexSearcher = get_searcher() |
| 176 | 44 |
hits = get_results_list(field, query) |
| 0 | 45 |
|
46 |
res = [] |
|
| 176 | 47 |
for hit in hits: |
| 0 | 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 | 50 |
indexSearcher.close() |
51 |
return res |
|
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 | 54 |
return self.query("all", query) |
| 176 | 55 |
|
| 0 | 56 |
|
57 |
class LdtUtils(object): |
|
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 | 60 |
|
61 |
iri = lxml.etree.Element(u'iri') |
|
62 |
doc = lxml.etree.ElementTree(iri) |
|
63 |
||
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 | 69 |
|
70 |
medias = lxml.etree.SubElement(iri, u"medias") |
|
71 |
for content in contentList: |
|
72 |
videopath = unicode(settings.STREAM_URL) |
|
| 115 | 73 |
if content.videopath != None : |
| 0 | 74 |
videopath = unicode(content.videopath) |
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 | 81 |
|
| 114 | 82 |
if projects is None: |
83 |
projects = [] |
|
84 |
annotations_nodes = {} |
|
| 115 | 85 |
for project in projects: |
| 114 | 86 |
ldtdoc = lxml.etree.fromstring(project.ldt.encode("utf-8")) |
87 |
res = ldtdoc.xpath("/iri/annotations/content") |
|
88 |
||
89 |
for content in res: |
|
90 |
contentid = content.get("id") |
|
91 |
if annotations_nodes.has_key(contentid): |
|
92 |
contentnode = annotations_nodes[contentid] |
|
93 |
else: |
|
94 |
contentnode = {"id":contentid, "ensembles":[]} |
|
95 |
annotations_nodes[contentid] = contentnode |
|
96 |
for ens in content: |
|
97 |
if ens.tag.endswith("ensemble"): |
|
98 |
contentnode["ensembles"].append(deepcopy(ens)) |
|
99 |
||
100 |
annotations = lxml.etree.SubElement(iri, "annotations") |
|
101 |
if len(annotations_nodes) > 0: |
|
102 |
for content in contentList: |
|
103 |
if content.iri_id in annotations_nodes: |
|
| 115 | 104 |
contentnode = annotations_nodes[content.iri_id] |
| 114 | 105 |
if contentnode is not None: |
| 115 | 106 |
content_node = lxml.etree.SubElement(annotations, "content") |
107 |
content_node.set("id", contentnode["id"]) |
|
108 |
content_node.text = u"" |
|
109 |
for ens in contentnode["ensembles"]: |
|
110 |
content_node.append(ens) |
|
| 0 | 111 |
|
112 |
||
113 |
displays = lxml.etree.SubElement(iri, "displays") |
|
114 |
if len(contentList) > 0: |
|
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 | 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 | 123 |
filepath = urllib.urlopen(content.iri_url()) |
124 |
||
125 |
udoc = lxml.etree.parse(filepath) |
|
126 |
res = udoc.xpath("/iri/body/ensembles/ensemble/decoupage") |
|
127 |
for decoupagenode in res: |
|
128 |
decoupage_id = decoupagenode.get(u"id") |
|
129 |
ensemble_id = decoupagenode.getparent().get(u"id") |
|
130 |
decoupage_id = decoupagenode.get(u"id") |
|
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 | 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 | 142 |
|
| 19 | 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 | 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 | 147 |
|
148 |
iri = lxml.etree.Element('iri') |
|
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 | 151 |
elementInit = lxml.etree.SubElement(elementFiles, 'init') |
152 |
elementfile = lxml.etree.SubElement(elementInit, 'file') |
|
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 | 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 | 158 |
|
| 19 | 159 |
lxml.etree.SubElement(elementFiles, 'recent') |
160 |
lxml.etree.SubElement(elementFiles, 'library') |
|
| 0 | 161 |
|
162 |
return iri |
|
163 |
||
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 | 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) |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
174 |
def add(self, media, cutting_id, cutting_title, title, text, tags_list, begin, dur, author, date, view_id=None, color="16776960"): |
|
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 |
""" |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
178 |
|
|
332
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
179 |
# 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
|
180 |
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
|
181 |
if len(path_media) == 0: |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
182 |
self.to_add = False |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
183 |
return False |
|
332
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
184 |
|
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
185 |
# We get the content node |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
186 |
path_annotations = self.ldtdoc.xpath('/iri/annotations')[0] |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
187 |
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
|
188 |
# 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
|
189 |
if len(path_content) == 0: |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
190 |
path_content = lxml.etree.SubElement(path_annotations, 'content') |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
191 |
path_content.set('id', media) |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
192 |
path_content = [path_content] |
|
332
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
193 |
|
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
194 |
# 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
|
195 |
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
|
196 |
cutting_id = 'c_' + generate_uuid() |
|
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 get the ensemble node |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
199 |
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
|
200 |
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
|
201 |
# 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
|
202 |
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
|
203 |
path_ensemble.set('id', 'g_' + generate_uuid()) |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
204 |
path_ensemble.set('title', _('Personal cutting')) |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
205 |
path_ensemble.set('author', 'undefined') |
|
332
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
206 |
path_ensemble = [path_ensemble] |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
207 |
#else: |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
208 |
# 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
|
209 |
|
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
210 |
|
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
211 |
# We get the elements node in the good decoupage node |
| 199 | 212 |
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
|
213 |
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
|
214 |
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
|
215 |
# 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
|
216 |
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
|
217 |
#cutting_id = "c_" + generate_uuid() |
| 199 | 218 |
decoupage.set('id', cutting_id) |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
219 |
decoupage.set('author', author) |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
220 |
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
|
221 |
decoupage_title.text = cutting_title |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
222 |
lxml.etree.SubElement(decoupage, 'abstract') |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
223 |
decoupage_elements = lxml.etree.SubElement(decoupage, 'elements') |
| 199 | 224 |
decoupage_elements = [decoupage_elements] |
|
332
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
225 |
#else: |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
226 |
# cutting_id = path_ensemble[0].xpath('decoupage[title="%s"]' % cutting_title)[0].get('id') |
| 199 | 227 |
|
|
332
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
228 |
# 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
|
229 |
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
|
230 |
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
|
231 |
if len(path_view) == 0: |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
232 |
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
|
233 |
else : |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
234 |
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
|
235 |
if len(content_display) == 0: |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
236 |
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
|
237 |
content_display.set('id', media) |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
238 |
content_display = [content_display] |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
239 |
# We add the decoupage node to the content node |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
240 |
dec = lxml.etree.SubElement(content_display[0], 'decoupage') |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
241 |
dec.set('idens', ensemble_id) |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
242 |
dec.set('id', cutting_id) |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
243 |
dec.set('tagsSelect', '') |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
244 |
|
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
245 |
# We add the annotation/element node |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
246 |
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
|
247 |
id_annotation = 's_' + generate_uuid() |
|
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
248 |
element.set('id', id_annotation) |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
249 |
element.set('begin', begin) |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
250 |
element.set('dur', dur) |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
251 |
element.set('author', author) |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
252 |
element.set('date', date) |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
253 |
element.set('color', color) |
| 337 | 254 |
element.set('src', "") |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
255 |
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
|
256 |
abstract.text = text |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
257 |
title_node = lxml.etree.SubElement(element, 'title') |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
258 |
title_node.text = title |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
259 |
audio = lxml.etree.SubElement(element, 'audio') |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
260 |
audio.set('source', '') |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
261 |
tags = lxml.etree.SubElement(element, 'tags') |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
262 |
|
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
263 |
for tag in tags_list: |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
264 |
tag_node = lxml.etree.SubElement(tags, 'tag') |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
265 |
tag_node.text = tag |
|
332
c28d4dc49a50
add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents:
223
diff
changeset
|
266 |
|
| 216 | 267 |
return id_annotation |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
268 |
|
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
269 |
def save(self): |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
270 |
if self.to_add: |
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
271 |
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
|
272 |
#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
|
273 |
self.project.save() |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
274 |
|
|
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
275 |
def __del__(self): |
| 336 | 276 |
self.save() |
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
277 |
|
| 0 | 278 |
|
279 |
def create_ldt(project, user): |
|
280 |
"""create xml""" |
|
281 |
||
282 |
||
283 |
contentList = project.contents.all() |
|
284 |
||
285 |
# create a dom |
|
286 |
iri = lxml.etree.Element('iri') |
|
287 |
doc = lxml.etree.ElementTree(iri) |
|
288 |
||
289 |
#node project |
|
290 |
elementProject = lxml.etree.SubElement(iri, 'project') |
|
291 |
||
| 179 | 292 |
elementProject.set('abstract', project.description) |
| 0 | 293 |
elementProject.set('title', project.title) |
294 |
elementProject.set('user', user.username) |
|
295 |
elementProject.set('id', project.ldt_id) |
|
296 |
||
297 |
#node medias |
|
298 |
elementMedias = lxml.etree.SubElement(iri, 'medias') |
|
299 |
||
300 |
idsel = None |
|
301 |
for content in contentList: |
|
302 |
if not idsel: |
|
303 |
idsel = content.iri_id |
|
304 |
elementMedia = lxml.etree.SubElement(elementMedias, 'media') |
|
305 |
elementMedia.set('id', content.iri_id) |
|
306 |
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
|
307 |
|
|
784ebba76424
enhance new media/content creation with url : external stream, local stream using STREAM_URL or youtube http.
cavaliet
parents:
63
diff
changeset
|
308 |
if content.videopath != None : |
| 0 | 309 |
elementMedia.set('video', content.videopath) |
310 |
else: |
|
311 |
elementMedia.set('video', settings.STREAM_URL) |
|
312 |
elementMedia.set('pict', "") |
|
313 |
elementMedia.set('extra', "") |
|
314 |
||
315 |
if not idsel: |
|
316 |
idsel = "" |
|
317 |
||
318 |
#node annotations |
|
| 19 | 319 |
lxml.etree.SubElement(iri, 'annotations') |
| 0 | 320 |
|
321 |
#node displays |
|
322 |
elementDisplays = lxml.etree.SubElement(iri, 'displays') |
|
323 |
elementDisplay = lxml.etree.SubElement(elementDisplays, 'display') |
|
324 |
elementDisplay.set('id', '0') |
|
325 |
elementDisplay.set('title', 'Init view') |
|
326 |
elementDisplay.set('idsel', idsel) |
|
327 |
elementDisplay.set('tc', '0') |
|
328 |
elementDisplay.set('zoom', '0') |
|
329 |
elementDisplay.set('scroll', '0') |
|
330 |
elementDisplay.set('infoBAB', '') |
|
331 |
||
332 |
||
333 |
#node content |
|
334 |
for content in contentList: |
|
335 |
elementContent = lxml.etree.SubElement(elementDisplay, 'content') |
|
336 |
elementContent.set('id', content.iri_id) |
|
337 |
||
338 |
if not 'http' in content.iriurl: |
|
339 |
#eg: "iiiielizabethrosse/ENMI08-III_elizabethrosse.iri" |
|
340 |
url = content.iri_url() |
|
341 |
else: |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
342 |
url = content.iriurl |
| 0 | 343 |
file = urllib.urlopen(url) |
344 |
doc = lxml.etree.parse(file) |
|
345 |
res = doc.xpath("/iri/body/ensembles/ensemble/decoupage") |
|
346 |
||
347 |
#node decoupage |
|
348 |
for decoupagenode in res: |
|
349 |
decoupage_id = decoupagenode.get(u"id") |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
350 |
parent = decoupagenode.getparent() |
| 0 | 351 |
ensemble_id = parent.get(u"id") |
352 |
elementDecoupage = lxml.etree.SubElement(elementContent, 'decoupage') |
|
353 |
elementDecoupage.set('idens', ensemble_id) |
|
354 |
elementDecoupage.set('id', decoupage_id) |
|
355 |
||
356 |
#node edits |
|
| 19 | 357 |
lxml.etree.SubElement(iri, 'edits') |
| 0 | 358 |
|
359 |
#write dom in Project.ldt |
|
360 |
project.ldt = lxml.etree.tostring(iri, pretty_print=True) |
|
361 |
||
362 |
#save Project |
|
363 |
project.save() |
|
364 |
return project |
|
365 |
||
366 |
||
367 |
def copy_ldt(project, new_project, user): |
|
368 |
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
|
369 |
new_project.created_by = user.username |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
370 |
new_project.changed_by = user.username |
| 0 | 371 |
new_project.state = 1 |
372 |
|
|
373 |
|
|
374 |
"""create xml""" |
|
375 |
||
376 |
ldt = lxml.etree.fromstring(project.ldt.encode("utf-8")) |
|
377 |
res = ldt.xpath("/iri/project") |
|
378 |
for elementProject in res: |
|
|
182
b7add86c2772
Resized modal windows #3 working on Firefox, Chrome, IE8 + update jQuery
verrierj
parents:
179
diff
changeset
|
379 |
elementProject.set('abstract', project.get_description()) |
| 0 | 380 |
elementProject.set('title', new_project.title) |
381 |
elementProject.set('user', user.username) |
|
382 |
elementProject.set('id', new_project.ldt_id) |
|
383 |
||
384 |
new_project.ldt = lxml.etree.tostring(ldt, pretty_print=True) |
|
385 |
||
386 |
#save Project |
|
387 |
new_project.save() |
|
388 |
return new_project |
|
389 |
||
390 |
def create_empty_iri(file, content, username): |
|
391 |
||
392 |
iri = lxml.etree.Element('iri') |
|
393 |
doc = lxml.etree.ElementTree(iri) |
|
394 |
||
395 |
head = lxml.etree.SubElement(iri, 'head') |
|
396 |
meta_id = lxml.etree.SubElement(head, 'meta') |
|
397 |
meta_id.set(u'name', u'id') |
|
398 |
meta_id.set(u'content', unicode(content.iri_id)) |
|
399 |
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
|
400 |
meta_title.set(u'name', u'title') |
| 0 | 401 |
meta_title.set(u'content', unicode(content.title)) |
402 |
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
|
403 |
meta_abstract.set(u'name', u'abstract') |
| 0 | 404 |
meta_abstract.set(u'content', unicode(content.description)) |
405 |
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
|
406 |
meta_author.set(u'name', u'author') |
| 0 | 407 |
meta_author.set(u'content', unicode(username)) |
408 |
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
|
409 |
meta_contributor.set(u'name', u'contributor') |
| 0 | 410 |
meta_contributor.set(u'content', unicode(username)) |
411 |
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
|
412 |
meta_date.set(u'name', u'date') |
| 0 | 413 |
meta_date.set(u'content', unicode(datetime.date.today().isoformat())) |
414 |
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
|
415 |
meta_copyright.set(u'name', u'copyright') |
| 0 | 416 |
meta_copyright.set(u'content', u'IRI') |
417 |
meta_type = lxml.etree.SubElement(head, 'meta') |
|
418 |
meta_type.set(u'name', u'type') |
|
419 |
meta_type.set(u'content', u'video') |
|
420 |
||
421 |
body = lxml.etree.SubElement(iri, 'body') |
|
| 19 | 422 |
lxml.etree.SubElement(body, 'ensembles') |
423 |
lxml.etree.SubElement(body, 'links') |
|
| 0 | 424 |
|
425 |
medias = lxml.etree.SubElement(body, 'medias') |
|
426 |
||
427 |
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
|
428 |
media_video.set(u'id', u'video') |
| 0 | 429 |
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
|
430 |
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
|
431 |
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
|
432 |
video.set(u'dur', unicode(content.duration)) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
433 |
video.set(u'begin', u'0') |
| 0 | 434 |
|
435 |
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
|
436 |
media_tool.set(u'id', u'tool') |
| 19 | 437 |
lxml.etree.SubElement(media_tool, 'tool') |
| 0 | 438 |
|
| 19 | 439 |
lxml.etree.SubElement(body, 'display') |
| 0 | 440 |
|
441 |
doc.write(file, pretty_print=True) |
|
442 |
||
443 |
||
444 |
def update_iri(filepath, content, username): |
|
445 |
||
446 |
# open xml |
|
447 |
doc = lxml.etree.parse(filepath) |
|
448 |
||
449 |
res = doc.xpath("/iri/head/meta") |
|
450 |
# update meta |
|
451 |
||
452 |
for meta_node in res: |
|
453 |
meta_name = meta_node.get("name") |
|
454 |
content_attr = None |
|
455 |
if meta_name == u'id': |
|
456 |
content_attr = unicode(content.iri_id) |
|
457 |
elif meta_name == u'title': |
|
458 |
content_attr = unicode(content.title) |
|
459 |
elif meta_name == u'abstract': |
|
460 |
content_attr = unicode(content.description) |
|
461 |
elif meta_name == u'contributor': |
|
462 |
content_attr = unicode(username) |
|
463 |
elif meta_name == u"date": |
|
464 |
content_attr = unicode(datetime.date.today().isoformat()) |
|
465 |
if content_attr is not None: |
|
466 |
meta_node.set(u"content", content_attr) |
|
467 |
||
468 |
res = doc.xpath("/iri/body/medias/media[@id='video']/video") |
|
469 |
||
470 |
if len(res) > 0: |
|
471 |
video_node = res[0] |
|
472 |
video_node.set(u'src', unicode(content.stream_src)) |
|
473 |
video_node.set(u'dur', unicode(content.duration)) |
|
474 |
video_node.set(u'id', unicode(content.iri_id)) |
|
475 |
# update video |
|
476 |
||
477 |
f = open(filepath, "w") |
|
478 |
try: |
|
479 |
doc.write(f, encoding="UTF-8", pretty_print=True, xml_declaration=True) |
|
480 |
finally: |
|
481 |
f.close() |
|
|
196
b939a58d13b0
Moved code to add annotation to ldt_utils + added tests
verrierj
parents:
182
diff
changeset
|
482 |
|
|
223
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
483 |
def clean_description(description): |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
484 |
""" Remove html tags added by flash if necessary """ |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
485 |
new_desc = u'' |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
486 |
begin_str = "KERNING=\"0\">" |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
487 |
|
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
488 |
for chunk in description.split("<TEXTFORMAT"): |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
489 |
begin = chunk.find(begin_str) + len(begin_str) |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
490 |
end = chunk.find("</FONT") |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
491 |
|
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
492 |
if begin > 0 and end > 0: |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
493 |
new_desc = new_desc + chunk[begin:end] + "<br />" |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
494 |
|
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
495 |
if new_desc: |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
496 |
return new_desc |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
497 |
return None |
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
498 |
|
|
31cb29055591
HTML tags deletion in project description works with multiple paragraphs
verrierj
parents:
216
diff
changeset
|
499 |