1 from django.db import models |
1 from django.db import models |
2 from django.conf import settings |
2 from django.conf import settings |
3 from ldt.core.models import Document, Owner |
3 from ldt.core.models import Document, Owner |
4 from django.utils.translation import ugettext_lazy as _ |
4 from django.utils.translation import ugettext_lazy as _ |
5 from utils import create_ldt, copy_ldt |
5 from utils import create_ldt, copy_ldt, create_iri |
|
6 import os |
|
7 import os.path |
6 import uuid |
8 import uuid |
7 |
9 |
8 class Author(models.Model): |
10 class Author(models.Model): |
9 |
11 |
10 handle = models.CharField(max_length=512, unique=True, blank=True, null=True) |
12 handle = models.CharField(max_length=512, unique=True, blank=True, null=True) |
40 |
42 |
41 def delete(self): |
43 def delete(self): |
42 super(Content, self).delete() |
44 super(Content, self).delete() |
43 writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED) |
45 writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED) |
44 writer.deleteDocuments(lucene.Term("iri_id", self.iri_id)) |
46 writer.deleteDocuments(lucene.Term("iri_id", self.iri_id)) |
45 writer.commit() |
47 writer.commit() |
|
48 |
|
49 def save(self): |
|
50 # create iri file if needed |
|
51 iri_file_path = self.iri_file_path() |
|
52 if not os.path.exists(iri_file_path): |
|
53 dir = os.path.dirname(iri_file_path) |
|
54 if not os.path.exists(dir): |
|
55 os.makedirs(dir) |
|
56 file = open(iri_file_path,"w") |
|
57 create_iri(file, self, "IRI") |
|
58 # update it |
|
59 super(Content, self).save() |
46 |
60 |
47 def __unicode__(self): |
61 def __unicode__(self): |
48 return str(self.id) + ": " + self.iri_id |
62 return str(self.id) + ": " + self.iri_id |
49 |
63 |
50 def iri_url(self, web_url=settings.WEB_URL): |
64 def iri_url(self, web_url=settings.WEB_URL): |
51 if 'http' in self.iriurl or 'https' in self.iriurl: |
65 if 'http' in self.iriurl or 'https' in self.iriurl: |
52 return self.iriurl |
66 return self.iriurl |
53 else: |
67 else: |
54 return unicode(web_url) + unicode(settings.MEDIA_URL)+u"media/ldt/"+unicode(self.iriurl) |
68 return unicode(web_url) + unicode(settings.MEDIA_URL)+u"media/ldt/"+unicode(self.iriurl) |
|
69 |
|
70 def iri_file_path(self): |
|
71 return os.path.join(os.path.join(os.path.join(os.path.join(settings.MEDIA_ROOT, "media"), "ldt"), self.iri_id), os.path.basename(self.iri.path)) |
|
72 |
|
73 def iri_url_template(self): |
|
74 return "${web_url}${media_url}media/ldt/" + unicode(self.iri_id) + "/" + os.path.basename(self.iri.path) |
55 |
75 |
56 |
76 |
57 class Project(Document): |
77 class Project(Document): |
58 STATE_CHOICES=( |
78 STATE_CHOICES=( |
59 (1, 'edition'), |
79 (1, 'edition'), |
72 changed_by = models.CharField(_("changed by"), max_length=70) |
92 changed_by = models.CharField(_("changed by"), max_length=70) |
73 state = models.IntegerField(choices=STATE_CHOICES, default=1) |
93 state = models.IntegerField(choices=STATE_CHOICES, default=1) |
74 |
94 |
75 def __unicode__(self): |
95 def __unicode__(self): |
76 return unicode(self.id) + u": " + unicode(self.ldt_id) |
96 return unicode(self.id) + u": " + unicode(self.ldt_id) |
|
97 |
|
98 def get_description(self, doc=None): |
|
99 |
|
100 if doc is None: |
|
101 doc = xml.dom.minidom.parseString(self.ldt) |
|
102 doc = Ft.Xml.Domlette.ConvertDocument(doc) |
|
103 |
|
104 con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
105 res = xml.xpath.Evaluate("/iri/project", context=con) |
|
106 if len(res) > 0: |
|
107 return res[0].getAttributeNS(None, 'abstract') |
|
108 else: |
|
109 return None |
|
110 |
77 |
111 |
78 @staticmethod |
112 @staticmethod |
79 def create_project(user, title, contents): |
113 def create_project(user, title, contents): |
80 owner = Owner.objects.get(user=user) |
114 owner = Owner.objects.get(user=user) |
81 project = Project(title=title, owner=owner) |
115 project = Project(title=title, owner=owner) |