web/ldt/ldt_utils/views.py
changeset 2 59311c28454f
parent 1 3a30d255c235
child 9 22ab430e9b64
--- a/web/ldt/ldt_utils/views.py	Sun Nov 14 20:25:22 2010 +0100
+++ b/web/ldt/ldt_utils/views.py	Mon Nov 15 18:56:22 2010 +0100
@@ -5,7 +5,7 @@
 from django.db import IntegrityError
 from django.db.models import Q
 from django.forms.util import ErrorList
-from django.http import HttpResponse, HttpResponseRedirect, \
+from django.http import HttpResponse, Http404, HttpResponseRedirect, \
     HttpResponseForbidden, HttpResponseServerError, HttpResponseBadRequest
 from django.shortcuts import render_to_response, get_object_or_404, \
     get_list_or_404
@@ -14,6 +14,7 @@
 from django.utils import simplejson
 from django.utils.html import escape
 from django.utils.translation import ugettext as _, ungettext
+from httplib import CONFLICT
 from ldt.core.models import Owner
 from ldt.ldt_utils.utils import boolean_convert
 from lxml import etree
@@ -38,19 +39,21 @@
 ## Returns an xml containing the resulting annotations
 def filter_annotation(request, uri=None, filter=None, limit=None, creator=None):
     annotlist = None
-    query = Q()
+    query = Q()    
     
-    if uri:
-        query &= Q(uri=uri)
-    if creator:
-        query &= Q(creator=creator)
-    if filter and len(filter) > 0:
-        query &= Q(text__icontains=filter)
+    if request.GET.get('uri'):
+        query &= Q(uri=request.GET.get('uri'))
+    if request.GET.get('creator'):
+        query &= Q(creator=request.GET.get('creator'))
+    if request.GET.get('filter') and len(request.GET.get('filter')) > 0:
+        query &= Q(text__icontains=request.GET.get('filter'))
 
     annotlist = Annotation.objects.filter(query)
-
-    if limit:
-        annotlist = annotlist[:limit]
+    
+    if request.GET.get('limit'):
+        nb = request.GET.get('limit')
+        #offset = request.GET.get('limit')[1]
+        annotlist = annotlist[:nb]
 
     #create xml
     iri = lxml.etree.Element('iri')
@@ -64,6 +67,8 @@
         uri.text = annot.uri
         
         if annot.tags:
+            if type(annot.tags) is unicode:
+                annot.tags = eval(annot.tags)
             tags = lxml.etree.SubElement(textannotation,'tags')
             ltags = normalize_tags(annot.tags)
             for t in ltags:
@@ -97,7 +102,7 @@
 ## Returns an xml-structured annotation
 #@login_required
 def create_annotation(request, content):
-    cont = base64.urlsafe_b64decode(content)
+    cont = base64.urlsafe_b64decode(request.POST["content"])
     doc = lxml.etree.fromstring(cont)
     
     id = unicode(doc.xpath("/iri/text-annotation/id/text()")[0])
@@ -126,18 +131,20 @@
         return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
         #return doc
     except:
-        #raise IntegrityError
+    #except Annotation.IntegrityError:
         #print 'This id is already used! Please choose another one!'
-        raise HttpResponseBadRequest('This id is already used! Please chose another one!')
+        raise CONFLICT
+    
     
     
-    #return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
-    
-
-## Gets an annotation from an id
+## Gets an annotation (from its id)
 ## Returns the xml-structured annotation
 def get_annotation(request, id):
-    annot = Annotation.objects.get_object_or_404(id=id)
+    try:
+        annot = Annotation.objects.get(id=request.GET.get('id',''))
+    except:
+    #except Annotation.DoesNotExist:
+        raise Http404
     iri = lxml.etree.Element('iri')
     doc = lxml.etree.ElementTree(iri)
     
@@ -180,12 +187,16 @@
     return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
 
 
-## Delete an annotation from an id
+## Deletes an annotation (from its id)
 ## Returns an empty xml-structured annotation
 #@login_required
 def delete_annotation(request, id):
-    annot = Annotation.objects.get_object_or_404(id=id)
-    annot.delete()
+    try:
+        annot = Annotation.objects.get(id=request.POST["id"])
+        annot.delete()
+    #except Annotation.DoesNotExist:
+    except:
+        raise Http404
     
     doc=create_empty_annotation()
     #return doc
@@ -196,9 +207,13 @@
 ## Returns the xml-structured updated annotation
 #@login_required
 def update_annotation(request, content, id):
-    annot = Annotation.objects.get_object_or_404(id=id)
+    try:
+        annot = Annotation.objects.get(id=request.POST["id"])
+    #except Annotation.DoesNotExist:
+    except:
+        raise Http404
     
-    cont = base64.urlsafe_b64decode(content)
+    cont = base64.urlsafe_b64decode(request.POST["content"])
     doc = lxml.etree.fromstring(cont)
     
     uri = doc.xpath("/iri/text-annotation/uri/text()")