| author | ymh <ymh.work@gmail.com> |
| Wed, 15 May 2013 10:05:17 +0200 | |
| changeset 1190 | 129d45eec68c |
| parent 718 | 5e27a39d3742 |
| permissions | -rw-r--r-- |
|
111
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
1 |
from django.db import IntegrityError |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
2 |
from django.db.models import Q |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
3 |
from django.http import HttpResponse, Http404 |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
4 |
from django.views.decorators.csrf import csrf_exempt |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
5 |
from ldt.text.models import Annotation |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
6 |
from ldt.text.utils import TextSearch, generate_uuid |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
7 |
from oauth_provider.decorators import oauth_required |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
8 |
from urllib2 import urlparse |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
718
diff
changeset
|
9 |
import lxml.etree |
|
111
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
10 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
11 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
12 |
## Filters the annotation depending on the request parameters |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
13 |
## Returns an xml containing the resulting annotations |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
718
diff
changeset
|
14 |
def filter_annotation(request, uri=None, filter=None, limit=None, creator=None): # @ReservedAssignment |
|
111
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
15 |
query = Q() |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
16 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
17 |
if request.GET.get('uri'): |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
18 |
query &= Q(uri=request.GET.get('uri')) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
19 |
if request.GET.get('creator'): |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
20 |
query &= Q(creator=request.GET.get('creator')) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
21 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
22 |
annotlist = Annotation.objects.filter(query) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
23 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
24 |
if request.GET.get('filter') and len(request.GET.get('filter')) > 0: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
25 |
search = TextSearch() |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
26 |
res = search.query("text", request.GET.get('filter')) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
27 |
for r in res: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
28 |
annotlist.append(r) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
29 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
30 |
if request.GET.get('limit'): |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
31 |
nb = request.GET.get('limit')[0] |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
32 |
offset = request.GET.get('limit')[2] |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
33 |
annotlist = annotlist[offset:] |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
34 |
annotlist = annotlist[:nb] |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
35 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
36 |
#create xml |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
37 |
iri = lxml.etree.Element('iri') |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
38 |
doc = lxml.etree.ElementTree(iri) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
39 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
40 |
for annot in annotlist: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
41 |
annot.serialize(iri) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
42 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
43 |
return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
44 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
45 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
46 |
## Creates an annotation from a urlencoded xml content |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
47 |
## Returns an xml-structured annotation |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
48 |
@oauth_required |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
49 |
@csrf_exempt |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
50 |
def create_annotation(request): |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
51 |
cont = request.POST["content"] |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
52 |
doc = lxml.etree.fromstring(cont) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
53 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
54 |
id_nodes = doc.xpath("/iri/text-annotation/id/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
55 |
if id_nodes: |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
718
diff
changeset
|
56 |
id_n = unicode(id_nodes[0]) |
|
111
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
57 |
else: |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
718
diff
changeset
|
58 |
id_n = generate_uuid() |
|
111
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
59 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
60 |
uri = unicode(doc.xpath("/iri/text-annotation/uri/text()")[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
61 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
62 |
ltags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")])) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
63 |
tags = ",".join(ltags) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
64 |
if len(ltags) == 1: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
65 |
tags += "," |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
66 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
67 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
68 |
title_nodes = doc.xpath("/iri/text-annotation/content/title/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
69 |
if title_nodes: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
70 |
title = unicode(title_nodes[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
71 |
else: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
72 |
title = None |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
73 |
desc_nodes = doc.xpath("/iri/text-annotation/content/description/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
74 |
if desc_nodes: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
75 |
desc = unicode(desc_nodes[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
76 |
else: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
77 |
desc = None |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
78 |
text_nodes = doc.xpath("/iri/text-annotation/content/text/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
79 |
if text_nodes: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
80 |
text = unicode(text_nodes[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
81 |
else: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
82 |
text = None |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
83 |
color_nodes = doc.xpath("/iri/text-annotation/content/color/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
84 |
if color_nodes: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
85 |
color = unicode(color_nodes[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
86 |
else: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
87 |
color = None |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
88 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
89 |
creator_nodes = doc.xpath("/iri/text-annotation/meta/creator/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
90 |
if creator_nodes: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
91 |
creator = unicode(creator_nodes[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
92 |
else: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
93 |
creator = None |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
94 |
contributor_nodes = doc.xpath("/iri/text-annotation/meta/contributor/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
95 |
if contributor_nodes: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
96 |
contributor = unicode(contributor_nodes[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
97 |
else: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
98 |
contributor = None |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
99 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
100 |
try: |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
718
diff
changeset
|
101 |
annotation = Annotation.create_annotation(external_id=id_n, uri=uri, tags=tags, title=title, description=desc, text=text, color=color, creator=creator, contributor=contributor) |
|
111
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
102 |
annotation.save() |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
103 |
return HttpResponse(lxml.etree.tostring(annotation.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
104 |
except IntegrityError: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
105 |
return HttpResponse(status=409) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
106 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
107 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
108 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
109 |
## Gets an annotation from its id |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
110 |
## Returns the xml-structured annotation |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
718
diff
changeset
|
111 |
def get_annotation(request, id): # @ReservedAssignment |
|
111
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
112 |
try: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
113 |
annot = Annotation.objects.get(external_id=id) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
114 |
except Annotation.DoesNotExist: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
115 |
raise Http404 |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
116 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
117 |
doc = annot.serialize() |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
118 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
119 |
return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
120 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
121 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
122 |
## Deletes an annotation (from its id) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
123 |
## Returns an empty xml-structured annotation |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
124 |
@oauth_required |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
125 |
@csrf_exempt |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
718
diff
changeset
|
126 |
def delete_annotation(request, id): # @ReservedAssignment |
|
111
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
127 |
try: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
128 |
annot = Annotation.objects.get(external_id=id) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
129 |
annot.delete() |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
130 |
except Annotation.DoesNotExist: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
131 |
raise Http404 |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
132 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
133 |
return HttpResponse("") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
134 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
135 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
136 |
## Updates the content of an annotation |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
137 |
## Returns the xml-structured updated annotation |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
138 |
@oauth_required |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
139 |
@csrf_exempt |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
718
diff
changeset
|
140 |
def update_annotation(request, id): # @ReservedAssignment |
|
111
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
141 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
142 |
put_data = {} |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
143 |
if request.GET != {}: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
144 |
put_data = request.GET.copy() |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
145 |
elif request.raw_post_data is not None: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
146 |
for k, v in urlparse.parse_qs(request.raw_post_data).iteritems(): |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
147 |
if len(v) > 1: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
148 |
for item in v: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
149 |
put_data[k] = item |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
150 |
else: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
151 |
put_data[k] = v[0] |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
152 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
153 |
try: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
154 |
annot = Annotation.objects.get(external_id=id) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
155 |
except Annotation.DoesNotExist: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
156 |
raise Http404 |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
157 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
158 |
cont = put_data['content'] |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
159 |
doc = lxml.etree.fromstring(cont) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
160 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
161 |
uri = doc.xpath("/iri/text-annotation/uri/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
162 |
if uri != [] and annot.uri != uri[0]: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
163 |
annot.uri = unicode(uri[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
164 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
165 |
tags_nodes = doc.xpath("/iri/text-annotation/tags") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
166 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
167 |
if len(tags_nodes) > 0: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
168 |
tags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")])) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
169 |
tags_str = ",".join(tags) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
170 |
if len(tags) == 1: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
171 |
tags_str += "," |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
172 |
annot.tags = tags_str |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
173 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
174 |
title = doc.xpath("/iri/text-annotation/content/title/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
175 |
if title and annot.title != title[0]: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
176 |
annot.title = unicode(title[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
177 |
desc = doc.xpath("/iri/text-annotation/content/description/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
178 |
if desc and annot.description != desc[0]: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
179 |
annot.description = unicode(desc[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
180 |
text = doc.xpath("/iri/text-annotation/content/text/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
181 |
if text and annot.text != text[0]: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
182 |
annot.text = unicode(text[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
183 |
color = doc.xpath("/iri/text-annotation/content/color/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
184 |
if color and annot.color != color[0]: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
185 |
annot.color = unicode(color[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
186 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
187 |
contributor = doc.xpath("/iri/text-annotation/meta/contributor/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
188 |
if contributor and annot.contributor != contributor[0]: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
189 |
annot.contributor = unicode(contributor[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
190 |
update_date = doc.xpath("/iri/text-annotation/meta/modified/text()") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
191 |
if update_date and annot.update_date != update_date[0]: |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
192 |
annot.update_date = unicode(update_date[0]) |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
193 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
194 |
annot.save() |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
195 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
196 |
return HttpResponse(lxml.etree.tostring(annot.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
197 |
|
|
4535dafa6007
improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
198 |