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