web/ldt/text/models.py
author wakimd
Wed, 22 Dec 2010 12:01:05 +0100
changeset 25 c8dfd7ea87e5
parent 24 9e19b7ae3780
permissions -rw-r--r--
Corrections on merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     1
from django.conf import settings
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     2
from django.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
21
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
    12
import lucene
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
    13
from ldt.ldt_utils import STORE, ANALYZER
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
    14
from annotindexer import AnnotIndexer
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    15
#from django.core.management.validation import max_length
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    16
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    17
def Property(func):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    18
    return property(**func()) 
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    19
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    20
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    21
class Annotation(models.Model):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    22
    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
    23
    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
    24
    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
    25
    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
    26
    description = models.TextField(null=True, blank=True, verbose_name=_('annotation.description'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    27
    text = models.TextField(null=True, blank=True, verbose_name=_('annotation.text'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    28
    color = models.CharField(max_length=1024, verbose_name=_('annotation.color'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    29
    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
    30
    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
    31
    creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('annotation.creation_date'))
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    32
    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
    33
    
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    34
    @Property
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    35
    def tags():
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 fget(self):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    38
            return ",".join(self.tag_list)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    39
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    40
        def fset(self, value):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    41
            values = None
22
83b28fc0d731 improve on ldt test framework
ymh <ymh.work@gmail.com>
parents: 17
diff changeset
    42
            if isinstance(value, (list,tuple)):
83b28fc0d731 improve on ldt test framework
ymh <ymh.work@gmail.com>
parents: 17
diff changeset
    43
                values = list(value)
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    44
            elif value is not None:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    45
                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
    46
            
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    47
            if values is not None:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    48
                self.tags_field = ",".join(values) + ","
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    49
            else:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    50
                self.tags_field = None
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    51
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    52
        return locals()
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    53
    
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    54
    @Property
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    55
    def tag_list():
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    56
        def fget(self):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    57
            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
    58
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    59
        return locals()
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    60
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    61
    
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    62
    def get_tag_list(self):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    63
        tags = []
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    64
        if self.tags:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    65
            tags_list = unicode(self.tags)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    66
            for t in tags_list.split(","):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    67
                tags.append(t.strip()) 
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    68
        return tags
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    69
        #return self.tags
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    70
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    71
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    72
    def __unicode__(self):
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    73
        return unicode(self.external_id) + u": " + unicode(self.title)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    74
17
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    75
    def serialize(self, root_element=None):
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    76
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    77
        if root_element is not None:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    78
            iri = root_element
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    79
        else :
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    80
            iri = lxml.etree.Element('iri')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    81
            doc = lxml.etree.ElementTree(iri)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    82
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    83
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    84
        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
    85
        id = lxml.etree.SubElement(textannotation,'id')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    86
        id.text = self.external_id
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    87
        uri = lxml.etree.SubElement(textannotation,'uri')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    88
        uri.text = self.uri
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    89
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    90
        if self.tags:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    91
            tags = lxml.etree.SubElement(textannotation, 'tags')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    92
            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
    93
                tag = lxml.etree.SubElement(tags, 'tag')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    94
                tag.text = t
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    95
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    96
        content = lxml.etree.SubElement(textannotation,'content')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    97
        color = lxml.etree.SubElement(content,'color')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    98
        color.text = self.color
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    99
        description = lxml.etree.SubElement(content,'description')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   100
        description.text = self.description
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   101
        title = lxml.etree.SubElement(content,'title')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   102
        title.text = self.title
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   103
        text = lxml.etree.SubElement(content,'text')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   104
        text.text = self.text
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   105
        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   106
        meta = lxml.etree.SubElement(textannotation,'meta')
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   107
        contributor = lxml.etree.SubElement(meta, "contributor")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   108
        contributor.text = self.contributor
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   109
        creator = lxml.etree.SubElement(meta, "creator")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   110
        creator.text = self.creator
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   111
        creationdate = lxml.etree.SubElement(meta, "created")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   112
        creationdate.text = str(self.creation_date)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   113
        updatedate = lxml.etree.SubElement(meta, "modified")
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   114
        updatedate.text = str(self.update_date)
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   115
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   116
        if root_element is not None:
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   117
            return root_element
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   118
        else:        
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   119
            return doc
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   120
683ce4109c28 various corrections, especially on the model (and tags)
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   121
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   122
    @staticmethod
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   123
    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
   124
        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
   125
        annotation.save()
21
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   126
        annotation.index_annot()
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   127
        
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   128
        return annotation
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   129
21
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   130
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   131
    def delete(self):
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   132
        super(Annotation, self).delete()
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   133
        lucene.getVMEnv().attachCurrentThread()
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   134
        writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED)
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   135
        writer.deleteDocuments(lucene.Term("external_id", self.external_id))
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   136
        writer.close()
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   137
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   138
    def index_annot(self):
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   139
        lucene.getVMEnv().attachCurrentThread()
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   140
        writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED)
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   141
        annotl = [self,]
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   142
        indexer = AnnotIndexer(annotl,writer)
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   143
        indexer.index_all()
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   144
        writer.close()
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   145
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   146
    def update_index(self):
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   147
        lucene.getVMEnv().attachCurrentThread()
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   148
        writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED)
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   149
        writer.deleteDocuments(lucene.Term("external_id", self.external_id))
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   150
        writer.close()
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   151
        self.index_annot()
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   152
        
1a061f244254 Pylucene indexation
wakimd
parents: 17
diff changeset
   153