web/ldt/text/models.py
author ymh <ymh.work@gmail.com>
Fri, 19 Nov 2010 14:29:17 +0100
changeset 17 683ce4109c28
parent 9 22ab430e9b64
child 21 1a061f244254
child 22 83b28fc0d731
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.db import models
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     3
from django.utils.translation import ugettext_lazy as _
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     4
from ldt.core.models import Document, Owner
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     5
from django.contrib.auth.models import User
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     6
import tagging.fields
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
     7
from tagging.models import Tag
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     8
from utils import generate_uuid
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     9
import os.path
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    10
import uuid
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    11
import lxml
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    12
#from django.core.management.validation import max_length
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    13
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    14
def Property(func):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    15
    return property(**func()) 
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    16
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    17
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    18
class Annotation(models.Model):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    19
    external_id = models.CharField(max_length=1024, null=False, unique=True, default=generate_uuid, verbose_name=_('annotation.external_id'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    20
    uri = models.CharField(max_length=1024, verbose_name=_('annotation.uri'))
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    21
    tags_field = tagging.fields.TagField(max_length=2048, null=True, blank=True, verbose_name=_('annotation.tags'))
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    22
    title = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('annotation.title'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    23
    description = models.TextField(null=True, blank=True, verbose_name=_('annotation.description'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    24
    text = models.TextField(null=True, blank=True, verbose_name=_('annotation.text'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    25
    color = models.CharField(max_length=1024, verbose_name=_('annotation.color'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    26
    creator = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('creator.title'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    27
    contributor = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('contributor.title'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    28
    creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('annotation.creation_date'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    29
    update_date = models.DateTimeField(auto_now=True, verbose_name=_('annotation.update_date'))
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    30
    
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    31
    @Property
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    32
    def tags():
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    33
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    34
        def fget(self):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    35
            return ",".join(self.tag_list)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    36
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    37
        def fset(self, value):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    38
            values = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    39
            if type(value) == type([]):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    40
                values = value
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    41
            elif value is not None:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    42
                values = [v.lower().strip() for v in unicode(value).split(",")]
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    43
            
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    44
            if values is not None:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    45
                self.tags_field = ",".join(values) + ","
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    46
            else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    47
                self.tags_field = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    48
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    49
        return locals()
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    50
    
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    51
    @Property
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    52
    def tag_list():
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    53
        def fget(self):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    54
            return [t.name for t in Tag.objects.get_for_object(self)]
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    55
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    56
        return locals()
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    57
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    58
    
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    59
    def get_tag_list(self):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    60
        tags = []
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    61
        if self.tags:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    62
            tags_list = unicode(self.tags)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    63
            for t in tags_list.split(","):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    64
                tags.append(t.strip()) 
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    65
        return tags
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    66
        #return self.tags
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    67
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    68
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    69
    def __unicode__(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    70
        return unicode(self.external_id) + u": " + unicode(self.title)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    71
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    72
    def serialize(self, root_element=None):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    73
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    74
        if root_element is not None:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    75
            iri = root_element
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    76
        else :
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    77
            iri = lxml.etree.Element('iri')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    78
            doc = lxml.etree.ElementTree(iri)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    79
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    80
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    81
        textannotation = lxml.etree.SubElement(iri, 'text-annotation')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    82
        id = lxml.etree.SubElement(textannotation,'id')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    83
        id.text = self.external_id
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    84
        uri = lxml.etree.SubElement(textannotation,'uri')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    85
        uri.text = self.uri
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    86
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    87
        if self.tags:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    88
            tags = lxml.etree.SubElement(textannotation, 'tags')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    89
            for t in self.get_tag_list():
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    90
                tag = lxml.etree.SubElement(tags, 'tag')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    91
                tag.text = t
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    92
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    93
        content = lxml.etree.SubElement(textannotation,'content')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    94
        color = lxml.etree.SubElement(content,'color')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    95
        color.text = self.color
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    96
        description = lxml.etree.SubElement(content,'description')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    97
        description.text = self.description
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    98
        title = lxml.etree.SubElement(content,'title')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    99
        title.text = self.title
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   100
        text = lxml.etree.SubElement(content,'text')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   101
        text.text = self.text
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   102
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   103
        meta = lxml.etree.SubElement(textannotation,'meta')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   104
        contributor = lxml.etree.SubElement(meta, "contributor")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   105
        contributor.text = self.contributor
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   106
        creator = lxml.etree.SubElement(meta, "creator")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   107
        creator.text = self.creator
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   108
        creationdate = lxml.etree.SubElement(meta, "created")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   109
        creationdate.text = str(self.creation_date)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   110
        updatedate = lxml.etree.SubElement(meta, "modified")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   111
        updatedate.text = str(self.update_date)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   112
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   113
        if root_element is not None:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   114
            return root_element
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   115
        else:        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   116
            return doc
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   117
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   118
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   119
    @staticmethod
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   120
    def create_annotation(external_id, uri=None, tags=None, title=None, description=None, text=None, color=None, creator=None, contributor=None, creation_date=None, update_date=None):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   121
        annotation = Annotation(external_id=external_id, uri=uri, tags=tags, title=title, description=description, 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
   122
        annotation.save()
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   123
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   124
        return annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   125