Added tests on server + some corrections on views
authorwakimd
Wed, 17 Nov 2010 18:57:34 +0100
changeset 16 d0f617472760
parent 15 37e051f2264d
child 17 683ce4109c28
Added tests on server + some corrections on views
web/ldt/text/tests.py
web/ldt/text/urls.py
web/ldt/text/views.py
--- a/web/ldt/text/tests.py	Wed Nov 17 10:28:55 2010 +0100
+++ b/web/ldt/text/tests.py	Wed Nov 17 18:57:34 2010 +0100
@@ -8,7 +8,7 @@
 from ldt.text.models import *
 from ldt.core.models import Owner
 from views import *
-import base64
+import urllib
 import uuid
 import tempfile
 import datetime
@@ -23,7 +23,7 @@
 # 2. the returned xml contains correct data
 class CreateTest(unittest.TestCase):
     def setUp(self):
-        self.content = base64.urlsafe_b64encode('<iri><text-annotation><id>f2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>')
+        self.content = str('<iri><text-annotation><id>f2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>')
         self.c = Client()
         self.annot = Annotation(external_id=u'd2c1d1fa-629d-4520-a3d2-955b4f2582c0', uri=u'http://iri.blabla', tags=[u'tag1',u'tag2'], title=u'montitre', description=u'madesc', text=u'letexteselectionne', color=u'#AAAAAA', creator=u'wakimd', contributor=u'wakimd', creation_date=u'2010-09-06 12:33:53.417550', update_date=u'2010-09-06 12:33:53.417550')
         self.annot.save()
@@ -34,19 +34,21 @@
 
     def test_create_annotation(self):
         response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':self.content})
+        #self.assertEqual(response.content, "  ")
         self.annot1 = lxml.etree.fromstring(response.content)
         self.assertEqual(self.annot1.xpath("/iri/text-annotation/id/text()")[0],"f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content")[0].tag,"content")
         self.assertEqual(self.annot1.xpath("/iri/text-annotation/tags/tag/text()")[0],"tag1")
         self.assertEqual(self.annot1.xpath("/iri/text-annotation/content/text/text()")[0],u"texte selectionne lors de la creation de l\'annotation")
         self.assertEqual(self.annot1.xpath("/iri/text-annotation/meta/created/text()")[0],"2010-09-06 12:33:53.417550")
-        annot = Annotation.objects.get(external_id="f2c1d1fa-629d-4520-a3d2-955b4f2582c0")
-        self.assertEqual(annot.uri, "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168")
+        response2 = self.c.get('/api/'+ VERSION_STR +'/text/get/', {'id':'f2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
+        annot2 = lxml.etree.fromstring(response.content)
+        self.assertEqual(annot2.xpath("/iri/text-annotation/uri/text()")[0], "http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168")
         
-#    def test_error_create(self):
-#        content = base64.urlsafe_b64encode('<iri><text-annotation><id>d2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>')
-#        response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':content})
-#        self.assertEqual(response.status_code, 409)
+    def test_error_create(self):
+        content = '<iri><text-annotation><id>d2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'
+        response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':content})
+        self.assertEqual(response.status_code, 409)
 
 
 # This test creates an annotation, then gets it, and checks that the returned xml contains correct data
@@ -136,6 +138,8 @@
             annot.delete()
     
     def test_delete_annotation(self):
+        id = urllib.urlencode({'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
+        f = urllib.urlopen("http://127.0.0.1:8000/api/1.0/text/delete/", id)
         response = self.c.post('/api/'+ VERSION_STR +'/text/delete/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
         response2 = self.c.get('/ldt/get/', {'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
         doc = lxml.etree.fromstring(response.content)
@@ -162,17 +166,45 @@
             annot.delete()
             
     def test_update_annotation(self):
-        content = base64.urlsafe_b64encode('<iri><text-annotation><id></id><uri></uri><tags><tag>tag1</tag><tag>tag2new</tag><tag>tag3</tag></tags><content><color>#DDDDDD</color><description><![CDATA[texte de description update]]></description><title></title><text><![CDATA[texte selectionne a nouveau lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>80cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created></created><creator></creator><creator-id></creator-id><modified>2010-11-06 12:33:53.420459</modified></meta></text-annotation></iri>')
-        response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})
+        content = '<iri><text-annotation><id></id><uri></uri><tags><tag>tag1</tag><tag>tag2new</tag><tag>tag3</tag></tags><content><color>#DDDDDD</color><description><![CDATA[texte de description update]]></description><title></title><text><![CDATA[texte selectionne a nouveau lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>80cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created></created><creator></creator><creator-id></creator-id><modified>2010-11-06 12:33:53.420459</modified></meta></text-annotation></iri>'
+        response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'})        
         doc = lxml.etree.fromstring(response.content)
-        self.assertEqual(lxml.etree.tostring(doc), " ")
-        self.assertEqual(doc.xpath("/iri/text-annotation/id/text()"),"d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
+        #self.assertEqual(lxml.etree.tostring(doc), " ")
+        self.assertEqual(doc.xpath("/iri/text-annotation/id/text()")[0],"d2c1d1fa-629d-4520-a3d2-955b4f2582c0")
         self.assertEqual(doc.xpath("/iri/text-annotation/tags/tag/text()")[1], "mytag")
         self.assertEqual(doc.xpath("/iri/text-annotation/content/color/text()")[0],"#DDDDDD")
         
     def test_error_update(self):
-        content = base64.urlsafe_b64encode('<iri><text-annotation><id>d2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>')
+        content = '<iri><text-annotation><id>d2c1d1fa-629d-4520-a3d2-955b4f2582c0</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'
         response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'1'})
         self.assertEqual(response.status_code,404)
                
 
+class OnServerGlobalTest(unittest.TestCase):
+    def setUp(self):
+        self.content = urllib.urlencode({'content':'<iri><text-annotation><id>mypersonnalid</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#AAAAAA</color><description><![CDATA[texte de description]]></description><title><![CDATA[titre de l\'annotation]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'})
+        self.content2 = urllib.urlencode({'content':'<iri><text-annotation><id>mypersonnalid2</id><uri>http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168</uri><tags><tag>tag1</tag><tag>tag2</tag></tags><content><color>#BBBBBB</color><description><![CDATA[texte de description2]]></description><title><![CDATA[titre de l\'annotation2]]></title><text><![CDATA[texte selectionne lors de la creation de l\'annotation2]]></text></content><meta><contributor>wakimd</contributor><contributor-id>79cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created>2010-09-06 12:33:53.417550</created><creator>oaubert</creator><creator-id>79cd0532-1dda-4130-b351-6a181130a7c9</creator-id><modified>2010-09-06 12:33:53.420459</modified></meta></text-annotation></iri>'})
+        self.id = urllib.urlencode({"id":"mypersonnalid"})
+        self.id2 = urllib.urlencode({"id":"mypersonnalid2"})
+        self.uri = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168"})
+        self.filt1 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168", "creator":"","limit":"","filter":""})
+        self.filt2 = urllib.urlencode({"uri":"http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168","creator":"wakimd","limit":"","filter":""})
+        self.up = urllib.urlencode({'content':'<iri><text-annotation><id></id><uri></uri><tags><tag>tag1</tag><tag>tag2new</tag><tag>tag3</tag></tags><content><color>#DDDDDD</color><description><![CDATA[texte de description update]]></description><title></title><text><![CDATA[texte selectionne a nouveau lors de la creation de l\'annotation]]></text></content><meta><contributor>oaubert</contributor><contributor-id>80cd0532-1dda-4130-b351-6a181130a7c9</contributor-id><created></created><creator></creator><creator-id></creator-id><modified>2010-11-06 12:33:53.420459</modified></meta></text-annotation></iri>','id':'mypersonnalid'})
+    
+    def test_everything(self):
+        creation = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content)
+        creation2 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/create/", self.content2)
+        
+        get = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/get/?%s" % self.id)
+        
+        update = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/update/", self.up)
+        
+        filt1 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/filter/?%s", self.uri)
+        filt2 = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/filter/?uri=http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168?creator=wakimd")
+        tmp = open('debug.html','r+')
+        tmp.write(filt2.read())
+        
+        delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id)
+        delete = urllib.urlopen("http://127.0.0.1:8000/api/"+VERSION_STR+"/text/delete/", self.id2)
+        
+        
\ No newline at end of file
--- a/web/ldt/text/urls.py	Wed Nov 17 10:28:55 2010 +0100
+++ b/web/ldt/text/urls.py	Wed Nov 17 18:57:34 2010 +0100
@@ -6,7 +6,7 @@
 # admin.autodiscover()
 
 urlpatterns = patterns('ldt.text',
-    url(r'^create/(?P<content>.*)$', 'views.create_annotation'),
+    url(r'^create/$', 'views.create_annotation'),
     url(r'^filter/(?P<uri>.*)/(?P<filter>.*)/(?P<limit>.*)/(?P<creator>.*)$', 'views.filter_annotation'),
     url(r'^filter/(?P<uri>.*)/(?P<filter>.*)/(?P<limit>.*)$', 'views.filter_annotation'),
     url(r'^filter/(?P<uri>.*)/(?P<filter>.*)/(?P<creator>.*)$', 'views.filter_annotation'),
@@ -15,6 +15,6 @@
     url(r'^filter/(?P<uri>.*)/(?P<creator>.*)$', 'views.filter_annotation'),
     url(r'^filter/(?P<uri>.*)$', 'views.filter_annotation'),
     url(r'^get/(?P<id>.*)$', 'views.get_annotation'),
-    url(r'^delete/(?P<id>.*)$', 'views.delete_annotation'),
-    url(r'^update/(?P<content>.*)/(?P<id>.*)$', 'views.update_annotation'),
+    url(r'^delete/$', 'views.delete_annotation'),
+    url(r'^update/$', 'views.update_annotation'),
 )
\ No newline at end of file
--- a/web/ldt/text/views.py	Wed Nov 17 10:28:55 2010 +0100
+++ b/web/ldt/text/views.py	Wed Nov 17 18:57:34 2010 +0100
@@ -23,7 +23,6 @@
 from urllib2 import urlparse
 from utils import *
 import StringIO
-import base64
 import cgi
 import django.core.urlresolvers
 import ldt.auth as ldt_auth
@@ -32,6 +31,7 @@
 import lucene
 import tempfile
 import uuid
+from urllib import urlopen
 
 
 ## Filters the annotation depending on the request parameters
@@ -101,9 +101,8 @@
 ## Returns an xml-structured annotation
 #@login_required
 @csrf_exempt
-def create_annotation(request, content):
-    #cont = base64.urlsafe_b64decode(str(request.POST["content"]))
-    cont = str(request.POST["content"])
+def create_annotation(request):
+    cont = request.POST["content"]
     doc = lxml.etree.fromstring(cont)
     
     id = unicode(doc.xpath("/iri/text-annotation/id/text()")[0])
@@ -130,12 +129,9 @@
         annotation = Annotation.create_annotation(external_id=id, uri=uri, tags=tags, title=title, description=desc, text=text, color=color, creator=creator, contributor=contributor, creation_date=creation_date, update_date=update_date)
         annotation.save()
         return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
-        #return doc
     except IntegrityError:
-    #except Annotation.IntegrityError:
-        #print 'This id is already used! Please choose another one!'
         return HttpResponse(status=409)
-    
+
     
     
 ## Gets an annotation (from its id)
@@ -143,8 +139,7 @@
 def get_annotation(request, id):
     try:
         annot = Annotation.objects.get(external_id=request.GET.get('id',''))
-    except:
-    #except Annotation.DoesNotExist:
+    except Annotation.DoesNotExist:
         raise Http404
     iri = lxml.etree.Element('iri')
     doc = lxml.etree.ElementTree(iri)
@@ -184,7 +179,6 @@
     updatedate = lxml.etree.SubElement(meta, "modified")
     updatedate.text = str(annot.update_date)
 
-    #return doc
     return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
 
 
@@ -192,16 +186,14 @@
 ## Returns an empty xml-structured annotation
 #@login_required
 @csrf_exempt
-def delete_annotation(request, id):
+def delete_annotation(request):
     try:
         annot = Annotation.objects.get(external_id=request.POST["id"])
         annot.delete()
-    #except Annotation.DoesNotExist:
-    except:
+    except Annotation.DoesNotExist:
         raise Http404
     
     doc=create_empty_annotation()
-    #return doc
     return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
 
 
@@ -209,14 +201,14 @@
 ## Returns the xml-structured updated annotation
 #@login_required
 @csrf_exempt
-def update_annotation(request, content, id):
+def update_annotation(request):
     try:
         annot = Annotation.objects.get(external_id=request.POST["id"])
-    #except Annotation.DoesNotExist:
-    except:
+    except Annotation.DoesNotExist:
+    #except:
         raise Http404
     
-    cont = base64.urlsafe_b64decode(str(request.POST["content"]))
+    cont = request.POST["content"]
     doc = lxml.etree.fromstring(cont)
     
     uri = doc.xpath("/iri/text-annotation/uri/text()")
@@ -254,6 +246,46 @@
         annot.update_date = unicode(update_date[0])
 
     annot.save()
+
+    #create xml
+    iri = lxml.etree.Element('iri')
+    doc2 = lxml.etree.ElementTree(iri)
     
-    return get_annotation(id)
+    textannotation = lxml.etree.SubElement(iri, 'text-annotation')
+    id = lxml.etree.SubElement(textannotation,'id')
+    id.text = annot.external_id
+    uri = lxml.etree.SubElement(textannotation,'uri')
+    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:
+            tag = lxml.etree.SubElement(tags, 'tag')
+            tag.text = t
     
+    content = lxml.etree.SubElement(textannotation,'content')
+    color = lxml.etree.SubElement(content,'color')
+    color.text = annot.color
+    description = lxml.etree.SubElement(content,'description')
+    description.text = annot.description
+    title = lxml.etree.SubElement(content,'title')
+    title.text = annot.title
+    text = lxml.etree.SubElement(content,'text')
+    text.text = annot.text
+    
+    meta = lxml.etree.SubElement(textannotation,'meta')
+    contributor = lxml.etree.SubElement(meta, "contributor")
+    contributor.text = annot.contributor
+    creator = lxml.etree.SubElement(meta, "creator")
+    creator.text = annot.creator
+    creationdate = lxml.etree.SubElement(meta, "created")
+    creationdate.text = str(annot.creation_date)
+    updatedate = lxml.etree.SubElement(meta, "modified")
+    updatedate.text = str(annot.update_date)
+
+    return HttpResponse(lxml.etree.tostring(doc2, pretty_print=True), mimetype="text/xml;charset=utf-8")
+
+