web/ldt/text/views.py
author wakimd
Wed, 22 Dec 2010 12:01:05 +0100
changeset 25 c8dfd7ea87e5
parent 24 9e19b7ae3780
permissions -rw-r--r--
Corrections on merge
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
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
    15
from django.views.decorators.csrf import csrf_exempt
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    16
from httplib import CONFLICT
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    17
from ldt.core.models import Owner
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
    18
from ldt.text.models import *
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    19
from ldt.text.utils import boolean_convert
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    20
from lxml import etree
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    21
from lxml.html import fromstring, fragment_fromstring
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    22
from string import Template
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    23
from urllib2 import urlparse
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    24
from utils import *
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    25
import StringIO
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
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    34
from tagging.models import Tag
19
7cf81d58a968 oauth start
wakimd
parents: 17
diff changeset
    35
from oauth_provider.decorators import * 
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    36
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    37
## Filters the annotation depending on the request parameters
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    38
## Returns an xml containing the resulting annotations
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    39
def filter_annotation(request, uri=None, filter=None, limit=None, creator=None):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    40
    annotlist = None
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    41
    query = Q()    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    42
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    43
    if request.GET.get('uri'):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    44
        query &= Q(uri=request.GET.get('uri'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    45
    if request.GET.get('creator'):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    46
        query &= Q(creator=request.GET.get('creator'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    47
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    48
    annotlist = Annotation.objects.filter(query)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    49
    
21
1a061f244254 Pylucene indexation
wakimd
parents: 19
diff changeset
    50
    if request.GET.get('filter') and len(request.GET.get('filter')) > 0:
1a061f244254 Pylucene indexation
wakimd
parents: 19
diff changeset
    51
        search = LdtSearch()
1a061f244254 Pylucene indexation
wakimd
parents: 19
diff changeset
    52
        res = search.query("all",request.GET.get('filter'))        
1a061f244254 Pylucene indexation
wakimd
parents: 19
diff changeset
    53
        for r in res:
1a061f244254 Pylucene indexation
wakimd
parents: 19
diff changeset
    54
            annotlist.append(r)
1a061f244254 Pylucene indexation
wakimd
parents: 19
diff changeset
    55
    
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    56
    if request.GET.get('limit'):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    57
        nb = request.GET.get('limit')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    58
        #offset = request.GET.get('limit')[1]
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    59
        annotlist = annotlist[:nb]
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    60
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    61
    #create xml
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    62
    iri = lxml.etree.Element('iri')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    63
    doc = lxml.etree.ElementTree(iri)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    64
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    65
    for annot in annotlist:
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    66
        annot.serialize(iri)
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    67
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    68
    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
    69
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    70
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    71
## Creates an annotation from a urlencoded xml content
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    72
## Returns an xml-structured annotation
25
c8dfd7ea87e5 Corrections on merge
wakimd
parents: 24
diff changeset
    73
@oauth_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
    74
@csrf_exempt
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
    75
def create_annotation(request):
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
    76
    cont = request.POST["content"]
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    77
    doc = lxml.etree.fromstring(cont)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    78
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    79
    id_nodes = doc.xpath("/iri/text-annotation/id/text()")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    80
    if id_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    81
        id = unicode(id_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    82
    else:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    83
        id = generate_uuid()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    84
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    85
    uri = unicode(doc.xpath("/iri/text-annotation/uri/text()")[0])
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    86
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    87
    ltags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")]))
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    88
    tags = ",".join(ltags)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    89
    if len(ltags) == 1:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    90
        tags += ","
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    91
    
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    92
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    93
    title_nodes = doc.xpath("/iri/text-annotation/content/title/text()")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    94
    if title_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    95
        title = unicode(title_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    96
    else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    97
        title = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    98
    desc_nodes = doc.xpath("/iri/text-annotation/content/description/text()")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    99
    if desc_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   100
        desc = unicode(desc_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   101
    else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   102
        desc = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   103
    text_nodes = doc.xpath("/iri/text-annotation/content/text/text()")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   104
    if text_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   105
        text = unicode(text_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   106
    else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   107
        text = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   108
    color_nodes = doc.xpath("/iri/text-annotation/content/color/text()")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   109
    if color_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   110
        color = unicode(color_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   111
    else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   112
        color = None
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   113
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   114
    creator_nodes = doc.xpath("/iri/text-annotation/meta/creator/text()")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   115
    if creator_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   116
        creator = unicode(creator_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   117
    else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   118
        creator = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   119
    contributor_nodes = doc.xpath("/iri/text-annotation/meta/contributor/text()")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   120
    if contributor_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   121
        contributor = unicode(contributor_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   122
    else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   123
        contributor = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   124
    
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   125
    #creation_date = unicode(doc.xpath("/iri/text-annotation/meta/created/text()")[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   126
    #update_date = unicode(doc.xpath("/iri/text-annotation/meta/modified/text()")[0])
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   127
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   128
    try:
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   129
        annotation = Annotation.create_annotation(external_id=id, uri=uri, tags=tags, title=title, description=desc, text=text, color=color, creator=creator, contributor=contributor)
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   130
        annotation.save()
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   131
        return HttpResponse(lxml.etree.tostring(annotation.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8")
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   132
    except IntegrityError:
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   133
        return HttpResponse(status=409)
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   134
9
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
## Gets an annotation (from its id)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   138
## Returns the xml-structured annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   139
def get_annotation(request, id):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   140
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   141
        annot = Annotation.objects.get(external_id=request.GET.get('id',''))
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   142
    except Annotation.DoesNotExist:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   143
        raise Http404
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   144
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   145
    doc = annot.serialize()
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   146
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   147
    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
   148
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   149
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   150
## Deletes an annotation (from its id)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   151
## Returns an empty xml-structured annotation
25
c8dfd7ea87e5 Corrections on merge
wakimd
parents: 24
diff changeset
   152
@oauth_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   153
@csrf_exempt
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   154
def delete_annotation(request):
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   155
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   156
        annot = Annotation.objects.get(external_id=request.POST["id"])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   157
        annot.delete()
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   158
    except Annotation.DoesNotExist:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   159
        raise Http404
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   160
    
22
83b28fc0d731 improve on ldt test framework
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   161
    return HttpResponse("")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   162
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   163
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   164
## Updates the content of an annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   165
## Returns the xml-structured updated annotation
25
c8dfd7ea87e5 Corrections on merge
wakimd
parents: 24
diff changeset
   166
@oauth_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   167
@csrf_exempt
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   168
def update_annotation(request):
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   169
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   170
        annot = Annotation.objects.get(external_id=request.POST["id"])
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   171
    except Annotation.DoesNotExist:
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   172
    #except:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   173
        raise Http404
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   174
    
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   175
    cont = request.POST["content"]
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   176
    doc = lxml.etree.fromstring(cont)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   177
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   178
    uri = doc.xpath("/iri/text-annotation/uri/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   179
    if uri != [] and annot.uri != uri[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   180
        annot.uri = unicode(uri[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   181
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   182
    tags_nodes = doc.xpath("/iri/text-annotation/tags")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   183
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   184
    if len(tags_nodes) > 0:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   185
        tags = list(set([unicode(tag.text).lower().strip() for tag in doc.xpath("/iri/text-annotation/tags/tag")]))
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   186
        tags_str = ",".join(tags)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   187
        if len(tags) == 1:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   188
            tags_str += ","
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   189
        annot.tags = tags_str
21
1a061f244254 Pylucene indexation
wakimd
parents: 19
diff changeset
   190
                    
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   191
    title = doc.xpath("/iri/text-annotation/content/title/text()")
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   192
    if title and annot.title != title[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   193
        annot.title = unicode(title[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   194
    desc = doc.xpath("/iri/text-annotation/content/description/text()")
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   195
    if desc and annot.description != desc[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   196
        annot.description = unicode(desc[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   197
    text = doc.xpath("/iri/text-annotation/content/text/text()")
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   198
    if text and annot.text != text[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   199
        annot.text = unicode(text[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   200
    color = doc.xpath("/iri/text-annotation/content/color/text()")
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   201
    if color and annot.color != color[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   202
        annot.color = unicode(color[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   203
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   204
    contributor = doc.xpath("/iri/text-annotation/meta/contributor/text()")
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   205
    if contributor and annot.contributor != contributor[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   206
        annot.contributor = unicode(contributor[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   207
    update_date = doc.xpath("/iri/text-annotation/meta/modified/text()")
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   208
    if update_date and annot.update_date != update_date[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   209
        annot.update_date = unicode(update_date[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   210
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   211
    annot.save()
21
1a061f244254 Pylucene indexation
wakimd
parents: 19
diff changeset
   212
    annot.update_index()
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   213
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   214
    return HttpResponse(lxml.etree.tostring(annot.serialize(), pretty_print=True), mimetype="text/xml;charset=utf-8")
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   215
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   216