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