Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
authorverrierj
Mon, 06 Feb 2012 12:42:53 +0100
changeset 532 155456f99f3d
parent 527 348844fc63ce
child 533 4d95862afbb2
Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
src/ldt/ldt/ldt_utils/projectserializer.py
src/ldt/ldt/ldt_utils/segmentserializer.py
--- a/src/ldt/ldt/ldt_utils/projectserializer.py	Mon Feb 06 10:35:48 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/projectserializer.py	Mon Feb 06 12:42:53 2012 +0100
@@ -69,26 +69,30 @@
                     if ensemble_id not in self.display_ensemble_list:
                         self.display_ensemble_list.append(ensemble_id)
             
+            
             # sets cutting to display in first position for the metadataplayer
             if self.first_cutting:
                 
                 annotation_types = new_display['annotation_types']
-                if len(annotation_types) > 1:
-                    index = -1
-                    for i, s in enumerate(annotation_types):
-                        if s == self.first_cutting: 
-                            index = i
-                            break
-                    if index >= 0:
-                        annotation_types[0], annotation_types[index] = annotation_types[index], annotation_types[0]
+                    
+                if self.first_cutting not in annotation_types:
+                    annotation_types.append(self.first_cutting)
+                    
+                index = -1
+                for i, s in enumerate(annotation_types):
+                    if s == self.first_cutting: 
+                        index = i
+                        break
+                        
+                annotation_types[0], annotation_types[index] = annotation_types[index], annotation_types[0]                        
             
             if self.only_one_cutting:
                 new_display['annotation_types'] = [new_display['annotation_types'][0]] 
-                            
+            
             self.views_dict[display_id] = new_display
             
     
-    def __parse_ensemble(self, ensemble_node, content):
+    def __parse_ensemble(self, ensemble_node, content, cutting_only=None):
         
         ensemble_id = ensemble_node.attrib[u"id"]
         ensemble_author = ensemble_node.attrib[u"author"]
@@ -113,13 +117,17 @@
             }
         }
         
-        
-        for decoupage_node in ensemble_node:
+        if cutting_only:
+            cuttings_list = cutting_only
+        else:
+            cuttings_list = ensemble_node             
+                
+        for decoupage_node in cuttings_list:            
             if decoupage_node.tag != "decoupage" :
                 continue
             
             decoupage_id = decoupage_node.attrib[ u"id"]
-            if self.from_display and decoupage_id not in self.display_cuttings_list:
+            if not cutting_only and self.from_display and decoupage_id not in self.display_cuttings_list:
                 continue
             decoupage_creator = decoupage_node.attrib[u"author"]
             if not decoupage_creator:
@@ -134,6 +142,7 @@
                         break
                     except Exception:
                         decoupage_created = None
+                                            
             if decoupage_created is None:
                 decoupage_created = datetime.utcnow().isoformat()
             decoupage_modified = decoupage_created
@@ -146,9 +155,8 @@
             for txtRes in decoupage_node.xpath("abstract/text()", smart_strings=False): 
                     decoupage_description += txtRes
 
-            
             list_items.append({"id-ref":decoupage_id})
-            
+                        
             new_annotation_types = {
                 "id":decoupage_id,
                 "dc:creator":decoupage_creator,
@@ -159,9 +167,9 @@
                 "dc:description":decoupage_description
             }
             
-            self.annotation_types_dict[decoupage_id] = new_annotation_types
+            self.annotation_types_dict[decoupage_id] = new_annotation_types                
             self.annotations_by_annotation_types[decoupage_id] = []
-                        
+                      
             res = decoupage_node.xpath("elements/element")
             for element_node in res:
                 
@@ -262,7 +270,7 @@
                         "dc:modified": decoupage_modified,
                     }
                 }
-                
+                               
                 if element_source:
                     new_annotation['meta']['dc:source'] = element_source
                 
@@ -295,7 +303,8 @@
             
         res = self.ldt_doc.xpath("/iri/annotations/content")
         for content_node in res:
-            content_id = content_node.attrib[u"id"]
+            content_id = content_node.attrib[u"id"]               
+            
             if self.from_display and content_id not in self.display_contents_list:
                 continue
             content = Content.objects.get(iri_id=content_id) #@UndefinedVariable
@@ -305,13 +314,25 @@
                 ensemble_id = ensemble_node.get("id")
                 if self.from_display and ensemble_id not in self.display_ensemble_list:
                     continue
-                self.__parse_ensemble(ensemble_node, content)            
+                self.__parse_ensemble(ensemble_node, content)
+                
+        if self.first_cutting and self.first_cutting not in self.display_cuttings_list:
+            
+            cutting_node= self.ldt_doc.xpath('/iri/annotations/content/ensemble/decoupage[@id=\'%s\']' % self.first_cutting)[0]
+            ensemble_node = cutting_node.xpath('..')[0]
+            content_node = ensemble_node.xpath('..')[0]            
+            
+            iri_id = content_node.get("id")
+            content = Content.objects.get(iri_id=iri_id)
+            
+            self.__parse_ensemble(ensemble_node, content, cutting_only=[cutting_node])
+            
         
         #reorder annotations and annotation type from view
         if self.from_display and len(self.views_dict) > 0:
             new_annotation_types_dict = SortedDict()
             new_annotations_dict = SortedDict()
-            for annotation_type in self.display_cuttings_list:
+            for annotation_type in self.display_cuttings_list + [self.first_cutting]:
                 if annotation_type in self.annotation_types_dict:
                     new_annotation_types_dict[annotation_type] = self.annotation_types_dict[annotation_type]
                     for annot in self.annotations_by_annotation_types[annotation_type]:
@@ -319,7 +340,7 @@
                     
             self.annotations_dict = new_annotations_dict
             self.annotation_types_dict = new_annotation_types_dict
-        
+                               
         self.parsed = True
     
     def __parse_content(self, content):
--- a/src/ldt/ldt/ldt_utils/segmentserializer.py	Mon Feb 06 10:35:48 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/segmentserializer.py	Mon Feb 06 12:42:53 2012 +0100
@@ -32,7 +32,10 @@
     def __get_cutting_title(self, project_id, content_id, ensemble_id, cutting_id):
         
         if not self.xml_docs.has_key(project_id):
-            project = Project.objects.get(ldt_id=project_id)
+            project = Project.objects.filter(ldt_id=project_id)
+            if not project:
+                return None
+            project = project[0]
             doc = lxml.etree.fromstring(project.ldt)
             self.xml_docs[project_id] = doc
         else: