| author | verrierj |
| Fri, 13 Jan 2012 17:05:50 +0100 | |
| changeset 385 | 64a187532417 |
| parent 382 | 123b89cf599e |
| parent 377 | a1f9f7583925 |
| child 404 | 4adc42ab55fd |
| permissions | -rw-r--r-- |
| 366 | 1 |
from django.conf import settings |
| 0 | 2 |
from datetime import datetime |
3 |
from django.utils.datastructures import SortedDict |
|
| 19 | 4 |
from ldt.ldt_utils.models import Content |
| 0 | 5 |
from ldt.ldt_utils.utils import reduce_text_node |
| 195 | 6 |
from ldt.ldt_utils.models import User, Project |
| 0 | 7 |
import logging |
8 |
import lxml.etree |
|
9 |
import uuid |
|
10 |
||
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
11 |
DATE_FORMATS = ["%d/%m/%Y", "%Y-%m-%d"] |
| 0 | 12 |
|
13 |
||
14 |
""" |
|
15 |
Serialize a project object to a cinelab compatible array |
|
16 |
""" |
|
17 |
class ProjectSerializer: |
|
18 |
||
| 366 | 19 |
def __init__(self, project, from_contents=True, from_display=True, first_cutting=None, viewable_contents=[]): |
| 0 | 20 |
self.project = project |
21 |
self.parsed = False |
|
22 |
self.ldt_doc = None |
|
23 |
self.medias_dict = SortedDict() |
|
24 |
self.annotations_dict = SortedDict() |
|
25 |
self.annotations_by_annotation_types = {} |
|
26 |
self.tags = {} |
|
27 |
self.tags_dict = SortedDict() |
|
28 |
self.annotation_types_dict = SortedDict() |
|
29 |
self.views_dict = SortedDict() |
|
30 |
self.lists_dict = SortedDict() |
|
31 |
self.serialize_contents = from_contents |
|
32 |
self.from_display = from_display |
|
33 |
self.display_contents_list = [] |
|
34 |
self.display_cuttings_list = [] |
|
35 |
self.display_ensemble_list = [] |
|
| 366 | 36 |
self.viewable_contents = viewable_contents |
37 |
self.first_cutting = first_cutting |
|
| 0 | 38 |
|
39 |
||
40 |
def __parse_views(self, display_node_list): |
|
41 |
for display_node in display_node_list: |
|
42 |
display_id = display_node.get(u"id", None) |
|
43 |
if not display_id: |
|
44 |
continue |
|
45 |
content_list = [] |
|
46 |
cuttings_list = [] |
|
| 366 | 47 |
|
| 0 | 48 |
new_display = { |
49 |
"id": display_id, |
|
50 |
"contents": content_list, |
|
51 |
"annotation_types": cuttings_list, |
|
52 |
} |
|
53 |
||
54 |
for content_node in display_node.xpath("content"): |
|
55 |
content_id = content_node.get("id") |
|
56 |
if content_id not in content_list: |
|
57 |
content_list.append(content_id) |
|
58 |
if content_id not in self.display_contents_list: |
|
59 |
self.display_contents_list.append(content_id) |
|
60 |
for cutting_node in content_node.xpath("decoupage"): |
|
61 |
cutting_id = cutting_node.get("id") |
|
62 |
if cutting_id not in cuttings_list: |
|
63 |
cuttings_list.append(cutting_id) |
|
64 |
if cutting_id not in self.display_cuttings_list: |
|
65 |
self.display_cuttings_list.append(cutting_id) |
|
66 |
ensemble_id = cutting_node.get("idens") |
|
67 |
if ensemble_id not in self.display_ensemble_list: |
|
68 |
self.display_ensemble_list.append(ensemble_id) |
|
| 366 | 69 |
|
70 |
# sets cutting to display in first position for the metadataplayer |
|
71 |
if self.first_cutting: |
|
72 |
||
73 |
annotation_types = new_display['annotation_types'] |
|
74 |
if len(annotation_types) > 1: |
|
75 |
index = -1 |
|
76 |
for i, s in enumerate(annotation_types): |
|
| 382 | 77 |
if s == self.first_cutting: |
| 366 | 78 |
index = i |
79 |
break |
|
80 |
if index >= 0: |
|
81 |
annotation_types[0], annotation_types[index] = annotation_types[index], annotation_types[0] |
|
| 382 | 82 |
|
| 0 | 83 |
self.views_dict[display_id] = new_display |
| 366 | 84 |
|
| 0 | 85 |
|
86 |
def __parse_ensemble(self, ensemble_node, content): |
|
87 |
||
88 |
ensemble_id = ensemble_node.attrib[u"id"] |
|
89 |
ensemble_author = ensemble_node.attrib[u"author"] |
|
90 |
ensemble_title = ensemble_node.attrib[u"title"] |
|
91 |
ensemble_description = ensemble_node.attrib[u"abstract"] |
|
92 |
ensemble_created = datetime.utcnow().isoformat() |
|
93 |
ensemble_modified = ensemble_created |
|
94 |
||
95 |
list_items = [] |
|
96 |
new_list = { |
|
97 |
"id" : ensemble_id, |
|
98 |
"items" : list_items, |
|
99 |
"meta" : { |
|
100 |
"dc:creator":ensemble_author, |
|
101 |
"dc:created": ensemble_created, |
|
102 |
"dc:contributor":"undefined", |
|
103 |
"dc:modified": ensemble_modified, |
|
104 |
"dc:title":ensemble_title, |
|
105 |
"dc:description": ensemble_description, |
|
106 |
"id-ref":content.iri_id, |
|
107 |
"editable":"false" |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
||
112 |
for decoupage_node in ensemble_node: |
|
113 |
if decoupage_node.tag != "decoupage" : |
|
114 |
continue |
|
115 |
||
116 |
decoupage_id = decoupage_node.attrib[ u"id"] |
|
117 |
if self.from_display and decoupage_id not in self.display_cuttings_list: |
|
118 |
continue |
|
119 |
decoupage_creator = decoupage_node.attrib[u"author"] |
|
120 |
if not decoupage_creator: |
|
121 |
decoupage_creator = "IRI" |
|
122 |
decoupage_contributor = decoupage_creator |
|
123 |
date_str = decoupage_node.get(u"date") |
|
124 |
decoupage_created = None |
|
125 |
if date_str : |
|
126 |
for date_format in DATE_FORMATS: |
|
127 |
try: |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
128 |
decoupage_created = datetime.strptime(date_str, date_format).isoformat() |
| 0 | 129 |
break |
130 |
except Exception: |
|
131 |
decoupage_created = None |
|
132 |
if decoupage_created is None: |
|
133 |
decoupage_created = datetime.utcnow().isoformat() |
|
134 |
decoupage_modified = decoupage_created |
|
135 |
||
136 |
decoupage_title = "" |
|
137 |
for txtRes in decoupage_node.xpath("title/text()", smart_strings=False): |
|
138 |
decoupage_title += txtRes |
|
139 |
||
140 |
decoupage_description = "" |
|
141 |
for txtRes in decoupage_node.xpath("abstract/text()", smart_strings=False): |
|
142 |
decoupage_description += txtRes |
|
143 |
||
144 |
||
145 |
list_items.append({"id-ref":decoupage_id}) |
|
146 |
||
147 |
new_annotation_types = { |
|
148 |
"id":decoupage_id, |
|
149 |
"dc:creator":decoupage_creator, |
|
150 |
"dc:created":decoupage_created, |
|
151 |
"dc:contributor":decoupage_contributor, |
|
152 |
"dc:modified":decoupage_modified, |
|
153 |
"dc:title":decoupage_title, |
|
154 |
"dc:description":decoupage_description |
|
155 |
} |
|
156 |
||
157 |
self.annotation_types_dict[decoupage_id] = new_annotation_types |
|
158 |
self.annotations_by_annotation_types[decoupage_id] = [] |
|
159 |
||
160 |
res = decoupage_node.xpath("elements/element") |
|
161 |
for element_node in res: |
|
162 |
||
163 |
element_id = element_node.attrib[u"id"] |
|
164 |
element_begin = element_node.attrib[u"begin"] |
|
165 |
element_duration = element_node.attrib[u"dur"] |
|
166 |
element_media = content.iri_id |
|
| 382 | 167 |
element_color = element_node.attrib.get(u"color", "") |
| 52 | 168 |
element_ldt_src = element_node.attrib.get(u"src", "") |
| 0 | 169 |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
170 |
element_title = reduce_text_node(element_node, "title/text()") |
| 0 | 171 |
element_description = reduce_text_node(element_node, "abstract/text()") |
| 19 | 172 |
|
173 |
element_source_node_list = element_node.xpath("meta/source") |
|
174 |
||
175 |
if len(element_source_node_list) > 0: |
|
176 |
element_source_node = element_source_node_list[0] |
|
177 |
element_source = {"mimetype" :element_source_node.get(u'mimetype'), "url":element_source_node.get(u'url'), "content":reduce_text_node(element_source_node)} |
|
178 |
else: |
|
179 |
element_source = None |
|
| 0 | 180 |
|
181 |
element_audio_src = "" |
|
182 |
element_audio_href = "" |
|
183 |
res = element_node.xpath("audio") |
|
184 |
if len(res) > 0: |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
185 |
element_audio_src = res[0].get(u"source", u"") |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
186 |
element_audio_href = res[0].text |
| 0 | 187 |
|
188 |
element_tags = [] |
|
189 |
||
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
190 |
tags = element_node.get(u"tags", u"") |
| 0 | 191 |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
192 |
tags_list = map(lambda s:s.strip(), tags.split(",")) |
| 0 | 193 |
|
194 |
#tags |
|
195 |
if tags is None or len(tags) == 0: |
|
196 |
tags_list = [] |
|
197 |
restagnode = element_node.xpath("tag/text()", smart_strings=False) |
|
198 |
for tagnode in restagnode: |
|
199 |
tags_list.append(tagnode) |
|
200 |
||
201 |
if tags_list is None or len(tags_list) == 0: |
|
202 |
tags_list = [] |
|
203 |
restagnode = element_node.xpath("tags/tag/text()", smart_strings=False) |
|
204 |
for tagnode in restagnode: |
|
205 |
tags_list.append(tagnode) |
|
206 |
||
207 |
tag_date = datetime.utcnow().isoformat() |
|
208 |
for tag_title in tags_list: |
|
209 |
if tag_title not in self.tags: |
|
210 |
tag_id = unicode(uuid.uuid1()) |
|
211 |
new_tag = { |
|
212 |
"id":tag_id, |
|
213 |
"meta" : { |
|
214 |
"dc:creator":"IRI", |
|
215 |
"dc:created": tag_date, |
|
216 |
"dc:contributor":"IRI", |
|
217 |
"dc:modified": tag_date, |
|
218 |
"dc:title":tag_title |
|
219 |
} |
|
220 |
} |
|
221 |
self.tags[tag_title] = new_tag |
|
222 |
self.tags_dict[tag_id] = new_tag |
|
223 |
element_tags.append({"id-ref":tag_id}) |
|
224 |
||
225 |
if not element_tags: |
|
226 |
element_tags = None |
|
227 |
||
228 |
new_annotation = { |
|
| 132 | 229 |
"begin": int(element_begin), |
| 0 | 230 |
"end": int(element_begin) + int(element_duration), |
231 |
"id": element_id, |
|
232 |
"media": element_media, |
|
| 52 | 233 |
"color": element_color, |
| 0 | 234 |
"content": { |
235 |
"mimetype": "application/x-ldt-structured", |
|
236 |
"title": element_title, |
|
237 |
"description": element_description, |
|
238 |
"color": element_color, |
|
| 52 | 239 |
"img": { |
| 63 | 240 |
"src": element_ldt_src, |
| 52 | 241 |
}, |
| 0 | 242 |
"audio": { |
243 |
"src" : element_audio_src, |
|
244 |
"mimetype": "audio/mp3", |
|
245 |
"href": element_audio_href |
|
246 |
}, |
|
247 |
"polemics" :[pol_elem.text for pol_elem in element_node.xpath("meta/polemics/polemic")], |
|
248 |
}, |
|
249 |
"tags": element_tags, |
|
250 |
"meta": { |
|
251 |
"id-ref": decoupage_id, |
|
252 |
"dc:creator": decoupage_creator, |
|
253 |
"dc:contributor": decoupage_contributor, |
|
254 |
"dc:created": decoupage_created, |
|
255 |
"dc:modified": decoupage_modified, |
|
256 |
} |
|
257 |
} |
|
258 |
||
| 19 | 259 |
if element_source: |
260 |
new_annotation['meta']['dc:source'] = element_source |
|
| 0 | 261 |
|
262 |
self.annotations_dict[element_id] = new_annotation |
|
263 |
self.annotations_by_annotation_types[decoupage_id].append(new_annotation) |
|
264 |
||
265 |
if not list_items: |
|
266 |
new_list["items"] = None |
|
267 |
self.lists_dict[ensemble_id] = new_list |
|
268 |
||
269 |
||
270 |
def __parse_ldt(self): |
|
271 |
||
272 |
self.ldt_doc = lxml.etree.fromstring(self.project.ldt.encode("utf-8")) |
|
273 |
||
274 |
if self.from_display: |
|
275 |
xpath_str = "/iri/displays/display[position()=1]" |
|
276 |
if isinstance(self.from_display, basestring): |
|
277 |
xpath_str = "/iri/displays/display[@id='%s']" % self.from_display |
|
278 |
||
279 |
self.__parse_views(self.ldt_doc.xpath(xpath_str)) |
|
280 |
||
281 |
res = self.ldt_doc.xpath("/iri/medias/media") |
|
282 |
for mediaNode in res: |
|
283 |
iri_id = mediaNode.attrib[u"id"] |
|
284 |
if self.from_display and iri_id not in self.display_contents_list: |
|
285 |
continue |
|
| 63 | 286 |
content = Content.objects.get(iri_id=iri_id) #@UndefinedVariable |
| 0 | 287 |
self.__parse_content(content) |
288 |
||
289 |
res = self.ldt_doc.xpath("/iri/annotations/content") |
|
290 |
for content_node in res: |
|
291 |
content_id = content_node.attrib[u"id"] |
|
292 |
if self.from_display and content_id not in self.display_contents_list: |
|
293 |
continue |
|
| 63 | 294 |
content = Content.objects.get(iri_id=content_id) #@UndefinedVariable |
| 0 | 295 |
for ensemble_node in content_node: |
296 |
if ensemble_node.tag != "ensemble" : |
|
297 |
continue |
|
298 |
ensemble_id = ensemble_node.get("id") |
|
299 |
if self.from_display and ensemble_id not in self.display_ensemble_list: |
|
300 |
continue |
|
301 |
self.__parse_ensemble(ensemble_node, content) |
|
302 |
||
303 |
#reorder annotations and annotation type from view |
|
304 |
if self.from_display and len(self.views_dict) > 0: |
|
305 |
new_annotation_types_dict = SortedDict() |
|
306 |
new_annotations_dict = SortedDict() |
|
307 |
for annotation_type in self.display_cuttings_list: |
|
308 |
if annotation_type in self.annotation_types_dict: |
|
309 |
new_annotation_types_dict[annotation_type] = self.annotation_types_dict[annotation_type] |
|
310 |
for annot in self.annotations_by_annotation_types[annotation_type]: |
|
311 |
new_annotations_dict[annot['id']] = annot |
|
312 |
||
313 |
self.annotations_dict = new_annotations_dict |
|
314 |
self.annotation_types_dict = new_annotation_types_dict |
|
315 |
||
316 |
self.parsed = True |
|
317 |
||
318 |
def __parse_content(self, content): |
|
319 |
||
320 |
doc = lxml.etree.parse(content.iri_file_path()) |
|
321 |
||
322 |
authors = content.authors.all() |
|
323 |
||
324 |
if len(authors) > 0 : |
|
325 |
author = authors[0].handle |
|
326 |
else : |
|
327 |
author = "IRI" |
|
328 |
||
329 |
if len(authors) > 1 : |
|
330 |
contributor = authors[1].handle |
|
331 |
else : |
|
332 |
contributor = author |
|
333 |
||
334 |
content_author = "" |
|
335 |
||
336 |
res = doc.xpath("/iri/head/meta[@name='author']/@content") |
|
337 |
if len(res) > 0: |
|
338 |
content_author = res[0] |
|
339 |
||
340 |
||
341 |
content_date = "" |
|
342 |
||
343 |
res = doc.xpath("/iri/head/meta[@name='date']/@content") |
|
344 |
if len(res) > 0: |
|
345 |
content_date = res[0] |
|
346 |
||
347 |
href = "" |
|
348 |
meta_item_value = "" |
|
| 366 | 349 |
if content.iri_id not in self.viewable_contents: |
350 |
href = settings.FORBIDDEN_STREAM_URL |
|
351 |
elif content.videopath: |
|
| 0 | 352 |
href = content.videopath.rstrip('/') + "/" + content.src |
353 |
meta_item_value = content.videopath.rstrip('/') + "/" |
|
| 377 | 354 |
else: |
355 |
href = content.src |
|
| 0 | 356 |
|
357 |
new_media = { |
|
358 |
"http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0", |
|
359 |
"id" : content.iri_id, |
|
360 |
"href" : href, |
|
361 |
"unit" : "ms", |
|
362 |
"origin" : "0", |
|
363 |
"meta": { |
|
364 |
"dc:creator" : author, |
|
365 |
"dc:created" : content.creation_date.isoformat(), |
|
366 |
"dc:contributor" : contributor, |
|
367 |
"dc:modified" : content.update_date.isoformat(), |
|
368 |
"dc:creator.contents" : content_author, |
|
369 |
"dc:created.contents" : content_date, |
|
370 |
"dc:title" : content.title, |
|
371 |
"dc:description" : content.description, |
|
372 |
"dc:duration" : content.get_duration(), |
|
373 |
"item": { |
|
374 |
"name" : "streamer", |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
375 |
"value": meta_item_value, |
| 0 | 376 |
}, |
377 |
} |
|
378 |
} |
|
379 |
||
380 |
self.medias_dict[content.iri_id] = new_media |
|
381 |
||
382 |
if self.serialize_contents: |
|
383 |
res = doc.xpath("/iri/body/ensembles/ensemble") |
|
384 |
for ensemble_node in res: |
|
385 |
self.__parse_ensemble(ensemble_node, content) |
|
386 |
||
387 |
||
388 |
def serialize_to_cinelab(self): |
|
389 |
||
390 |
res = {} |
|
391 |
||
392 |
if not self.parsed: |
|
393 |
self.__parse_ldt() |
|
394 |
||
395 |
||
396 |
project_main_media = "" |
|
397 |
if len(self.medias_dict) > 0: |
|
398 |
project_main_media = self.medias_dict.value_for_index(0)["id"] |
|
399 |
||
400 |
res['meta'] = { |
|
401 |
'id': self.project.ldt_id, |
|
402 |
'dc:created':self.project.creation_date.isoformat(), |
|
403 |
'dc:modified':self.project.modification_date.isoformat(), |
|
404 |
'dc:contributor':self.project.changed_by, |
|
405 |
'dc:creator':self.project.created_by, |
|
406 |
'dc:title':self.project.title, |
|
407 |
'dc:description':self.project.get_description(self.ldt_doc), # get from doc, parse ldt |
|
408 |
'main_media': {"id-ref":project_main_media} |
|
409 |
} |
|
410 |
||
411 |
||
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
412 |
res['medias'] = self.medias_dict.values() if len(self.medias_dict) > 0 else None |
| 0 | 413 |
res['lists'] = self.lists_dict.values() if len(self.lists_dict) > 0 else None |
414 |
res['tags'] = self.tags.values() if len(self.tags) > 0 else None |
|
415 |
res['views'] = self.views_dict.values() if len(self.views_dict) > 0 else None |
|
416 |
||
417 |
res['annotation-types'] = self.annotation_types_dict.values() if len(self.annotation_types_dict) > 0 else None |
|
| 382 | 418 |
res['annotations'] = self.annotations_dict.values() if len(self.annotations_dict) > 0 else None |
| 0 | 419 |
|
| 382 | 420 |
return res |
| 0 | 421 |
|
|
167
fe00e7302efe
Change class and functions names to follow PEP8 formatting standards
verrierj
parents:
132
diff
changeset
|
422 |
def get_annotations(self, first_cutting=True): |
| 0 | 423 |
|
424 |
if not self.parsed: |
|
425 |
self.__parse_ldt() |
|
426 |
||
427 |
annotations = [] |
|
428 |
||
429 |
current_cutting = None |
|
430 |
uri = None |
|
431 |
for annot in self.annotations_dict.values(): |
|
| 63 | 432 |
logging.debug("current cutting" + repr(current_cutting) + " : annot " + annot['meta']['id-ref']) #@UndefinedVariable |
| 0 | 433 |
if first_cutting and current_cutting and current_cutting != annot['meta']['id-ref'] : |
434 |
break |
|
435 |
current_cutting = annot['meta']['id-ref'] |
|
436 |
content_id = annot['media'] |
|
| 63 | 437 |
content = Content.objects.get(iri_id=content_id) #@UndefinedVariable |
| 0 | 438 |
if annot['tags']: |
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
439 |
tags_list = map(lambda tag_entry: self.tags_dict[tag_entry['id-ref']]['meta']['dc:title'], annot['tags']) |
| 0 | 440 |
else: |
441 |
tags_list = [] |
|
442 |
begin = int(annot['begin']) |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
443 |
duration = int(annot['end']) - begin |
| 0 | 444 |
if content.media_obj and content.media_obj.external_publication_url: |
445 |
uri = "%s#t=%d" % (content.media_obj.external_publication_url, begin) |
|
446 |
||
447 |
||
448 |
annotations.append({ |
|
449 |
'begin': begin, |
|
450 |
'duration':duration, |
|
451 |
'title':annot['content']['title'], |
|
452 |
'desc':annot['content']['description'], |
|
453 |
'tags': tags_list, |
|
454 |
'id':annot['id'], |
|
455 |
'uri':uri |
|
456 |
}) |
|
457 |
||
458 |
return annotations |
|
459 |
||
| 195 | 460 |
""" |
461 |
Quick and dirty converter from cinelab JSON to ldt format. |
|
462 |
Does not support imports, mutliple medias, or media creation |
|
463 |
""" |
|
464 |
class JsonCinelab2Ldt: |
|
465 |
||
466 |
def create_json(self, json): |
|
467 |
||
468 |
medias = json['medias'] |
|
469 |
contentList = [] |
|
470 |
for media in medias: |
|
471 |
c = Content.objects.get(iri_id=media['id']) |
|
472 |
if c != None: |
|
473 |
contentList.append(c) |
|
474 |
||
475 |
meta = json['meta'] |
|
476 |
creator = meta['creator'] |
|
477 |
contributor = meta['contributor'] |
|
478 |
||
479 |
user = User.objects.get(username=creator) |
|
480 |
project = Project.create_project(user, creator + '_' + contributor, contentList) |
|
481 |
project.changed_by = contributor |
|
482 |
||
483 |
ldtdoc = lxml.etree.fromstring(project.ldt.encode("utf-8")) |
|
484 |
element = ldtdoc.xpath('/iri/annotations') |
|
485 |
||
486 |
for media in contentList: |
|
487 |
content = lxml.etree.Element('content') |
|
488 |
content.set('id', media.iri_id) |
|
489 |
||
490 |
annotation_types = json['annotation_types'] |
|
491 |
cuttings = {} |
|
492 |
if len(annotation_types) > 0: |
|
493 |
media = lxml.etree.SubElement(element[0], 'content') |
|
494 |
media.set('id', medias[0]['id']) |
|
495 |
||
496 |
ens = lxml.etree.SubElement(media, 'ensemble') |
|
497 |
ens.set('title', 'Decoupages personnels') |
|
498 |
ens.set('idProject', project.ldt_id) |
|
499 |
ens.set('abstract', '') |
|
500 |
ens.set('id', 'g_' + str(uuid.uuid1())) |
|
501 |
||
502 |
for i in annotation_types: |
|
503 |
cutting_infos = {'desc' : i['meta']['description']} |
|
504 |
||
505 |
dec = lxml.etree.SubElement(ens, 'decoupage') |
|
506 |
dec.set('author', contributor) |
|
507 |
dec.set('id', 'c_' + str(uuid.uuid1())) |
|
508 |
elements_list = lxml.etree.SubElement(dec, 'elements') |
|
509 |
||
510 |
title = lxml.etree.SubElement(dec, 'title') |
|
511 |
title.text = i['id'] |
|
512 |
||
513 |
abstract = lxml.etree.SubElement(dec, 'abstract') |
|
514 |
abstract.text = i['meta']['description'] |
|
515 |
||
516 |
cutting_infos['xml_node'] = elements_list |
|
517 |
cuttings[i['id']] = cutting_infos |
|
518 |
||
519 |
||
520 |
annotations = json['annotations'] |
|
521 |
for i in annotations: |
|
522 |
cutting_infos = cuttings[i['type']] |
|
523 |
elements_node = cutting_infos['xml_node'] |
|
524 |
element = lxml.etree.SubElement(elements_node, 'element') |
|
525 |
||
526 |
element.set('begin', str(i['begin'])) |
|
527 |
element.set('dur', str(i['end'] - i['begin'])) |
|
528 |
element.set('id', 's_' + str(uuid.uuid1())) |
|
529 |
||
530 |
title = lxml.etree.SubElement(element, 'title') |
|
531 |
audio = lxml.etree.SubElement(element, 'audio') |
|
532 |
audio.set('source', 'undefined') |
|
533 |
abstract = lxml.etree.SubElement(element, 'abstract') |
|
534 |
abstract.text = i['content']['data'] |
|
535 |
tags = lxml.etree.SubElement(element, 'tags') |
|
536 |
for tag in i['tags']: |
|
537 |
tag_xml = lxml.etree.SubElement(tags, 'tag') |
|
538 |
tag_xml.text = tag |
|
539 |
||
540 |
||
541 |
project.ldt = lxml.etree.tostring(ldtdoc, pretty_print=True) |
|
542 |
project.save() |
|
| 0 | 543 |
|
| 195 | 544 |
return project.ldt |