web/ldt/text/views.py
author wakimd
Wed, 17 Nov 2010 18:57:34 +0100
changeset 16 d0f617472760
parent 15 37e051f2264d
child 17 683ce4109c28
permissions -rw-r--r--
Added tests on server + some corrections on views
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
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
    34
from urllib import urlopen
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
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
    if request.GET.get('filter') and len(request.GET.get('filter')) > 0:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    48
        query &= Q(text__icontains=request.GET.get('filter'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    49
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    50
    annotlist = Annotation.objects.filter(query)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    51
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    52
    if request.GET.get('limit'):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    53
        nb = request.GET.get('limit')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    54
        #offset = request.GET.get('limit')[1]
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    55
        annotlist = annotlist[:nb]
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    56
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    57
    #create xml
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    58
    iri = lxml.etree.Element('iri')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    59
    doc = lxml.etree.ElementTree(iri)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    60
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    61
    for annot in annotlist:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    62
        textannotation = lxml.etree.SubElement(iri, 'text-annotation')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    63
        id = lxml.etree.SubElement(textannotation,'id')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    64
        id.text = annot.external_id
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    65
        uri = lxml.etree.SubElement(textannotation,'uri')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    66
        uri.text = annot.uri
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    67
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    68
        if annot.tags:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    69
            if type(annot.tags) is unicode:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    70
                annot.tags = eval(annot.tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    71
            tags = lxml.etree.SubElement(textannotation,'tags')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    72
            ltags = normalize_tags(annot.tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    73
            for t in ltags:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    74
                tag = lxml.etree.SubElement(tags, 'tag')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    75
                tag.text = t
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    76
            
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    77
        content = lxml.etree.SubElement(textannotation,'content')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    78
        color = lxml.etree.SubElement(content,'color')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    79
        color.text = annot.color
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    80
        description = lxml.etree.SubElement(content,'description')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    81
        description.text = annot.description
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    82
        title = lxml.etree.SubElement(content,'title')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    83
        title.text = annot.title
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    84
        text = lxml.etree.SubElement(content,'text')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    85
        text.text = annot.text
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    86
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    87
        meta = lxml.etree.SubElement(textannotation,'meta')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    88
        contributor = lxml.etree.SubElement(meta, "contributor")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    89
        contributor.text = annot.contributor
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    90
        creator = lxml.etree.SubElement(meta, "contributor")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    91
        creator.text = annot.creator
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    92
        creationdate = lxml.etree.SubElement(meta, "created")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    93
        creationdate.text = str(annot.creation_date)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    94
        updatedate = lxml.etree.SubElement(meta, "modified")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    95
        updatedate.text = str(annot.update_date)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    96
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    97
    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
    98
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    99
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   100
## Creates an annotation from a urlencoded xml content
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   101
## Returns an xml-structured annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   102
#@login_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   103
@csrf_exempt
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   104
def create_annotation(request):
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   105
    cont = request.POST["content"]
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   106
    doc = lxml.etree.fromstring(cont)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   107
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   108
    id = unicode(doc.xpath("/iri/text-annotation/id/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   109
    if id is None:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   110
        id = generate_uuid()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   111
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   112
    uri = unicode(doc.xpath("/iri/text-annotation/uri/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   113
    ltags = []
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   114
    for tag in doc.xpath("/iri/text-annotation/tags/tag/text()"):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   115
        ltags.append(unicode(tag))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   116
    tags=normalize_tags(ltags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   117
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   118
    title = unicode(doc.xpath("/iri/text-annotation/content/title/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   119
    desc = unicode(doc.xpath("/iri/text-annotation/content/description/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   120
    text = unicode(doc.xpath("/iri/text-annotation/content/text/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   121
    color = unicode(doc.xpath("/iri/text-annotation/content/color/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   122
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   123
    creator = unicode(doc.xpath("/iri/text-annotation/meta/creator/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   124
    contributor = unicode(doc.xpath("/iri/text-annotation/meta/contributor/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   125
    creation_date = unicode(doc.xpath("/iri/text-annotation/meta/created/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   126
    update_date = unicode(doc.xpath("/iri/text-annotation/meta/modified/text()")[0])
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:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
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, creation_date=creation_date, update_date=update_date)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   130
        annotation.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   131
        return HttpResponse(lxml.etree.tostring(doc, 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
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   144
    iri = lxml.etree.Element('iri')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   145
    doc = lxml.etree.ElementTree(iri)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   146
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   147
    textannotation = lxml.etree.SubElement(iri, 'text-annotation')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   148
    id = lxml.etree.SubElement(textannotation,'id')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   149
    id.text = annot.external_id
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   150
    uri = lxml.etree.SubElement(textannotation,'uri')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   151
    uri.text = annot.uri
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   152
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   153
    if annot.tags:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   154
        if type(annot.tags) is unicode:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   155
            annot.tags = eval(annot.tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   156
        tags = lxml.etree.SubElement(textannotation,'tags')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   157
        ltags = normalize_tags(annot.tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   158
        for t in ltags:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   159
            tag = lxml.etree.SubElement(tags, 'tag')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   160
            tag.text = t
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   161
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   162
    content = lxml.etree.SubElement(textannotation,'content')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   163
    color = lxml.etree.SubElement(content,'color')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   164
    color.text = annot.color
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   165
    description = lxml.etree.SubElement(content,'description')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   166
    description.text = annot.description
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   167
    title = lxml.etree.SubElement(content,'title')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   168
    title.text = annot.title
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   169
    text = lxml.etree.SubElement(content,'text')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   170
    text.text = annot.text
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   171
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   172
    meta = lxml.etree.SubElement(textannotation,'meta')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   173
    contributor = lxml.etree.SubElement(meta, "contributor")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   174
    contributor.text = annot.contributor
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   175
    creator = lxml.etree.SubElement(meta, "creator")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   176
    creator.text = annot.creator
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   177
    creationdate = lxml.etree.SubElement(meta, "created")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   178
    creationdate.text = str(annot.creation_date)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   179
    updatedate = lxml.etree.SubElement(meta, "modified")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   180
    updatedate.text = str(annot.update_date)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   181
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   182
    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
   183
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   184
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   185
## Deletes an annotation (from its id)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   186
## Returns an empty xml-structured annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   187
#@login_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   188
@csrf_exempt
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   189
def delete_annotation(request):
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   190
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   191
        annot = Annotation.objects.get(external_id=request.POST["id"])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   192
        annot.delete()
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   193
    except Annotation.DoesNotExist:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   194
        raise Http404
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   195
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   196
    doc=create_empty_annotation()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   197
    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
   198
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   199
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   200
## Updates the content of an annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   201
## Returns the xml-structured updated annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   202
#@login_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   203
@csrf_exempt
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   204
def update_annotation(request):
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   205
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   206
        annot = Annotation.objects.get(external_id=request.POST["id"])
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   207
    except Annotation.DoesNotExist:
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   208
    #except:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   209
        raise Http404
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   210
    
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   211
    cont = request.POST["content"]
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   212
    doc = lxml.etree.fromstring(cont)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   213
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   214
    uri = doc.xpath("/iri/text-annotation/uri/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   215
    if uri != [] and annot.uri != uri[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   216
        annot.uri = unicode(uri[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   217
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   218
    tags = []
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   219
    for tag in doc.xpath("/iri/text-annotation/tags/tag"):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   220
        tags.append(unicode(tag.text))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   221
    if annot.tags is not None:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   222
        if type(annot.tags) is unicode:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   223
            annot.tags = eval(annot.tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   224
        tags += annot.tags
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   225
    tags = normalize_tags(tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   226
    annot.tags=tags
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   227
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   228
    title = doc.xpath("/iri/text-annotation/content/title/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   229
    if title != [] and annot.title != title[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   230
        annot.title = unicode(title[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   231
    desc = doc.xpath("/iri/text-annotation/content/description/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   232
    if desc != [] and annot.description != desc[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   233
        annot.description = unicode(desc[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   234
    text = doc.xpath("/iri/text-annotation/content/text/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   235
    if text != [] and annot.text != text[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   236
        annot.text = unicode(text[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   237
    color = doc.xpath("/iri/text-annotation/content/color/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   238
    if color != [] and annot.color != color[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   239
        annot.color = unicode(color[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   240
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   241
    contributor = doc.xpath("/iri/text-annotation/meta/contributor/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   242
    if contributor != [] and annot.contributor != contributor[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   243
        annot.contributor = unicode(contributor[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   244
    update_date = doc.xpath("/iri/text-annotation/meta/modified/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   245
    if update_date != [] and annot.update_date != update_date[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   246
        annot.update_date = unicode(update_date[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   247
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   248
    annot.save()
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   249
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   250
    #create xml
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   251
    iri = lxml.etree.Element('iri')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   252
    doc2 = lxml.etree.ElementTree(iri)
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   253
    
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   254
    textannotation = lxml.etree.SubElement(iri, 'text-annotation')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   255
    id = lxml.etree.SubElement(textannotation,'id')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   256
    id.text = annot.external_id
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   257
    uri = lxml.etree.SubElement(textannotation,'uri')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   258
    uri.text = annot.uri
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   259
    
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   260
    if annot.tags:
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   261
        if type(annot.tags) is unicode:
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   262
            annot.tags = eval(annot.tags)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   263
        tags = lxml.etree.SubElement(textannotation,'tags')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   264
        ltags = normalize_tags(annot.tags)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   265
        for t in ltags:
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   266
            tag = lxml.etree.SubElement(tags, 'tag')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   267
            tag.text = t
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   268
    
16
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   269
    content = lxml.etree.SubElement(textannotation,'content')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   270
    color = lxml.etree.SubElement(content,'color')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   271
    color.text = annot.color
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   272
    description = lxml.etree.SubElement(content,'description')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   273
    description.text = annot.description
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   274
    title = lxml.etree.SubElement(content,'title')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   275
    title.text = annot.title
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   276
    text = lxml.etree.SubElement(content,'text')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   277
    text.text = annot.text
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   278
    
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   279
    meta = lxml.etree.SubElement(textannotation,'meta')
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   280
    contributor = lxml.etree.SubElement(meta, "contributor")
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   281
    contributor.text = annot.contributor
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   282
    creator = lxml.etree.SubElement(meta, "creator")
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   283
    creator.text = annot.creator
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   284
    creationdate = lxml.etree.SubElement(meta, "created")
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   285
    creationdate.text = str(annot.creation_date)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   286
    updatedate = lxml.etree.SubElement(meta, "modified")
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   287
    updatedate.text = str(annot.update_date)
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   288
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   289
    return HttpResponse(lxml.etree.tostring(doc2, pretty_print=True), mimetype="text/xml;charset=utf-8")
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   290
d0f617472760 Added tests on server + some corrections on views
wakimd
parents: 15
diff changeset
   291