| author | wakimd |
| Wed, 22 Dec 2010 12:01:05 +0100 | |
| changeset 25 | c8dfd7ea87e5 |
| parent 24 | 9e19b7ae3780 |
| permissions | -rw-r--r-- |
| 9 | 1 |
from django.conf import settings |
2 |
from django.db import models |
|
3 |
from django.utils.translation import ugettext_lazy as _ |
|
4 |
from ldt.core.models import Document, Owner |
|
5 |
from django.contrib.auth.models import User |
|
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 | 8 |
from utils import generate_uuid |
9 |
import os.path |
|
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 | 12 |
import lucene |
13 |
from ldt.ldt_utils import STORE, ANALYZER |
|
14 |
from annotindexer import AnnotIndexer |
|
| 9 | 15 |
#from django.core.management.validation import max_length |
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 | 20 |
|
21 |
class Annotation(models.Model): |
|
22 |
external_id = models.CharField(max_length=1024, null=False, unique=True, default=generate_uuid, verbose_name=_('annotation.external_id')) |
|
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 | 25 |
title = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('annotation.title')) |
26 |
description = models.TextField(null=True, blank=True, verbose_name=_('annotation.description')) |
|
27 |
text = models.TextField(null=True, blank=True, verbose_name=_('annotation.text')) |
|
28 |
color = models.CharField(max_length=1024, verbose_name=_('annotation.color')) |
|
29 |
creator = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('creator.title')) |
|
30 |
contributor = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('contributor.title')) |
|
31 |
creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('annotation.creation_date')) |
|
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 | 42 |
if isinstance(value, (list,tuple)): |
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 | 70 |
|
71 |
||
72 |
def __unicode__(self): |
|
73 |
return unicode(self.external_id) + u": " + unicode(self.title) |
|
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 | 122 |
@staticmethod |
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): |
|
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) |
|
125 |
annotation.save() |
|
| 21 | 126 |
annotation.index_annot() |
| 9 | 127 |
|
128 |
return annotation |
|
129 |
||
| 21 | 130 |
|
131 |
def delete(self): |
|
132 |
super(Annotation, self).delete() |
|
133 |
lucene.getVMEnv().attachCurrentThread() |
|
134 |
writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED) |
|
135 |
writer.deleteDocuments(lucene.Term("external_id", self.external_id)) |
|
136 |
writer.close() |
|
137 |
||
138 |
def index_annot(self): |
|
139 |
lucene.getVMEnv().attachCurrentThread() |
|
140 |
writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED) |
|
141 |
annotl = [self,] |
|
142 |
indexer = AnnotIndexer(annotl,writer) |
|
143 |
indexer.index_all() |
|
144 |
writer.close() |
|
145 |
||
146 |
def update_index(self): |
|
147 |
lucene.getVMEnv().attachCurrentThread() |
|
148 |
writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED) |
|
149 |
writer.deleteDocuments(lucene.Term("external_id", self.external_id)) |
|
150 |
writer.close() |
|
151 |
self.index_annot() |
|
152 |
||
153 |