web/ldt/text/views.py
author ymh <ymh.work@gmail.com>
Fri, 19 Nov 2010 14:29:17 +0100
changeset 17 683ce4109c28
parent 16 d0f617472760
child 19 7cf81d58a968
permissions -rw-r--r--
various corrections, especially on the model (and tags)
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
9
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:
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    61
        annot.serialize(iri)
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    62
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    63
    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
    64
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    65
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    66
## Creates an annotation from a urlencoded xml content
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    67
## Returns an xml-structured annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    68
#@login_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
    69
@csrf_exempt
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
    70
def create_annotation(request):
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
    71
    cont = request.POST["content"]
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    72
    doc = lxml.etree.fromstring(cont)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    73
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    74
    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
    75
    if id_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    76
        id = unicode(id_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    77
    else:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    78
        id = generate_uuid()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    79
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    80
    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
    81
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    82
    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
    83
    tags = ",".join(ltags)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    84
    if len(ltags) == 1:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    85
        tags += ","
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    86
    
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    87
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    88
    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
    89
    if title_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    90
        title = unicode(title_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    91
    else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    92
        title = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    93
    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
    94
    if desc_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    95
        desc = unicode(desc_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
        desc = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    98
    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
    99
    if text_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   100
        text = unicode(text_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
        text = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   103
    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
   104
    if color_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   105
        color = unicode(color_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
        color = None
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   108
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   109
    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
   110
    if creator_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   111
        creator = unicode(creator_nodes[0])
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   112
    else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   113
        creator = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   114
    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
   115
    if contributor_nodes:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   116
        contributor = unicode(contributor_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
        contributor = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   119
    
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   120
    #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
   121
    #update_date = unicode(doc.xpath("/iri/text-annotation/meta/modified/text()")[0])
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   122
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   123
    try:
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   124
        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
   125
        annotation.save()
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   126
        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
   127
    except IntegrityError:
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   128
        return HttpResponse(status=409)
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   129
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   130
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   131
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   132
## Gets an annotation (from its id)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   133
## Returns the xml-structured annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   134
def get_annotation(request, id):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   135
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   136
        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
   137
    except Annotation.DoesNotExist:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   138
        raise Http404
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   139
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   140
    doc = annot.serialize()
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   141
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   142
    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
   143
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   144
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   145
## Deletes an annotation (from its id)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   146
## Returns an empty xml-structured annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   147
#@login_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   148
@csrf_exempt
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   149
def delete_annotation(request):
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   150
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   151
        annot = Annotation.objects.get(external_id=request.POST["id"])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   152
        annot.delete()
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   153
    except Annotation.DoesNotExist:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   154
        raise Http404
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   155
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   156
    doc=create_empty_annotation()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   157
    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
   158
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   159
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   160
## Updates the content of an annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   161
## Returns the xml-structured updated annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   162
#@login_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   163
@csrf_exempt
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   164
def update_annotation(request):
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   165
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   166
        annot = Annotation.objects.get(external_id=request.POST["id"])
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   167
    except Annotation.DoesNotExist:
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   168
    #except:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   169
        raise Http404
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   170
    
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   171
    cont = request.POST["content"]
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   172
    doc = lxml.etree.fromstring(cont)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   173
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   174
    uri = doc.xpath("/iri/text-annotation/uri/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   175
    if uri != [] and annot.uri != uri[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   176
        annot.uri = unicode(uri[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   177
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   178
    tags_nodes = doc.xpath("/iri/text-annotation/tags")
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   179
    
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   180
    if len(tags_nodes) > 0:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   181
        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
   182
        tags_str = ",".join(tags)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   183
        if len(tags) == 1:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   184
            tags_str += ","
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   185
        annot.tags = tags_str
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   186
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   187
            
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   188
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   189
            
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   190
    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
   191
    if title and annot.title != title[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   192
        annot.title = unicode(title[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   193
    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
   194
    if desc and annot.description != desc[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   195
        annot.description = unicode(desc[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   196
    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
   197
    if text and annot.text != text[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   198
        annot.text = unicode(text[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   199
    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
   200
    if color and annot.color != color[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   201
        annot.color = unicode(color[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   202
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   203
    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
   204
    if contributor and annot.contributor != contributor[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   205
        annot.contributor = unicode(contributor[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   206
    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
   207
    if update_date and annot.update_date != update_date[0]:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   208
        annot.update_date = unicode(update_date[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   209
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   210
    annot.save()
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   211
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   212
    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
   213
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   214