web/ldt/text/views.py
author ymh <ymh.work@gmail.com>
Wed, 17 Nov 2010 10:28:55 +0100
changeset 15 37e051f2264d
parent 10 000f3ca19eaa
child 16 d0f617472760
permissions -rw-r--r--
csrf protection unplug
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 base64
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    27
import cgi
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    28
import django.core.urlresolvers
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    29
import ldt.auth as ldt_auth
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    30
import ldt.utils.path as ldt_utils_path
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    31
import logging
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    32
import lucene
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    33
import tempfile
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    34
import uuid
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
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   104
def create_annotation(request, content):
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   105
    #cont = base64.urlsafe_b64decode(str(request.POST["content"]))
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   106
    cont = str(request.POST["content"])
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   107
    doc = lxml.etree.fromstring(cont)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   108
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   109
    id = unicode(doc.xpath("/iri/text-annotation/id/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   110
    if id is None:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   111
        id = generate_uuid()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   112
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   113
    uri = unicode(doc.xpath("/iri/text-annotation/uri/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   114
    ltags = []
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   115
    for tag in doc.xpath("/iri/text-annotation/tags/tag/text()"):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   116
        ltags.append(unicode(tag))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   117
    tags=normalize_tags(ltags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   118
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   119
    title = unicode(doc.xpath("/iri/text-annotation/content/title/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   120
    desc = unicode(doc.xpath("/iri/text-annotation/content/description/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   121
    text = unicode(doc.xpath("/iri/text-annotation/content/text/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   122
    color = unicode(doc.xpath("/iri/text-annotation/content/color/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   123
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   124
    creator = unicode(doc.xpath("/iri/text-annotation/meta/creator/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   125
    contributor = unicode(doc.xpath("/iri/text-annotation/meta/contributor/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   126
    creation_date = unicode(doc.xpath("/iri/text-annotation/meta/created/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   127
    update_date = unicode(doc.xpath("/iri/text-annotation/meta/modified/text()")[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   128
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   129
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   130
        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
   131
        annotation.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   132
        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
   133
        #return doc
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   134
    except IntegrityError:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   135
    #except Annotation.IntegrityError:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   136
        #print 'This id is already used! Please choose another one!'
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   137
        return HttpResponse(status=409)
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   138
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   139
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   140
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   141
## Gets an annotation (from its id)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   142
## Returns the xml-structured annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   143
def get_annotation(request, id):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   144
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   145
        annot = Annotation.objects.get(external_id=request.GET.get('id',''))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   146
    except:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   147
    #except Annotation.DoesNotExist:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   148
        raise Http404
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   149
    iri = lxml.etree.Element('iri')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   150
    doc = lxml.etree.ElementTree(iri)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   151
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   152
    textannotation = lxml.etree.SubElement(iri, 'text-annotation')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   153
    id = lxml.etree.SubElement(textannotation,'id')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   154
    id.text = annot.external_id
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   155
    uri = lxml.etree.SubElement(textannotation,'uri')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   156
    uri.text = annot.uri
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   157
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   158
    if annot.tags:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   159
        if type(annot.tags) is unicode:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   160
            annot.tags = eval(annot.tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   161
        tags = lxml.etree.SubElement(textannotation,'tags')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   162
        ltags = normalize_tags(annot.tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   163
        for t in ltags:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   164
            tag = lxml.etree.SubElement(tags, 'tag')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   165
            tag.text = t
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   166
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   167
    content = lxml.etree.SubElement(textannotation,'content')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   168
    color = lxml.etree.SubElement(content,'color')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   169
    color.text = annot.color
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   170
    description = lxml.etree.SubElement(content,'description')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   171
    description.text = annot.description
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   172
    title = lxml.etree.SubElement(content,'title')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   173
    title.text = annot.title
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   174
    text = lxml.etree.SubElement(content,'text')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   175
    text.text = annot.text
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   176
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   177
    meta = lxml.etree.SubElement(textannotation,'meta')
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   178
    contributor = lxml.etree.SubElement(meta, "contributor")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   179
    contributor.text = annot.contributor
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   180
    creator = lxml.etree.SubElement(meta, "creator")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   181
    creator.text = annot.creator
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   182
    creationdate = lxml.etree.SubElement(meta, "created")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   183
    creationdate.text = str(annot.creation_date)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   184
    updatedate = lxml.etree.SubElement(meta, "modified")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   185
    updatedate.text = str(annot.update_date)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   186
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   187
    #return doc
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   188
    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
   189
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   190
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   191
## Deletes an annotation (from its id)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   192
## Returns an empty xml-structured annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   193
#@login_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   194
@csrf_exempt
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   195
def delete_annotation(request, id):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   196
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   197
        annot = Annotation.objects.get(external_id=request.POST["id"])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   198
        annot.delete()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   199
    #except Annotation.DoesNotExist:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   200
    except:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   201
        raise Http404
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   202
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   203
    doc=create_empty_annotation()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   204
    #return doc
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   205
    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
   206
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   207
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   208
## Updates the content of an annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   209
## Returns the xml-structured updated annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   210
#@login_required
15
37e051f2264d csrf protection unplug
ymh <ymh.work@gmail.com>
parents: 10
diff changeset
   211
@csrf_exempt
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   212
def update_annotation(request, content, id):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   213
    try:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   214
        annot = Annotation.objects.get(external_id=request.POST["id"])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   215
    #except Annotation.DoesNotExist:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   216
    except:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   217
        raise Http404
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   218
    
10
000f3ca19eaa Added 404 templates + some views corrections
wakimd
parents: 9
diff changeset
   219
    cont = base64.urlsafe_b64decode(str(request.POST["content"]))
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   220
    doc = lxml.etree.fromstring(cont)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   221
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   222
    uri = doc.xpath("/iri/text-annotation/uri/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   223
    if uri != [] and annot.uri != uri[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   224
        annot.uri = unicode(uri[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   225
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   226
    tags = []
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   227
    for tag in doc.xpath("/iri/text-annotation/tags/tag"):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   228
        tags.append(unicode(tag.text))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   229
    if annot.tags is not None:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   230
        if type(annot.tags) is unicode:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   231
            annot.tags = eval(annot.tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   232
        tags += annot.tags
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   233
    tags = normalize_tags(tags)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   234
    annot.tags=tags
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   235
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   236
    title = doc.xpath("/iri/text-annotation/content/title/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   237
    if title != [] and annot.title != title[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   238
        annot.title = unicode(title[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   239
    desc = doc.xpath("/iri/text-annotation/content/description/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   240
    if desc != [] and annot.description != desc[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   241
        annot.description = unicode(desc[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   242
    text = doc.xpath("/iri/text-annotation/content/text/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   243
    if text != [] and annot.text != text[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   244
        annot.text = unicode(text[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   245
    color = doc.xpath("/iri/text-annotation/content/color/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   246
    if color != [] and annot.color != color[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   247
        annot.color = unicode(color[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   248
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   249
    contributor = doc.xpath("/iri/text-annotation/meta/contributor/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   250
    if contributor != [] and annot.contributor != contributor[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   251
        annot.contributor = unicode(contributor[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   252
    update_date = doc.xpath("/iri/text-annotation/meta/modified/text()")
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   253
    if update_date != [] and annot.update_date != update_date[0]:
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   254
        annot.update_date = unicode(update_date[0])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   255
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   256
    annot.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   257
    
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   258
    return get_annotation(id)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   259