corrections on evolutions
authorymh <ymh.work@gmail.com>
Tue, 12 Oct 2010 16:11:15 +0200
changeset 83 ec31a4bd86d3
parent 82 9202807b4cec
child 84 6c3162d9e632
corrections on evolutions
web/franceculture/templates/franceculture/partial/embed_player.html
web/franceculture/templates/franceculture/partial/embed_seo_meta.html
web/franceculture/views.py
web/ldt/auth/__init__.py
web/ldt/ldt_utils/models.py
web/ldt/ldt_utils/urls.py
web/ldt/ldt_utils/views.py
--- a/web/franceculture/templates/franceculture/partial/embed_player.html	Tue Oct 12 03:23:33 2010 +0200
+++ b/web/franceculture/templates/franceculture/partial/embed_player.html	Tue Oct 12 16:11:15 2010 +0200
@@ -1,5 +1,6 @@
 {% spaceless %}
 {% load i18n %}
+<div>
 <div style="height: 80px">&nbsp;</div>
 <div id="{{ player_id }}_embed" class="iri_player_embed">
 </div>
@@ -22,4 +23,5 @@
 		};
 	__IriSP.init(config);     
 </script>
+</div>
 {% endspaceless %}
\ No newline at end of file
--- a/web/franceculture/templates/franceculture/partial/embed_seo_meta.html	Tue Oct 12 03:23:33 2010 +0200
+++ b/web/franceculture/templates/franceculture/partial/embed_seo_meta.html	Tue Oct 12 16:11:15 2010 +0200
@@ -1,6 +1,3 @@
 {% spaceless %}
-	{% for annotation in annotations %}
-		<li><span class="title">{{annotation.title}}</span><span class="desc">{{annotation.desc}}</span><span class="tags">{{annotation.tags}}</span><span class="uri">{% if annotation.uri %}<a href="{{annotation.uri}}">{{annotation.uri}}</a>{% endif %}</span></li>
-	{% endfor %}
-	</ul>
+<link rel="meta" href="{% url ldt.ldt_utils.views.project_annotations_rdf ldt_id=ldt_id %}"/>
 {% endspaceless %}
\ No newline at end of file
--- a/web/franceculture/views.py	Tue Oct 12 03:23:33 2010 +0200
+++ b/web/franceculture/views.py	Tue Oct 12 16:11:15 2010 +0200
@@ -9,7 +9,7 @@
 from django.db.models import Q
 import logging
 import lxml.etree
-from lxml.html import fromstring
+from lxml.html import fromstring, fragment_fromstring
 import ldt.auth
 
 
@@ -87,46 +87,10 @@
     if not ldt.auth.checkAccess(request.user, project):
         return HttpResponseForbidden(_("You can not access this project"))
 
-    
-    doc = lxml.etree.fromstring(project.ldt)
-    
-    annotations = []
-    
-    for contentnode in doc.xpath("/iri/annotations/content"):
-        iri_id = contentnode.get("id")
-        content = Content.objects.get(iri_id=iri_id)
-        
-        for annotationnode in contentnode.xpath("ensemble/decoupage/elements/element"):
-             
-            tags = annotationnode.get('tags')
-            tags_list = []
-            tags_list.extend(annotationnode.xpath("tags/tag/text()"))
-            if tags:
-                tags_list.append(tags)
-            tags = ",".join(tags_list)
-            
-            begin = annotationnode.get('begin')
-            if begin is None:
-                begin = 0
-            else:
-                begin = int(begin)
-
-            uri = None            
-            if 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,
-                'id':u"".join(annotationnode.get('id')),
-                'uri':uri
-            })
+    annotations = project.getAnnotations()
     
     embed_rendered = dict((typestr,
-                           (lambda s:escape(lxml.etree.tostring(fromstring(render_to_string("franceculture/partial/embed_%s.html"%(s), {'json_url':json_url,'player_id':player_id, 'annotations':annotations}, context_instance=RequestContext(request))),pretty_print=True)))(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)) 
                            for typestr in ('player','seo_body', 'seo_meta', 'links')  )
     
     return render_to_response("franceculture/embed_popup.html",
--- a/web/ldt/auth/__init__.py	Tue Oct 12 03:23:33 2010 +0200
+++ b/web/ldt/auth/__init__.py	Tue Oct 12 16:11:15 2010 +0200
@@ -4,4 +4,4 @@
     if check_meth:
         return check_meth(user)
     else:
-        return False
\ No newline at end of file
+        return user.is_staff
\ No newline at end of file
--- a/web/ldt/ldt_utils/models.py	Tue Oct 12 03:23:33 2010 +0200
+++ b/web/ldt/ldt_utils/models.py	Tue Oct 12 16:11:15 2010 +0200
@@ -297,6 +297,57 @@
             return True
         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.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/urls.py	Tue Oct 12 03:23:33 2010 +0200
+++ b/web/ldt/ldt_utils/urls.py	Tue Oct 12 16:11:15 2010 +0200
@@ -21,6 +21,7 @@
     url(r'^update/(?P<ldt_id>.*)$', 'views.update_project'),
     url(r'^cljson/id/(?P<id>.*)$', 'views.project_json_id'),
     url(r'^cljson/externalid/(?P<id>.*)$', 'views.project_json_externalid'),
+    url(r'^rdf/id/(?P<ldt_id>.*)$', 'views.project_annotations_rdf'),
 )
 
 urlpatterns += patterns('',
--- a/web/ldt/ldt_utils/views.py	Tue Oct 12 03:23:33 2010 +0200
+++ b/web/ldt/ldt_utils/views.py	Tue Oct 12 16:11:15 2010 +0200
@@ -17,11 +17,13 @@
 from forms import LdtImportForm, LdtAddForm, SearchForm, AddProjectForm, \
     CopyProjectForm, ContentForm, MediaForm
 from ldt.core.models import Owner
+from lxml import etree
 from models import *
 from projectserializer import *
 from string import Template
 from urllib2 import urlparse
 from utils import *
+import StringIO
 import base64
 import cgi
 import django.core.urlresolvers
@@ -269,6 +271,56 @@
 
     return resp
 
+def project_annotations_rdf(request, ldt_id):
+
+    project = Project.objects.get(ldt_id=ldt_id);
+    
+    if not ldt_auth.checkAccess(request.user, project):
+        return HttpResponseForbidden(_("You can not access this project"))
+
+    mimetype = request.REQUEST.get("mimetype")
+    if mimetype is None:
+        mimetype = "application/rdf+xml; charset=utf-8"
+    else:
+        mimetype = mimetype.encode("utf-8")
+    if "charset" not in mimetype:
+        mimetype += "; charset=utf-8" 
+    resp = HttpResponse(mimetype=mimetype)
+    resp['Cache-Control']='no-cache, must-revalidate'
+    resp['Pragma']='no-cache'
+
+    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
+    dc = u"{%s}" % dc_ns
+    nsmap = {u'rdf' : rdf_ns, u'dc':dc_ns}
+    
+    rdf_root = etree.Element(rdf+u"RDF", nsmap=nsmap)
+    
+    logging.debug("RDF annotations : " + repr(annotations))
+    
+    for annotation in annotations:
+        uri = u""
+        if 'uri' in annotation and annotation['uri']:
+            uri = unicode(annotation['uri'])
+        annot_desc = etree.SubElement(rdf_root, rdf+u"Description")
+        annot_desc.set(rdf+u'about',uri)
+        if annotation['title']:
+            etree.SubElement(annot_desc, dc+'title').text = etree.CDATA(unicode(annotation['title']))
+        if annotation['desc']:
+            etree.SubElement(annot_desc, dc+'description').text = etree.CDATA(unicode(annotation['desc']))
+        if annotation['tags']:
+            for tag in annotation['tags']:
+                etree.SubElement(annot_desc, dc+'subject').text = etree.CDATA(unicode(tag))
+        etree.SubElement(annot_desc,dc+'coverage').text = u"start=%s, duration=%s" % (annotation['begin'], annotation['duration'])
+        
+    resp.write(u"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
+    resp.write(u"<!DOCTYPE rdf:RDF PUBLIC \"-//DUBLIN CORE//DCMES DTD 2002/07/31//EN\" \"http://dublincore.org/documents/2002/07/31/dcmes-xml/dcmes-xml-dtd.dtd\">\n")
+    
+    resp.write(etree.tostring(rdf_root, xml_declaration=False, encoding="utf-8", pretty_print=True))
+
+    return resp
 
 def save_ldtProject(request):
     if request.method=="POST":
@@ -628,4 +680,3 @@
     if iri_id:
         Content.objects.filter(iri_id=iri_id).delete()
     
-    
\ No newline at end of file