13 def __init__(self, project, from_contents=True, from_display=True): |
13 def __init__(self, project, from_contents=True, from_display=True): |
14 self.project = project |
14 self.project = project |
15 self.parsed = False |
15 self.parsed = False |
16 self.ldt_doc = None |
16 self.ldt_doc = None |
17 self.medias = [] |
17 self.medias = [] |
|
18 self.medias_by_id = {} |
18 self.annotations = [] |
19 self.annotations = [] |
|
20 self.annotations_by_annotation_type = {} |
19 self.tags = {} |
21 self.tags = {} |
20 self.tags_by_id = {} |
22 self.tags_by_id = {} |
21 self.annotation_types = [] |
23 self.annotation_types = [] |
|
24 self.annotation_types_by_id = {} |
22 self.views = [] |
25 self.views = [] |
23 self.lists = [] |
26 self.lists = [] |
|
27 self.lists_by_id = {} |
24 self.serialize_contents = from_contents |
28 self.serialize_contents = from_contents |
25 self.from_display = from_display |
29 self.from_display = from_display |
26 |
30 |
27 |
31 |
28 def __parse_ensemble(self, ensemble_node, content): |
32 def __parse_ensemble(self, ensemble_node, content): |
93 "dc:modified":decoupage_modified, |
97 "dc:modified":decoupage_modified, |
94 "dc:title":decoupage_title, |
98 "dc:title":decoupage_title, |
95 "dc:description":decoupage_description |
99 "dc:description":decoupage_description |
96 } |
100 } |
97 |
101 |
98 self.annotation_types.append(new_annotation_types) |
102 self.annotation_types.append(new_annotation_types) |
|
103 self.annotation_types_by_id[decoupage_id] = new_annotation_types |
|
104 annotations_list = [] |
|
105 |
|
106 self.annotations_by_annotation_type[decoupage_id] = annotations_list |
99 |
107 |
100 res = decoupage_node.xpath("elements/element") |
108 res = decoupage_node.xpath("elements/element") |
101 for element_node in res: |
109 for element_node in res: |
102 |
110 |
103 element_id = element_node.attrib[u"id"] |
111 element_id = element_node.attrib[u"id"] |
186 "dc:modified": decoupage_modified |
194 "dc:modified": decoupage_modified |
187 } |
195 } |
188 } |
196 } |
189 |
197 |
190 self.annotations.append(new_annotation) |
198 self.annotations.append(new_annotation) |
|
199 annotations_list.append(new_annotation) |
191 |
200 |
192 if not list_items: |
201 if not list_items: |
193 new_list["items"] = None |
202 new_list["items"] = None |
194 self.lists.append(new_list) |
203 self.lists.append(new_list) |
|
204 self.lists_by_id[ensemble_id] = new_list |
195 |
205 |
196 |
206 |
197 def __parse_ldt(self): |
207 def __parse_ldt(self): |
198 |
208 |
199 self.ldt_doc = lxml.etree.fromstring(self.project.ldt.encode("utf-8")) |
209 self.ldt_doc = lxml.etree.fromstring(self.project.ldt.encode("utf-8")) |
210 content = Content.objects.get(iri_id=content_id) |
220 content = Content.objects.get(iri_id=content_id) |
211 for ensemble_node in content_node: |
221 for ensemble_node in content_node: |
212 if ensemble_node.tag != "ensemble" : |
222 if ensemble_node.tag != "ensemble" : |
213 continue |
223 continue |
214 self.__parse_ensemble(ensemble_node, content) |
224 self.__parse_ensemble(ensemble_node, content) |
215 |
225 |
216 # res = self.ldt_doc.xpath("/iri/displays/display") |
226 if self.from_display : |
217 # for display_node in res: |
227 annotations = [] |
218 # pass |
228 annotation_types = [] |
|
229 ensembles = [] |
|
230 medias = [] |
|
231 tags = [] |
|
232 xpath_str = "/iri/displays/display[position()=1]/content" |
|
233 if isinstance(self.from_display, basestring): |
|
234 xpath_str = "/iri/displays/display[id='%s']/content" % self.from_display |
|
235 |
|
236 for content_node in self.ldt_doc.xpath(xpath_str): |
|
237 content_id = content_node.get("id") |
|
238 if content_id not in medias: |
|
239 medias.append(content_id) |
|
240 for node in content_node.xpath("decoupage"): |
|
241 annotation_type_id = node.get('id') |
|
242 ensemble_id = node.get('idens') |
|
243 if annotation_type_id in self.annotations_by_annotation_type: |
|
244 annot_list = self.annotations_by_annotation_type[annotation_type_id] |
|
245 annotations.extend(annot_list) |
|
246 for annot in annot_list: |
|
247 if annot['tags']: |
|
248 for tag in annot['tags']: |
|
249 tag_id = tag['id-ref'] |
|
250 if tag_id not in tags: |
|
251 tags.append(tag_id) |
|
252 if annotation_type_id not in annotation_types: |
|
253 annotation_types.append(annotation_type_id) |
|
254 if ensemble_id not in ensembles: |
|
255 ensembles.append(ensemble_id) |
|
256 |
|
257 self.annotations = annotations |
|
258 self.annotation_types = map(lambda id: self.annotation_types_by_id[id], annotation_types) |
|
259 self.lists = map(lambda id: self.lists_by_id[id], ensembles) |
|
260 self.medias = map(lambda id: self.medias_by_id[id], medias) |
|
261 self.tags = {} |
|
262 for tag_id in tags: |
|
263 tag_inst = self.tags_by_id[tag_id] |
|
264 self.tags[tag_inst['meta']['dc:title']] = tag_inst |
219 |
265 |
220 self.parsed = True |
266 self.parsed = True |
221 |
267 |
222 def __parse_content(self, content): |
268 def __parse_content(self, content): |
223 |
269 |
271 }, |
317 }, |
272 } |
318 } |
273 } |
319 } |
274 |
320 |
275 self.medias.append(new_media) |
321 self.medias.append(new_media) |
|
322 self.medias_by_id[content.iri_id] = new_media |
276 |
323 |
277 if self.serialize_contents: |
324 if self.serialize_contents: |
278 res = doc.xpath("/iri/body/ensembles/ensemble") |
325 res = doc.xpath("/iri/body/ensembles/ensemble") |
279 for ensemble_node in res: |
326 for ensemble_node in res: |
280 self.__parse_ensemble(ensemble_node, content) |
327 self.__parse_ensemble(ensemble_node, content) |