web/ldt/ldt_utils/projectserializer.py
changeset 88 7f2c2d9adf58
parent 85 3b70d84e661a
child 89 30c6e597a7de
child 94 9927a619d2b5
--- a/web/ldt/ldt_utils/projectserializer.py	Tue Oct 12 19:12:27 2010 +0200
+++ b/web/ldt/ldt_utils/projectserializer.py	Thu Oct 14 11:36:12 2010 +0200
@@ -10,16 +10,19 @@
 """
 class ProjectSerializer:
     
-    def __init__(self, project, serialize_contents=True):
+    def __init__(self, project, from_contents=True, from_display=True):
         self.project = project
+        self.parsed = False
         self.ldt_doc = None
         self.medias = []
         self.annotations = []
         self.tags = {}
+        self.tags_by_id = {}
         self.annotation_types = []
         self.views = []
         self.lists = []
-        self.serialize_contents = serialize_contents
+        self.serialize_contents = from_contents
+        self.from_display = from_display
         
     
     def __parse_ensemble(self, ensemble_node, content):
@@ -152,6 +155,7 @@
                             }
                         }
                         self.tags[tag_title] = new_tag
+                        self.tags_by_id[tag_id] = new_tag
                     element_tags.append({"id-ref":tag_id})
 
                 if not element_tags:
@@ -200,8 +204,7 @@
             content = Content.objects.get(iri_id=iri_id)
             self.__parse_content(content)
             
-        res = self.ldt_doc.xpath("/iri/annotations/content")               
-        
+        res = self.ldt_doc.xpath("/iri/annotations/content")
         for content_node in res:
             content_id = content_node.attrib[u"id"]
             content = Content.objects.get(iri_id=content_id)
@@ -210,6 +213,11 @@
                     continue
                 self.__parse_ensemble(ensemble_node, content)            
     
+#        res = self.ldt_doc.xpath("/iri/displays/display")
+#        for display_node in res:
+#            pass
+        
+        self.parsed = True
     
     def __parse_content(self, content):
         
@@ -276,7 +284,8 @@
     
         res = {}
         
-        self.__parse_ldt()
+        if not self.parsed:
+            self.__parse_ldt()    
         
         project_main_media = ""
         if len(self.medias) > 0:
@@ -321,4 +330,42 @@
         res['views'] = self.views # ignored for the moment
         
         return res
+    
+    def getAnnotations(self, first_cutting=True):
         
+        if not self.parsed:
+            self.__parse_ldt()
+        
+        annotations = []
+        
+        current_cutting = None
+        uri = None
+        for annot in self.annotations:
+            if first_cutting and current_cutting and current_cuttings != annot['meta']['id-ref'] :
+                break
+            current_cuttings = annot['meta']['id-ref']
+            content_id = annot['media']
+            content = Content.objects.get(iri_id=content_id)
+            if annot['tags']:
+                tags_list = map(lambda tag_entry: self.tags_by_id[tag_entry['id-ref']]['meta']['dc:title'],annot['tags'])
+            else:
+                tags_list = []
+            begin = int(annot['begin'])
+            duration = int(annot['end'])-begin
+            if content.media_obj and content.media_obj.external_publication_url:
+                uri = "%s#t=%d" % (content.media_obj.external_publication_url, begin)
+
+        
+            annotations.append({
+                'begin': begin,
+                'duration':duration,
+                'title':annot['content']['title'],
+                'desc':annot['content']['description'],
+                'tags': tags_list,
+                'id':annot['id'],
+                'uri':uri
+            })
+            
+        return annotations
+
+