| 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-- |
| 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 |
| 9 | 12 |
#from django.core.management.validation import max_length |
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 | 17 |
|
18 |
class Annotation(models.Model): |
|
19 |
external_id = models.CharField(max_length=1024, null=False, unique=True, default=generate_uuid, verbose_name=_('annotation.external_id')) |
|
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 | 22 |
title = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('annotation.title')) |
23 |
description = models.TextField(null=True, blank=True, verbose_name=_('annotation.description')) |
|
24 |
text = models.TextField(null=True, blank=True, verbose_name=_('annotation.text')) |
|
25 |
color = models.CharField(max_length=1024, verbose_name=_('annotation.color')) |
|
26 |
creator = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('creator.title')) |
|
27 |
contributor = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('contributor.title')) |
|
28 |
creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('annotation.creation_date')) |
|
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 | 67 |
|
68 |
||
69 |
def __unicode__(self): |
|
70 |
return unicode(self.external_id) + u": " + unicode(self.title) |
|
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 | 119 |
@staticmethod |
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): |
|
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) |
|
122 |
annotation.save() |
|
123 |
||
124 |
return annotation |
|
125 |