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