correct project serializer
authorymh <ymh.work@gmail.com>
Thu, 14 Oct 2010 11:36:12 +0200
changeset 88 7f2c2d9adf58
parent 87 b105636ae38d
child 89 30c6e597a7de
child 94 9927a619d2b5
correct project serializer
web/franceculture/templates/franceculture/partial/embed_links.html
web/franceculture/views.py
web/ldt/ldt_utils/models.py
web/ldt/ldt_utils/projectserializer.py
web/ldt/ldt_utils/views.py
--- a/web/franceculture/templates/franceculture/partial/embed_links.html	Tue Oct 12 19:12:27 2010 +0200
+++ b/web/franceculture/templates/franceculture/partial/embed_links.html	Thu Oct 14 11:36:12 2010 +0200
@@ -2,7 +2,7 @@
 <div id="{{ player_id }}_link_list">
 	<ul>
 	    {% for annotation in annotations %}
-	        <li><span class="title">{{annotation.title}}</span>:&nbsp;<span class="uri">{% if annotation.uri %}<a href="{{annotation.uri}}">{{annotation.uri}}</a>{% endif %}</span></li>
+	        <li><span class="title">{% firstof annotation.title annotation.desc %}</span>:&nbsp;<span class="uri">{% if annotation.uri %}<a href="{{annotation.uri}}">{{annotation.uri}}</a>{% endif %}</span></li>
 	    {% endfor %}
 	</ul>
 </div>
--- a/web/franceculture/views.py	Tue Oct 12 19:12:27 2010 +0200
+++ b/web/franceculture/views.py	Thu Oct 14 11:36:12 2010 +0200
@@ -1,16 +1,17 @@
+from django.contrib.auth.decorators import login_required
+from django.db.models import Q
+from django.http import HttpResponseServerError, HttpResponseForbidden
 from django.shortcuts import render_to_response
-from django.contrib.auth.decorators import login_required
 from django.template import RequestContext
-from django.utils.html import escape
 from django.template.loader import render_to_string
+from django.utils.html import escape
 from ldt.ldt_utils.models import Content, Project, Owner
+from ldt.ldt_utils.projectserializer import ProjectSerializer
 from ldt.ldt_utils.utils import boolean_convert
-from django.http import HttpResponseServerError, HttpResponseForbidden
-from django.db.models import Q
+from lxml.html import fromstring, fragment_fromstring
+import ldt.auth
 import logging
 import lxml.etree
-from lxml.html import fromstring, fragment_fromstring
-import ldt.auth
 
 
 @login_required
@@ -87,7 +88,8 @@
     if not ldt.auth.checkAccess(request.user, project):
         return HttpResponseForbidden(_("You can not access this project"))
 
-    annotations = project.getAnnotations()
+    ps = ProjectSerializer(project, from_contents=False, from_display=True)
+    annotations = ps.getAnnotations(first_cutting=True)
     
     embed_rendered = dict((typestr,
                            (lambda s:escape(lxml.etree.tostring(fragment_fromstring(render_to_string("franceculture/partial/embed_%s.html"%(s), {'json_url':json_url,'player_id':player_id, 'annotations':annotations, 'ldt_id': ldt_id}, context_instance=RequestContext(request))),pretty_print=True)))(typestr)) 
--- a/web/ldt/ldt_utils/models.py	Tue Oct 12 19:12:27 2010 +0200
+++ b/web/ldt/ldt_utils/models.py	Thu Oct 14 11:36:12 2010 +0200
@@ -298,56 +298,6 @@
         else:
             return False
         
-    def getAnnotations(self, first_cutting=True):
-        doc = lxml.etree.fromstring(self.ldt)
-    
-        annotations = []
-        
-        for contentnode in doc.xpath("/iri/annotations/content"):
-            iri_id = contentnode.get("id")
-            content = Content.objects.get(iri_id=iri_id)
-            
-            for cuttingnode in contentnode.xpath("ensemble/decoupage"):
-                
-                for annotationnode in cuttingnode.xpath("elements/element"):
-                    tags = annotationnode.get('tags')
-                    tags_list = []
-                    tags_list.extend(annotationnode.xpath("tags/tag/text()"))
-                    if tags:
-                        tags_list.append(tags)
-                    
-                    def accumulate(a, b):
-                        a.extend(b)
-                        return a
-                    
-                    tags_list = reduce(lambda a, s:accumulate(a,s.split(',')), tags_list, [])
-                    
-                    begin = annotationnode.get('begin')
-                    if begin is None:
-                        begin = 0
-                    else:
-                        begin = int(begin)
-        
-                    uri = None            
-                    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':annotationnode.get('dur'),
-                        'title':u"".join(annotationnode.xpath("title/text()")),
-                        'desc':u"".join(annotationnode.xpath("abstract/text()")),
-                        'tags': tags_list,
-                        'id':u"".join(annotationnode.get('id')),
-                        'uri':uri
-                    })
-                if first_cutting and len(annotations) > 0 :
-                    break
-            if first_cutting and len(annotations) > 0 :
-                break
-            
-        return annotations
-
 
 class Segment(models.Model):
     
--- 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
+
+        
--- a/web/ldt/ldt_utils/views.py	Tue Oct 12 19:12:27 2010 +0200
+++ b/web/ldt/ldt_utils/views.py	Thu Oct 14 11:36:12 2010 +0200
@@ -290,7 +290,9 @@
     resp['Cache-Control']='no-cache, must-revalidate'
     resp['Pragma']='no-cache'
 
+    ps = ProjectSerializer(project, from_content=False, from_display=True) 
     annotations = project.getAnnotations(first_cutting=True)
+    
     rdf_ns = u"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     dc_ns = u"http://purl.org/dc/elements/1.1/"
     rdf = u"{%s}" % rdf_ns