web/ldt/ldt_utils/views.py
changeset 83 ec31a4bd86d3
parent 82 9202807b4cec
child 84 6c3162d9e632
--- 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