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