# HG changeset patch # User wakimd # Date 1290016654 -3600 # Node ID d0f617472760509327c630dbd6275688aaa1d5a1 # Parent 37e051f2264d22df928b57afbfb4c78a57126ae1 Added tests on server + some corrections on views diff -r 37e051f2264d -r d0f617472760 web/ldt/text/tests.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('f2c1d1fa-629d-4520-a3d2-955b4f2582c0http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l\'annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459') + self.content = str('f2c1d1fa-629d-4520-a3d2-955b4f2582c0http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l\'annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459') 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('d2c1d1fa-629d-4520-a3d2-955b4f2582c0http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l\'annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459') -# response = self.c.post('/api/'+ VERSION_STR +'/text/create/', {'content':content}) -# self.assertEqual(response.status_code, 409) + def test_error_create(self): + content = 'd2c1d1fa-629d-4520-a3d2-955b4f2582c0http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l\'annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459' + 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('tag1tag2newtag3#DDDDDDoaubert80cd0532-1dda-4130-b351-6a181130a7c92010-11-06 12:33:53.420459') - response = self.c.post('/api/'+ VERSION_STR +'/text/update/', {'content':content,'id':'d2c1d1fa-629d-4520-a3d2-955b4f2582c0'}) + content = 'tag1tag2newtag3#DDDDDDoaubert80cd0532-1dda-4130-b351-6a181130a7c92010-11-06 12:33:53.420459' + 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('d2c1d1fa-629d-4520-a3d2-955b4f2582c0http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l\'annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459') + content = 'd2c1d1fa-629d-4520-a3d2-955b4f2582c0http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l\'annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459' 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':'mypersonnalidhttp://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#AAAAAA<![CDATA[titre de l\'annotation]]>oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459'}) + self.content2 = urllib.urlencode({'content':'mypersonnalid2http://www.leezam.com/pub/epub/123456!/OPS/chapter2.xhtml#pos=56,168tag1tag2#BBBBBB<![CDATA[titre de l\'annotation2]]>wakimd79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.417550oaubert79cd0532-1dda-4130-b351-6a181130a7c92010-09-06 12:33:53.420459'}) + 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':'tag1tag2newtag3#DDDDDDoaubert80cd0532-1dda-4130-b351-6a181130a7c92010-11-06 12:33:53.420459','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 diff -r 37e051f2264d -r d0f617472760 web/ldt/text/urls.py --- 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.*)$', 'views.create_annotation'), + url(r'^create/$', 'views.create_annotation'), url(r'^filter/(?P.*)/(?P.*)/(?P.*)/(?P.*)$', 'views.filter_annotation'), url(r'^filter/(?P.*)/(?P.*)/(?P.*)$', 'views.filter_annotation'), url(r'^filter/(?P.*)/(?P.*)/(?P.*)$', 'views.filter_annotation'), @@ -15,6 +15,6 @@ url(r'^filter/(?P.*)/(?P.*)$', 'views.filter_annotation'), url(r'^filter/(?P.*)$', 'views.filter_annotation'), url(r'^get/(?P.*)$', 'views.get_annotation'), - url(r'^delete/(?P.*)$', 'views.delete_annotation'), - url(r'^update/(?P.*)/(?P.*)$', 'views.update_annotation'), + url(r'^delete/$', 'views.delete_annotation'), + url(r'^update/$', 'views.update_annotation'), ) \ No newline at end of file diff -r 37e051f2264d -r d0f617472760 web/ldt/text/views.py --- 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") + +