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