--- a/web/ldt/text/views.py Wed Nov 17 18:57:34 2010 +0100
+++ b/web/ldt/text/views.py Fri Nov 19 14:29:17 2010 +0100
@@ -31,8 +31,7 @@
import lucene
import tempfile
import uuid
-from urllib import urlopen
-
+from tagging.models import Tag
## Filters the annotation depending on the request parameters
## Returns an xml containing the resulting annotations
@@ -59,40 +58,7 @@
doc = lxml.etree.ElementTree(iri)
for annot in annotlist:
- textannotation = lxml.etree.SubElement(iri, 'text-annotation')
- id = lxml.etree.SubElement(textannotation,'id')
- id.text = annot.external_id
- uri = lxml.etree.SubElement(textannotation,'uri')
- uri.text = annot.uri
-
- if annot.tags:
- if type(annot.tags) is unicode:
- annot.tags = eval(annot.tags)
- tags = lxml.etree.SubElement(textannotation,'tags')
- ltags = normalize_tags(annot.tags)
- for t in ltags:
- tag = lxml.etree.SubElement(tags, 'tag')
- tag.text = t
-
- content = lxml.etree.SubElement(textannotation,'content')
- color = lxml.etree.SubElement(content,'color')
- color.text = annot.color
- description = lxml.etree.SubElement(content,'description')
- description.text = annot.description
- title = lxml.etree.SubElement(content,'title')
- title.text = annot.title
- text = lxml.etree.SubElement(content,'text')
- text.text = annot.text
-
- meta = lxml.etree.SubElement(textannotation,'meta')
- contributor = lxml.etree.SubElement(meta, "contributor")
- contributor.text = annot.contributor
- creator = lxml.etree.SubElement(meta, "contributor")
- creator.text = annot.creator
- creationdate = lxml.etree.SubElement(meta, "created")
- creationdate.text = str(annot.creation_date)
- updatedate = lxml.etree.SubElement(meta, "modified")
- updatedate.text = str(annot.update_date)
+ annot.serialize(iri)
return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
@@ -105,30 +71,59 @@
cont = request.POST["content"]
doc = lxml.etree.fromstring(cont)
- id = unicode(doc.xpath("/iri/text-annotation/id/text()")[0])
- if id is None:
+ id_nodes = doc.xpath("/iri/text-annotation/id/text()")
+ if id_nodes:
+ id = unicode(id_nodes[0])
+ else:
id = generate_uuid()
uri = unicode(doc.xpath("/iri/text-annotation/uri/text()")[0])
- ltags = []
- for tag in doc.xpath("/iri/text-annotation/tags/tag/text()"):
- ltags.append(unicode(tag))
- tags=normalize_tags(ltags)
+
+ ltags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")]))
+ tags = ",".join(ltags)
+ if len(ltags) == 1:
+ tags += ","
+
- title = unicode(doc.xpath("/iri/text-annotation/content/title/text()")[0])
- desc = unicode(doc.xpath("/iri/text-annotation/content/description/text()")[0])
- text = unicode(doc.xpath("/iri/text-annotation/content/text/text()")[0])
- color = unicode(doc.xpath("/iri/text-annotation/content/color/text()")[0])
+ title_nodes = doc.xpath("/iri/text-annotation/content/title/text()")
+ if title_nodes:
+ title = unicode(title_nodes[0])
+ else:
+ title = None
+ desc_nodes = doc.xpath("/iri/text-annotation/content/description/text()")
+ if desc_nodes:
+ desc = unicode(desc_nodes[0])
+ else:
+ desc = None
+ text_nodes = doc.xpath("/iri/text-annotation/content/text/text()")
+ if text_nodes:
+ text = unicode(text_nodes[0])
+ else:
+ text = None
+ color_nodes = doc.xpath("/iri/text-annotation/content/color/text()")
+ if color_nodes:
+ color = unicode(color_nodes[0])
+ else:
+ color = None
- creator = unicode(doc.xpath("/iri/text-annotation/meta/creator/text()")[0])
- contributor = unicode(doc.xpath("/iri/text-annotation/meta/contributor/text()")[0])
- creation_date = unicode(doc.xpath("/iri/text-annotation/meta/created/text()")[0])
- update_date = unicode(doc.xpath("/iri/text-annotation/meta/modified/text()")[0])
+ creator_nodes = doc.xpath("/iri/text-annotation/meta/creator/text()")
+ if creator_nodes:
+ creator = unicode(creator_nodes[0])
+ else:
+ creator = None
+ contributor_nodes = doc.xpath("/iri/text-annotation/meta/contributor/text()")
+ if contributor_nodes:
+ contributor = unicode(contributor_nodes[0])
+ else:
+ contributor = None
+
+ #creation_date = unicode(doc.xpath("/iri/text-annotation/meta/created/text()")[0])
+ #update_date = unicode(doc.xpath("/iri/text-annotation/meta/modified/text()")[0])
try:
- 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)
+ annotation = Annotation.create_annotation(external_id=id, uri=uri, tags=tags, title=title, description=desc, text=text, color=color, creator=creator, contributor=contributor)
annotation.save()
- return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
+ return HttpResponse(lxml.etree.tostring(annotation.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8")
except IntegrityError:
return HttpResponse(status=409)
@@ -141,43 +136,8 @@
annot = Annotation.objects.get(external_id=request.GET.get('id',''))
except Annotation.DoesNotExist:
raise Http404
- iri = lxml.etree.Element('iri')
- doc = lxml.etree.ElementTree(iri)
-
- textannotation = lxml.etree.SubElement(iri, 'text-annotation')
- id = lxml.etree.SubElement(textannotation,'id')
- id.text = annot.external_id
- uri = lxml.etree.SubElement(textannotation,'uri')
- uri.text = annot.uri
-
- if annot.tags:
- if type(annot.tags) is unicode:
- annot.tags = eval(annot.tags)
- tags = lxml.etree.SubElement(textannotation,'tags')
- ltags = normalize_tags(annot.tags)
- for t in ltags:
- tag = lxml.etree.SubElement(tags, 'tag')
- tag.text = t
-
- content = lxml.etree.SubElement(textannotation,'content')
- color = lxml.etree.SubElement(content,'color')
- color.text = annot.color
- description = lxml.etree.SubElement(content,'description')
- description.text = annot.description
- title = lxml.etree.SubElement(content,'title')
- title.text = annot.title
- text = lxml.etree.SubElement(content,'text')
- text.text = annot.text
-
- meta = lxml.etree.SubElement(textannotation,'meta')
- contributor = lxml.etree.SubElement(meta, "contributor")
- contributor.text = annot.contributor
- creator = lxml.etree.SubElement(meta, "creator")
- creator.text = annot.creator
- creationdate = lxml.etree.SubElement(meta, "created")
- creationdate.text = str(annot.creation_date)
- updatedate = lxml.etree.SubElement(meta, "modified")
- updatedate.text = str(annot.update_date)
+
+ doc = annot.serialize()
return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8")
@@ -215,77 +175,40 @@
if uri != [] and annot.uri != uri[0]:
annot.uri = unicode(uri[0])
- tags = []
- for tag in doc.xpath("/iri/text-annotation/tags/tag"):
- tags.append(unicode(tag.text))
- if annot.tags is not None:
- if type(annot.tags) is unicode:
- annot.tags = eval(annot.tags)
- tags += annot.tags
- tags = normalize_tags(tags)
- annot.tags=tags
+ tags_nodes = doc.xpath("/iri/text-annotation/tags")
+ if len(tags_nodes) > 0:
+ tags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")]))
+ tags_str = ",".join(tags)
+ if len(tags) == 1:
+ tags_str += ","
+ annot.tags = tags_str
+
+
+
+
title = doc.xpath("/iri/text-annotation/content/title/text()")
- if title != [] and annot.title != title[0]:
+ if title and annot.title != title[0]:
annot.title = unicode(title[0])
desc = doc.xpath("/iri/text-annotation/content/description/text()")
- if desc != [] and annot.description != desc[0]:
+ if desc and annot.description != desc[0]:
annot.description = unicode(desc[0])
text = doc.xpath("/iri/text-annotation/content/text/text()")
- if text != [] and annot.text != text[0]:
+ if text and annot.text != text[0]:
annot.text = unicode(text[0])
color = doc.xpath("/iri/text-annotation/content/color/text()")
- if color != [] and annot.color != color[0]:
+ if color and annot.color != color[0]:
annot.color = unicode(color[0])
contributor = doc.xpath("/iri/text-annotation/meta/contributor/text()")
- if contributor != [] and annot.contributor != contributor[0]:
+ if contributor and annot.contributor != contributor[0]:
annot.contributor = unicode(contributor[0])
update_date = doc.xpath("/iri/text-annotation/meta/modified/text()")
- if update_date != [] and annot.update_date != update_date[0]:
+ if update_date and annot.update_date != update_date[0]:
annot.update_date = unicode(update_date[0])
annot.save()
- #create xml
- iri = lxml.etree.Element('iri')
- doc2 = lxml.etree.ElementTree(iri)
-
- textannotation = lxml.etree.SubElement(iri, 'text-annotation')
- id = lxml.etree.SubElement(textannotation,'id')
- id.text = annot.external_id
- uri = lxml.etree.SubElement(textannotation,'uri')
- uri.text = annot.uri
-
- if annot.tags:
- if type(annot.tags) is unicode:
- annot.tags = eval(annot.tags)
- tags = lxml.etree.SubElement(textannotation,'tags')
- ltags = normalize_tags(annot.tags)
- for t in ltags:
- tag = lxml.etree.SubElement(tags, 'tag')
- tag.text = t
-
- content = lxml.etree.SubElement(textannotation,'content')
- color = lxml.etree.SubElement(content,'color')
- color.text = annot.color
- description = lxml.etree.SubElement(content,'description')
- description.text = annot.description
- title = lxml.etree.SubElement(content,'title')
- title.text = annot.title
- text = lxml.etree.SubElement(content,'text')
- text.text = annot.text
-
- meta = lxml.etree.SubElement(textannotation,'meta')
- contributor = lxml.etree.SubElement(meta, "contributor")
- contributor.text = annot.contributor
- creator = lxml.etree.SubElement(meta, "creator")
- creator.text = annot.creator
- creationdate = lxml.etree.SubElement(meta, "created")
- creationdate.text = str(annot.creation_date)
- updatedate = lxml.etree.SubElement(meta, "modified")
- updatedate.text = str(annot.update_date)
-
- return HttpResponse(lxml.etree.tostring(doc2, pretty_print=True), mimetype="text/xml;charset=utf-8")
+ return HttpResponse(lxml.etree.tostring(annot.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8")