web/ldt/ldt_utils/projectserializer.py
changeset 100 c5514a2bfdcd
parent 99 0fb4b009c6eb
child 137 d363fa6232b2
equal deleted inserted replaced
99:0fb4b009c6eb 100:c5514a2bfdcd
    16         self.project = project
    16         self.project = project
    17         self.parsed = False
    17         self.parsed = False
    18         self.ldt_doc = None
    18         self.ldt_doc = None
    19         self.medias_dict = SortedDict()
    19         self.medias_dict = SortedDict()
    20         self.annotations_dict = SortedDict()
    20         self.annotations_dict = SortedDict()
       
    21         self.annotations_by_annotation_types = {}
    21         self.tags = {}
    22         self.tags = {}
    22         self.tags_dict = SortedDict()
    23         self.tags_dict = SortedDict()
    23         self.annotation_types_dict = SortedDict()
    24         self.annotation_types_dict = SortedDict()
    24         self.views_dict = SortedDict()
    25         self.views_dict = SortedDict()
    25         self.lists_dict = SortedDict()
    26         self.lists_dict = SortedDict()
   132                 "dc:title":decoupage_title,
   133                 "dc:title":decoupage_title,
   133                 "dc:description":decoupage_description
   134                 "dc:description":decoupage_description
   134             }
   135             }
   135             
   136             
   136             self.annotation_types_dict[decoupage_id] = new_annotation_types
   137             self.annotation_types_dict[decoupage_id] = new_annotation_types
       
   138             self.annotations_by_annotation_types[decoupage_id] = []
   137                         
   139                         
   138             res = decoupage_node.xpath("elements/element")
   140             res = decoupage_node.xpath("elements/element")
   139             for element_node in res:
   141             for element_node in res:
   140                 
   142                 
   141                 element_id = element_node.attrib[u"id"]
   143                 element_id = element_node.attrib[u"id"]
   224                         "dc:modified": decoupage_modified
   226                         "dc:modified": decoupage_modified
   225                     }
   227                     }
   226                 }
   228                 }
   227                 
   229                 
   228                 self.annotations_dict[element_id] = new_annotation
   230                 self.annotations_dict[element_id] = new_annotation
       
   231                 self.annotations_by_annotation_types[decoupage_id].append(new_annotation)
   229         
   232         
   230         if not list_items:
   233         if not list_items:
   231             new_list["items"] = None
   234             new_list["items"] = None
   232         self.lists_dict[ensemble_id] = new_list
   235         self.lists_dict[ensemble_id] = new_list
   233 
   236 
   239         if self.from_display:
   242         if self.from_display:
   240             xpath_str = "/iri/displays/display[position()=1]"
   243             xpath_str = "/iri/displays/display[position()=1]"
   241             if isinstance(self.from_display, basestring):
   244             if isinstance(self.from_display, basestring):
   242                 xpath_str = "/iri/displays/display[@id='%s']" % self.from_display
   245                 xpath_str = "/iri/displays/display[@id='%s']" % self.from_display
   243             
   246             
   244             logging.debug("xpath_str " + xpath_str)
       
   245             self.__parse_views(self.ldt_doc.xpath(xpath_str))
   247             self.__parse_views(self.ldt_doc.xpath(xpath_str))
   246             logging.debug("xpath_str " + repr(self.views_dict))
       
   247         
   248         
   248         res = self.ldt_doc.xpath("/iri/medias/media")
   249         res = self.ldt_doc.xpath("/iri/medias/media")
   249         for mediaNode in res:
   250         for mediaNode in res:
   250             iri_id = mediaNode.attrib[u"id"]
   251             iri_id = mediaNode.attrib[u"id"]
   251             if self.from_display and iri_id not in self.display_contents_list:
   252             if self.from_display and iri_id not in self.display_contents_list:
   264                     continue
   265                     continue
   265                 ensemble_id = ensemble_node.get("id")
   266                 ensemble_id = ensemble_node.get("id")
   266                 if self.from_display and ensemble_id not in self.display_ensemble_list:
   267                 if self.from_display and ensemble_id not in self.display_ensemble_list:
   267                     continue
   268                     continue
   268                 self.__parse_ensemble(ensemble_node, content)            
   269                 self.__parse_ensemble(ensemble_node, content)            
       
   270         
       
   271         #reorder annotations and annotation type from view
       
   272         if self.from_display and len(self.views_dict) > 0:
       
   273             new_annotation_types_dict = SortedDict()
       
   274             new_annotations_dict = SortedDict()
       
   275             for annotation_type in self.display_cuttings_list:
       
   276                 if annotation_type in self.annotation_types_dict:
       
   277                     new_annotation_types_dict[annotation_type] = self.annotation_types_dict[annotation_type]
       
   278                     for annot in self.annotations_by_annotation_types[annotation_type]:
       
   279                         new_annotations_dict[annot['id']] = annot
       
   280                     
       
   281             self.annotations_dict = new_annotations_dict
       
   282             self.annotation_types_dict = new_annotation_types_dict
   269         
   283         
   270         self.parsed = True
   284         self.parsed = True
   271     
   285     
   272     def __parse_content(self, content):
   286     def __parse_content(self, content):
   273         
   287         
   358              'main_media': {"id-ref":project_main_media}
   372              'main_media': {"id-ref":project_main_media}
   359             }
   373             }
   360                 
   374                 
   361                     
   375                     
   362         res['medias'] =  self.medias_dict.values() if len(self.medias_dict) > 0 else None
   376         res['medias'] =  self.medias_dict.values() if len(self.medias_dict) > 0 else None
   363         res['annotation-types'] = self.annotation_types_dict.values() if len(self.annotation_types_dict) > 0 else None
       
   364         res['annotations'] = self.annotations_dict.values() if len(self.annotations_dict) > 0 else None 
       
   365         res['lists'] = self.lists_dict.values() if len(self.lists_dict) > 0 else None
   377         res['lists'] = self.lists_dict.values() if len(self.lists_dict) > 0 else None
   366         res['tags'] = self.tags.values() if len(self.tags) > 0 else None
   378         res['tags'] = self.tags.values() if len(self.tags) > 0 else None
   367         res['views'] = self.views_dict.values() if len(self.views_dict) > 0 else None
   379         res['views'] = self.views_dict.values() if len(self.views_dict) > 0 else None
   368         
   380         
       
   381         res['annotation-types'] = self.annotation_types_dict.values() if len(self.annotation_types_dict) > 0 else None
       
   382         res['annotations'] = self.annotations_dict.values() if len(self.annotations_dict) > 0 else None 
       
   383 
       
   384         
   369 
   385 
   370         return res
   386         return res
   371     
   387     
   372     def getAnnotations(self, first_cutting=True):
   388     def getAnnotations(self, first_cutting=True):
   373         
   389         
   377         annotations = []
   393         annotations = []
   378         
   394         
   379         current_cutting = None
   395         current_cutting = None
   380         uri = None
   396         uri = None
   381         for annot in self.annotations_dict.values():
   397         for annot in self.annotations_dict.values():
   382             if first_cutting and current_cutting and current_cuttings != annot['meta']['id-ref'] :
   398             logging.debug("current cutting" + repr(current_cutting) + " : annot " + annot['meta']['id-ref'])
       
   399             if first_cutting and current_cutting and current_cutting != annot['meta']['id-ref'] :
   383                 break
   400                 break
   384             current_cuttings = annot['meta']['id-ref']
   401             current_cutting = annot['meta']['id-ref']
   385             content_id = annot['media']
   402             content_id = annot['media']
   386             content = Content.objects.get(iri_id=content_id)
   403             content = Content.objects.get(iri_id=content_id)
   387             if annot['tags']:
   404             if annot['tags']:
   388                 tags_list = map(lambda tag_entry: self.tags_dict[tag_entry['id-ref']]['meta']['dc:title'],annot['tags'])
   405                 tags_list = map(lambda tag_entry: self.tags_dict[tag_entry['id-ref']]['meta']['dc:title'],annot['tags'])
   389             else:
   406             else: