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