| author | wakimd |
| Wed, 22 Dec 2010 12:01:05 +0100 | |
| changeset 25 | c8dfd7ea87e5 |
| parent 24 | 9e19b7ae3780 |
| permissions | -rw-r--r-- |
| 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 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 |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
34 |
from tagging.models import Tag |
| 19 | 35 |
from oauth_provider.decorators import * |
| 9 | 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 |
||
48 |
annotlist = Annotation.objects.filter(query) |
|
49 |
||
| 21 | 50 |
if request.GET.get('filter') and len(request.GET.get('filter')) > 0: |
51 |
search = LdtSearch() |
|
52 |
res = search.query("all",request.GET.get('filter')) |
|
53 |
for r in res: |
|
54 |
annotlist.append(r) |
|
55 |
||
| 9 | 56 |
if request.GET.get('limit'): |
57 |
nb = request.GET.get('limit') |
|
58 |
#offset = request.GET.get('limit')[1] |
|
59 |
annotlist = annotlist[:nb] |
|
60 |
||
61 |
#create xml |
|
62 |
iri = lxml.etree.Element('iri') |
|
63 |
doc = lxml.etree.ElementTree(iri) |
|
64 |
||
65 |
for annot in annotlist: |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
66 |
annot.serialize(iri) |
| 9 | 67 |
|
68 |
return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
69 |
||
70 |
||
71 |
## Creates an annotation from a urlencoded xml content |
|
72 |
## Returns an xml-structured annotation |
|
| 25 | 73 |
@oauth_required |
| 15 | 74 |
@csrf_exempt |
| 16 | 75 |
def create_annotation(request): |
76 |
cont = request.POST["content"] |
|
| 9 | 77 |
doc = lxml.etree.fromstring(cont) |
78 |
||
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
79 |
id_nodes = doc.xpath("/iri/text-annotation/id/text()") |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
80 |
if id_nodes: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
81 |
id = unicode(id_nodes[0]) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
82 |
else: |
| 9 | 83 |
id = generate_uuid() |
84 |
||
85 |
uri = unicode(doc.xpath("/iri/text-annotation/uri/text()")[0]) |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
86 |
|
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
87 |
ltags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")])) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
88 |
tags = ",".join(ltags) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
89 |
if len(ltags) == 1: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
90 |
tags += "," |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
91 |
|
| 9 | 92 |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
93 |
title_nodes = doc.xpath("/iri/text-annotation/content/title/text()") |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
94 |
if title_nodes: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
95 |
title = unicode(title_nodes[0]) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
96 |
else: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
97 |
title = None |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
98 |
desc_nodes = doc.xpath("/iri/text-annotation/content/description/text()") |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
99 |
if desc_nodes: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
100 |
desc = unicode(desc_nodes[0]) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
101 |
else: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
102 |
desc = None |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
103 |
text_nodes = doc.xpath("/iri/text-annotation/content/text/text()") |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
104 |
if text_nodes: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
105 |
text = unicode(text_nodes[0]) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
106 |
else: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
107 |
text = None |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
108 |
color_nodes = doc.xpath("/iri/text-annotation/content/color/text()") |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
109 |
if color_nodes: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
110 |
color = unicode(color_nodes[0]) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
111 |
else: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
112 |
color = None |
| 9 | 113 |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
114 |
creator_nodes = doc.xpath("/iri/text-annotation/meta/creator/text()") |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
115 |
if creator_nodes: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
116 |
creator = unicode(creator_nodes[0]) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
117 |
else: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
118 |
creator = None |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
119 |
contributor_nodes = doc.xpath("/iri/text-annotation/meta/contributor/text()") |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
120 |
if contributor_nodes: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
121 |
contributor = unicode(contributor_nodes[0]) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
122 |
else: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
123 |
contributor = None |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
124 |
|
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
125 |
#creation_date = unicode(doc.xpath("/iri/text-annotation/meta/created/text()")[0]) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
126 |
#update_date = unicode(doc.xpath("/iri/text-annotation/meta/modified/text()")[0]) |
| 9 | 127 |
|
128 |
try: |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
129 |
annotation = Annotation.create_annotation(external_id=id, uri=uri, tags=tags, title=title, description=desc, text=text, color=color, creator=creator, contributor=contributor) |
| 9 | 130 |
annotation.save() |
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
131 |
return HttpResponse(lxml.etree.tostring(annotation.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8") |
| 10 | 132 |
except IntegrityError: |
133 |
return HttpResponse(status=409) |
|
| 16 | 134 |
|
| 9 | 135 |
|
136 |
||
137 |
## Gets an annotation (from its id) |
|
138 |
## Returns the xml-structured annotation |
|
139 |
def get_annotation(request, id): |
|
140 |
try: |
|
141 |
annot = Annotation.objects.get(external_id=request.GET.get('id','')) |
|
| 16 | 142 |
except Annotation.DoesNotExist: |
| 9 | 143 |
raise Http404 |
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
144 |
|
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
145 |
doc = annot.serialize() |
| 9 | 146 |
|
147 |
return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
148 |
||
149 |
||
150 |
## Deletes an annotation (from its id) |
|
151 |
## Returns an empty xml-structured annotation |
|
| 25 | 152 |
@oauth_required |
| 15 | 153 |
@csrf_exempt |
| 16 | 154 |
def delete_annotation(request): |
| 9 | 155 |
try: |
156 |
annot = Annotation.objects.get(external_id=request.POST["id"]) |
|
157 |
annot.delete() |
|
| 16 | 158 |
except Annotation.DoesNotExist: |
| 9 | 159 |
raise Http404 |
160 |
||
| 22 | 161 |
return HttpResponse("") |
| 9 | 162 |
|
163 |
||
164 |
## Updates the content of an annotation |
|
165 |
## Returns the xml-structured updated annotation |
|
| 25 | 166 |
@oauth_required |
| 15 | 167 |
@csrf_exempt |
| 16 | 168 |
def update_annotation(request): |
| 9 | 169 |
try: |
170 |
annot = Annotation.objects.get(external_id=request.POST["id"]) |
|
| 16 | 171 |
except Annotation.DoesNotExist: |
172 |
#except: |
|
| 9 | 173 |
raise Http404 |
174 |
||
| 16 | 175 |
cont = request.POST["content"] |
| 9 | 176 |
doc = lxml.etree.fromstring(cont) |
177 |
||
178 |
uri = doc.xpath("/iri/text-annotation/uri/text()") |
|
179 |
if uri != [] and annot.uri != uri[0]: |
|
180 |
annot.uri = unicode(uri[0]) |
|
181 |
||
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
182 |
tags_nodes = doc.xpath("/iri/text-annotation/tags") |
| 9 | 183 |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
184 |
if len(tags_nodes) > 0: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
185 |
tags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")])) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
186 |
tags_str = ",".join(tags) |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
187 |
if len(tags) == 1: |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
188 |
tags_str += "," |
|
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
189 |
annot.tags = tags_str |
| 21 | 190 |
|
| 9 | 191 |
title = doc.xpath("/iri/text-annotation/content/title/text()") |
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
192 |
if title and annot.title != title[0]: |
| 9 | 193 |
annot.title = unicode(title[0]) |
194 |
desc = doc.xpath("/iri/text-annotation/content/description/text()") |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
195 |
if desc and annot.description != desc[0]: |
| 9 | 196 |
annot.description = unicode(desc[0]) |
197 |
text = doc.xpath("/iri/text-annotation/content/text/text()") |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
198 |
if text and annot.text != text[0]: |
| 9 | 199 |
annot.text = unicode(text[0]) |
200 |
color = doc.xpath("/iri/text-annotation/content/color/text()") |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
201 |
if color and annot.color != color[0]: |
| 9 | 202 |
annot.color = unicode(color[0]) |
203 |
||
204 |
contributor = doc.xpath("/iri/text-annotation/meta/contributor/text()") |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
205 |
if contributor and annot.contributor != contributor[0]: |
| 9 | 206 |
annot.contributor = unicode(contributor[0]) |
207 |
update_date = doc.xpath("/iri/text-annotation/meta/modified/text()") |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
208 |
if update_date and annot.update_date != update_date[0]: |
| 9 | 209 |
annot.update_date = unicode(update_date[0]) |
210 |
||
211 |
annot.save() |
|
| 21 | 212 |
annot.update_index() |
| 16 | 213 |
|
|
17
683ce4109c28
various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents:
16
diff
changeset
|
214 |
return HttpResponse(lxml.etree.tostring(annot.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8") |
| 16 | 215 |
|
216 |