21 from lxml.html import fromstring, fragment_fromstring |
21 from lxml.html import fromstring, fragment_fromstring |
22 from string import Template |
22 from string import Template |
23 from urllib2 import urlparse |
23 from urllib2 import urlparse |
24 from utils import * |
24 from utils import * |
25 import StringIO |
25 import StringIO |
26 import base64 |
|
27 import cgi |
26 import cgi |
28 import django.core.urlresolvers |
27 import django.core.urlresolvers |
29 import ldt.auth as ldt_auth |
28 import ldt.auth as ldt_auth |
30 import ldt.utils.path as ldt_utils_path |
29 import ldt.utils.path as ldt_utils_path |
31 import logging |
30 import logging |
32 import lucene |
31 import lucene |
33 import tempfile |
32 import tempfile |
34 import uuid |
33 import uuid |
|
34 from urllib import urlopen |
35 |
35 |
36 |
36 |
37 ## Filters the annotation depending on the request parameters |
37 ## Filters the annotation depending on the request parameters |
38 ## Returns an xml containing the resulting annotations |
38 ## Returns an xml containing the resulting annotations |
39 def filter_annotation(request, uri=None, filter=None, limit=None, creator=None): |
39 def filter_annotation(request, uri=None, filter=None, limit=None, creator=None): |
99 |
99 |
100 ## Creates an annotation from a urlencoded xml content |
100 ## Creates an annotation from a urlencoded xml content |
101 ## Returns an xml-structured annotation |
101 ## Returns an xml-structured annotation |
102 #@login_required |
102 #@login_required |
103 @csrf_exempt |
103 @csrf_exempt |
104 def create_annotation(request, content): |
104 def create_annotation(request): |
105 #cont = base64.urlsafe_b64decode(str(request.POST["content"])) |
105 cont = request.POST["content"] |
106 cont = str(request.POST["content"]) |
|
107 doc = lxml.etree.fromstring(cont) |
106 doc = lxml.etree.fromstring(cont) |
108 |
107 |
109 id = unicode(doc.xpath("/iri/text-annotation/id/text()")[0]) |
108 id = unicode(doc.xpath("/iri/text-annotation/id/text()")[0]) |
110 if id is None: |
109 if id is None: |
111 id = generate_uuid() |
110 id = generate_uuid() |
128 |
127 |
129 try: |
128 try: |
130 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) |
129 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) |
131 annotation.save() |
130 annotation.save() |
132 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
131 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
133 #return doc |
|
134 except IntegrityError: |
132 except IntegrityError: |
135 #except Annotation.IntegrityError: |
|
136 #print 'This id is already used! Please choose another one!' |
|
137 return HttpResponse(status=409) |
133 return HttpResponse(status=409) |
138 |
134 |
139 |
135 |
140 |
136 |
141 ## Gets an annotation (from its id) |
137 ## Gets an annotation (from its id) |
142 ## Returns the xml-structured annotation |
138 ## Returns the xml-structured annotation |
143 def get_annotation(request, id): |
139 def get_annotation(request, id): |
144 try: |
140 try: |
145 annot = Annotation.objects.get(external_id=request.GET.get('id','')) |
141 annot = Annotation.objects.get(external_id=request.GET.get('id','')) |
146 except: |
142 except Annotation.DoesNotExist: |
147 #except Annotation.DoesNotExist: |
|
148 raise Http404 |
143 raise Http404 |
149 iri = lxml.etree.Element('iri') |
144 iri = lxml.etree.Element('iri') |
150 doc = lxml.etree.ElementTree(iri) |
145 doc = lxml.etree.ElementTree(iri) |
151 |
146 |
152 textannotation = lxml.etree.SubElement(iri, 'text-annotation') |
147 textannotation = lxml.etree.SubElement(iri, 'text-annotation') |
182 creationdate = lxml.etree.SubElement(meta, "created") |
177 creationdate = lxml.etree.SubElement(meta, "created") |
183 creationdate.text = str(annot.creation_date) |
178 creationdate.text = str(annot.creation_date) |
184 updatedate = lxml.etree.SubElement(meta, "modified") |
179 updatedate = lxml.etree.SubElement(meta, "modified") |
185 updatedate.text = str(annot.update_date) |
180 updatedate.text = str(annot.update_date) |
186 |
181 |
187 #return doc |
|
188 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
182 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
189 |
183 |
190 |
184 |
191 ## Deletes an annotation (from its id) |
185 ## Deletes an annotation (from its id) |
192 ## Returns an empty xml-structured annotation |
186 ## Returns an empty xml-structured annotation |
193 #@login_required |
187 #@login_required |
194 @csrf_exempt |
188 @csrf_exempt |
195 def delete_annotation(request, id): |
189 def delete_annotation(request): |
196 try: |
190 try: |
197 annot = Annotation.objects.get(external_id=request.POST["id"]) |
191 annot = Annotation.objects.get(external_id=request.POST["id"]) |
198 annot.delete() |
192 annot.delete() |
199 #except Annotation.DoesNotExist: |
193 except Annotation.DoesNotExist: |
200 except: |
|
201 raise Http404 |
194 raise Http404 |
202 |
195 |
203 doc=create_empty_annotation() |
196 doc=create_empty_annotation() |
204 #return doc |
|
205 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
197 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
206 |
198 |
207 |
199 |
208 ## Updates the content of an annotation |
200 ## Updates the content of an annotation |
209 ## Returns the xml-structured updated annotation |
201 ## Returns the xml-structured updated annotation |
210 #@login_required |
202 #@login_required |
211 @csrf_exempt |
203 @csrf_exempt |
212 def update_annotation(request, content, id): |
204 def update_annotation(request): |
213 try: |
205 try: |
214 annot = Annotation.objects.get(external_id=request.POST["id"]) |
206 annot = Annotation.objects.get(external_id=request.POST["id"]) |
215 #except Annotation.DoesNotExist: |
207 except Annotation.DoesNotExist: |
216 except: |
208 #except: |
217 raise Http404 |
209 raise Http404 |
218 |
210 |
219 cont = base64.urlsafe_b64decode(str(request.POST["content"])) |
211 cont = request.POST["content"] |
220 doc = lxml.etree.fromstring(cont) |
212 doc = lxml.etree.fromstring(cont) |
221 |
213 |
222 uri = doc.xpath("/iri/text-annotation/uri/text()") |
214 uri = doc.xpath("/iri/text-annotation/uri/text()") |
223 if uri != [] and annot.uri != uri[0]: |
215 if uri != [] and annot.uri != uri[0]: |
224 annot.uri = unicode(uri[0]) |
216 annot.uri = unicode(uri[0]) |
252 update_date = doc.xpath("/iri/text-annotation/meta/modified/text()") |
244 update_date = doc.xpath("/iri/text-annotation/meta/modified/text()") |
253 if update_date != [] and annot.update_date != update_date[0]: |
245 if update_date != [] and annot.update_date != update_date[0]: |
254 annot.update_date = unicode(update_date[0]) |
246 annot.update_date = unicode(update_date[0]) |
255 |
247 |
256 annot.save() |
248 annot.save() |
257 |
249 |
258 return get_annotation(id) |
250 #create xml |
259 |
251 iri = lxml.etree.Element('iri') |
|
252 doc2 = lxml.etree.ElementTree(iri) |
|
253 |
|
254 textannotation = lxml.etree.SubElement(iri, 'text-annotation') |
|
255 id = lxml.etree.SubElement(textannotation,'id') |
|
256 id.text = annot.external_id |
|
257 uri = lxml.etree.SubElement(textannotation,'uri') |
|
258 uri.text = annot.uri |
|
259 |
|
260 if annot.tags: |
|
261 if type(annot.tags) is unicode: |
|
262 annot.tags = eval(annot.tags) |
|
263 tags = lxml.etree.SubElement(textannotation,'tags') |
|
264 ltags = normalize_tags(annot.tags) |
|
265 for t in ltags: |
|
266 tag = lxml.etree.SubElement(tags, 'tag') |
|
267 tag.text = t |
|
268 |
|
269 content = lxml.etree.SubElement(textannotation,'content') |
|
270 color = lxml.etree.SubElement(content,'color') |
|
271 color.text = annot.color |
|
272 description = lxml.etree.SubElement(content,'description') |
|
273 description.text = annot.description |
|
274 title = lxml.etree.SubElement(content,'title') |
|
275 title.text = annot.title |
|
276 text = lxml.etree.SubElement(content,'text') |
|
277 text.text = annot.text |
|
278 |
|
279 meta = lxml.etree.SubElement(textannotation,'meta') |
|
280 contributor = lxml.etree.SubElement(meta, "contributor") |
|
281 contributor.text = annot.contributor |
|
282 creator = lxml.etree.SubElement(meta, "creator") |
|
283 creator.text = annot.creator |
|
284 creationdate = lxml.etree.SubElement(meta, "created") |
|
285 creationdate.text = str(annot.creation_date) |
|
286 updatedate = lxml.etree.SubElement(meta, "modified") |
|
287 updatedate.text = str(annot.update_date) |
|
288 |
|
289 return HttpResponse(lxml.etree.tostring(doc2, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
290 |
|
291 |