|
1 from django.conf import settings |
|
2 from django.contrib.auth.decorators import login_required |
|
3 from django.core.urlresolvers import reverse |
|
4 from django.db import IntegrityError |
|
5 from django.db.models import Q |
|
6 from django.forms.util import ErrorList |
|
7 from django.http import HttpResponse, Http404, HttpResponseRedirect, \ |
|
8 HttpResponseForbidden, HttpResponseServerError, HttpResponseBadRequest |
|
9 from django.shortcuts import render_to_response, get_object_or_404, \ |
|
10 get_list_or_404 |
|
11 from django.template import RequestContext |
|
12 from django.template.loader import render_to_string |
|
13 from django.utils.html import escape |
|
14 from django.utils.translation import ugettext as _, ungettext |
|
15 from httplib import CONFLICT |
|
16 from ldt.core.models import Owner |
|
17 from ldt.text.utils import boolean_convert |
|
18 from lxml import etree |
|
19 from lxml.html import fromstring, fragment_fromstring |
|
20 from ldt.text.models import * |
|
21 from string import Template |
|
22 from urllib2 import urlparse |
|
23 from utils import * |
|
24 import StringIO |
|
25 import base64 |
|
26 import cgi |
|
27 import django.core.urlresolvers |
|
28 import ldt.auth as ldt_auth |
|
29 import ldt.utils.path as ldt_utils_path |
|
30 import logging |
|
31 import lucene |
|
32 import tempfile |
|
33 import uuid |
|
34 |
|
35 |
|
36 ## Filters the annotation depending on the request parameters |
|
37 ## Returns an xml containing the resulting annotations |
|
38 def filter_annotation(request, uri=None, filter=None, limit=None, creator=None): |
|
39 annotlist = None |
|
40 query = Q() |
|
41 |
|
42 if request.GET.get('uri'): |
|
43 query &= Q(uri=request.GET.get('uri')) |
|
44 if request.GET.get('creator'): |
|
45 query &= Q(creator=request.GET.get('creator')) |
|
46 if request.GET.get('filter') and len(request.GET.get('filter')) > 0: |
|
47 query &= Q(text__icontains=request.GET.get('filter')) |
|
48 |
|
49 annotlist = Annotation.objects.filter(query) |
|
50 |
|
51 if request.GET.get('limit'): |
|
52 nb = request.GET.get('limit') |
|
53 #offset = request.GET.get('limit')[1] |
|
54 annotlist = annotlist[:nb] |
|
55 |
|
56 #create xml |
|
57 iri = lxml.etree.Element('iri') |
|
58 doc = lxml.etree.ElementTree(iri) |
|
59 |
|
60 for annot in annotlist: |
|
61 textannotation = lxml.etree.SubElement(iri, 'text-annotation') |
|
62 id = lxml.etree.SubElement(textannotation,'id') |
|
63 id.text = annot.external_id |
|
64 uri = lxml.etree.SubElement(textannotation,'uri') |
|
65 uri.text = annot.uri |
|
66 |
|
67 if annot.tags: |
|
68 if type(annot.tags) is unicode: |
|
69 annot.tags = eval(annot.tags) |
|
70 tags = lxml.etree.SubElement(textannotation,'tags') |
|
71 ltags = normalize_tags(annot.tags) |
|
72 for t in ltags: |
|
73 tag = lxml.etree.SubElement(tags, 'tag') |
|
74 tag.text = t |
|
75 |
|
76 content = lxml.etree.SubElement(textannotation,'content') |
|
77 color = lxml.etree.SubElement(content,'color') |
|
78 color.text = annot.color |
|
79 description = lxml.etree.SubElement(content,'description') |
|
80 description.text = annot.description |
|
81 title = lxml.etree.SubElement(content,'title') |
|
82 title.text = annot.title |
|
83 text = lxml.etree.SubElement(content,'text') |
|
84 text.text = annot.text |
|
85 |
|
86 meta = lxml.etree.SubElement(textannotation,'meta') |
|
87 contributor = lxml.etree.SubElement(meta, "contributor") |
|
88 contributor.text = annot.contributor |
|
89 creator = lxml.etree.SubElement(meta, "contributor") |
|
90 creator.text = annot.creator |
|
91 creationdate = lxml.etree.SubElement(meta, "created") |
|
92 creationdate.text = str(annot.creation_date) |
|
93 updatedate = lxml.etree.SubElement(meta, "modified") |
|
94 updatedate.text = str(annot.update_date) |
|
95 |
|
96 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
97 |
|
98 |
|
99 ## Creates an annotation from a urlencoded xml content |
|
100 ## Returns an xml-structured annotation |
|
101 #@login_required |
|
102 def create_annotation(request, content): |
|
103 cont = base64.urlsafe_b64decode(request.POST["content"]) |
|
104 doc = lxml.etree.fromstring(cont) |
|
105 |
|
106 id = unicode(doc.xpath("/iri/text-annotation/id/text()")[0]) |
|
107 if id is None: |
|
108 id = generate_uuid() |
|
109 |
|
110 uri = unicode(doc.xpath("/iri/text-annotation/uri/text()")[0]) |
|
111 ltags = [] |
|
112 for tag in doc.xpath("/iri/text-annotation/tags/tag/text()"): |
|
113 ltags.append(unicode(tag)) |
|
114 tags=normalize_tags(ltags) |
|
115 |
|
116 title = unicode(doc.xpath("/iri/text-annotation/content/title/text()")[0]) |
|
117 desc = unicode(doc.xpath("/iri/text-annotation/content/description/text()")[0]) |
|
118 text = unicode(doc.xpath("/iri/text-annotation/content/text/text()")[0]) |
|
119 color = unicode(doc.xpath("/iri/text-annotation/content/color/text()")[0]) |
|
120 |
|
121 creator = unicode(doc.xpath("/iri/text-annotation/meta/creator/text()")[0]) |
|
122 contributor = unicode(doc.xpath("/iri/text-annotation/meta/contributor/text()")[0]) |
|
123 creation_date = unicode(doc.xpath("/iri/text-annotation/meta/created/text()")[0]) |
|
124 update_date = unicode(doc.xpath("/iri/text-annotation/meta/modified/text()")[0]) |
|
125 |
|
126 try: |
|
127 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) |
|
128 annotation.save() |
|
129 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
130 #return doc |
|
131 except: |
|
132 #except Annotation.IntegrityError: |
|
133 #print 'This id is already used! Please choose another one!' |
|
134 raise CONFLICT |
|
135 |
|
136 |
|
137 |
|
138 ## Gets an annotation (from its id) |
|
139 ## Returns the xml-structured annotation |
|
140 def get_annotation(request, id): |
|
141 try: |
|
142 annot = Annotation.objects.get(external_id=request.GET.get('id','')) |
|
143 except: |
|
144 #except Annotation.DoesNotExist: |
|
145 raise Http404 |
|
146 iri = lxml.etree.Element('iri') |
|
147 doc = lxml.etree.ElementTree(iri) |
|
148 |
|
149 textannotation = lxml.etree.SubElement(iri, 'text-annotation') |
|
150 id = lxml.etree.SubElement(textannotation,'id') |
|
151 id.text = annot.external_id |
|
152 uri = lxml.etree.SubElement(textannotation,'uri') |
|
153 uri.text = annot.uri |
|
154 |
|
155 if annot.tags: |
|
156 if type(annot.tags) is unicode: |
|
157 annot.tags = eval(annot.tags) |
|
158 tags = lxml.etree.SubElement(textannotation,'tags') |
|
159 ltags = normalize_tags(annot.tags) |
|
160 for t in ltags: |
|
161 tag = lxml.etree.SubElement(tags, 'tag') |
|
162 tag.text = t |
|
163 |
|
164 content = lxml.etree.SubElement(textannotation,'content') |
|
165 color = lxml.etree.SubElement(content,'color') |
|
166 color.text = annot.color |
|
167 description = lxml.etree.SubElement(content,'description') |
|
168 description.text = annot.description |
|
169 title = lxml.etree.SubElement(content,'title') |
|
170 title.text = annot.title |
|
171 text = lxml.etree.SubElement(content,'text') |
|
172 text.text = annot.text |
|
173 |
|
174 meta = lxml.etree.SubElement(textannotation,'meta') |
|
175 contributor = lxml.etree.SubElement(meta, "contributor") |
|
176 contributor.text = annot.contributor |
|
177 creator = lxml.etree.SubElement(meta, "creator") |
|
178 creator.text = annot.creator |
|
179 creationdate = lxml.etree.SubElement(meta, "created") |
|
180 creationdate.text = str(annot.creation_date) |
|
181 updatedate = lxml.etree.SubElement(meta, "modified") |
|
182 updatedate.text = str(annot.update_date) |
|
183 |
|
184 #return doc |
|
185 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
186 |
|
187 |
|
188 ## Deletes an annotation (from its id) |
|
189 ## Returns an empty xml-structured annotation |
|
190 #@login_required |
|
191 def delete_annotation(request, id): |
|
192 try: |
|
193 annot = Annotation.objects.get(external_id=request.POST["id"]) |
|
194 annot.delete() |
|
195 #except Annotation.DoesNotExist: |
|
196 except: |
|
197 raise Http404 |
|
198 |
|
199 doc=create_empty_annotation() |
|
200 #return doc |
|
201 return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
202 |
|
203 |
|
204 ## Updates the content of an annotation |
|
205 ## Returns the xml-structured updated annotation |
|
206 #@login_required |
|
207 def update_annotation(request, content, id): |
|
208 try: |
|
209 annot = Annotation.objects.get(external_id=request.POST["id"]) |
|
210 #except Annotation.DoesNotExist: |
|
211 except: |
|
212 raise Http404 |
|
213 |
|
214 cont = base64.urlsafe_b64decode(request.POST["content"]) |
|
215 doc = lxml.etree.fromstring(cont) |
|
216 |
|
217 uri = doc.xpath("/iri/text-annotation/uri/text()") |
|
218 if uri != [] and annot.uri != uri[0]: |
|
219 annot.uri = unicode(uri[0]) |
|
220 |
|
221 tags = [] |
|
222 for tag in doc.xpath("/iri/text-annotation/tags/tag"): |
|
223 tags.append(unicode(tag.text)) |
|
224 if annot.tags is not None: |
|
225 if type(annot.tags) is unicode: |
|
226 annot.tags = eval(annot.tags) |
|
227 tags += annot.tags |
|
228 tags = normalize_tags(tags) |
|
229 annot.tags=tags |
|
230 |
|
231 title = doc.xpath("/iri/text-annotation/content/title/text()") |
|
232 if title != [] and annot.title != title[0]: |
|
233 annot.title = unicode(title[0]) |
|
234 desc = doc.xpath("/iri/text-annotation/content/description/text()") |
|
235 if desc != [] and annot.description != desc[0]: |
|
236 annot.description = unicode(desc[0]) |
|
237 text = doc.xpath("/iri/text-annotation/content/text/text()") |
|
238 if text != [] and annot.text != text[0]: |
|
239 annot.text = unicode(text[0]) |
|
240 color = doc.xpath("/iri/text-annotation/content/color/text()") |
|
241 if color != [] and annot.color != color[0]: |
|
242 annot.color = unicode(color[0]) |
|
243 |
|
244 contributor = doc.xpath("/iri/text-annotation/meta/contributor/text()") |
|
245 if contributor != [] and annot.contributor != contributor[0]: |
|
246 annot.contributor = unicode(contributor[0]) |
|
247 update_date = doc.xpath("/iri/text-annotation/meta/modified/text()") |
|
248 if update_date != [] and annot.update_date != update_date[0]: |
|
249 annot.update_date = unicode(update_date[0]) |
|
250 |
|
251 annot.save() |
|
252 |
|
253 return get_annotation(id) |
|
254 |