|
1 from django.db import models |
|
2 from django.conf import settings |
|
3 from ldt.core.models import Document, Owner |
|
4 from django.utils.translation import ugettext_lazy as _ |
|
5 from utils import create_ldt, copy_ldt, create_empty_iri |
|
6 import os |
|
7 import os.path |
|
8 import uuid |
|
9 import xml |
|
10 |
|
11 class Author(models.Model): |
|
12 |
|
13 handle = models.CharField(max_length=512, unique=True, blank=True, null=True) |
|
14 email = models.EmailField(unique=False, blank=True, null=True) |
|
15 firstname = models.CharField(max_length=512, blank=True, null=True) |
|
16 lastname = models.CharField(max_length=512, blank=True, null=True) |
|
17 |
|
18 def __unicode__(self): |
|
19 return unicode(self.id) + " - " + self.handle + ", " + self.email + ", " + self.firstname + " " + self.lastname |
|
20 |
|
21 |
|
22 class Content(models.Model): |
|
23 iri_id = models.CharField(max_length=1024, unique=True) |
|
24 iriurl = models.CharField(max_length=1024) |
|
25 src = models.CharField(max_length=1024) |
|
26 videopath = models.CharField(max_length=1024, null=True, blank=True) |
|
27 creation_date = models.DateTimeField(auto_now_add=True) |
|
28 update_date = models.DateTimeField(auto_now=True) |
|
29 title = models.CharField(max_length=1024, null=True, blank=True) |
|
30 description = models.TextField(null=True, blank=True) |
|
31 external_id = models.CharField(max_length=1024, null=True, blank=True) |
|
32 authors = models.ManyToManyField(Author, blank=True) |
|
33 duration = models.IntegerField(null=True, blank=True) |
|
34 |
|
35 def get_duration(self): |
|
36 if self.duration is None: |
|
37 doc = xml.dom.minidom.parse(self.iri_file_path()) |
|
38 doc = Ft.Xml.Domlette.ConvertDocument(doc) |
|
39 con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
40 res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con) |
|
41 self.duration = int(res[0].getAttributeNS(None, u'dur')) |
|
42 self.save() |
|
43 return self.duration |
|
44 |
|
45 def delete(self): |
|
46 super(Content, self).delete() |
|
47 writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED) |
|
48 writer.deleteDocuments(lucene.Term("iri_id", self.iri_id)) |
|
49 writer.commit() |
|
50 |
|
51 def save(self): |
|
52 # create iri file if needed |
|
53 try: |
|
54 iri_file_path = self.iri_file_path() |
|
55 if not os.path.exists(iri_file_path): |
|
56 dir = os.path.dirname(iri_file_path) |
|
57 if not os.path.exists(dir): |
|
58 os.makedirs(dir) |
|
59 file = open(iri_file_path,"w") |
|
60 create_empty_iri(file, self, "IRI") |
|
61 except Exception, e: |
|
62 if os.path.exists(iri_file_path): |
|
63 os.remove(iri_file_path) |
|
64 raise e |
|
65 # update it |
|
66 super(Content, self).save() |
|
67 |
|
68 def __unicode__(self): |
|
69 return str(self.id) + ": " + self.iri_id |
|
70 |
|
71 def iri_url(self, web_url=settings.WEB_URL): |
|
72 if 'http' in self.iriurl or 'https' in self.iriurl: |
|
73 return self.iriurl |
|
74 else: |
|
75 return unicode(web_url) + unicode(settings.MEDIA_URL)+u"media/ldt/"+unicode(self.iriurl) |
|
76 |
|
77 def iri_file_path(self): |
|
78 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.iriurl)) |
|
79 |
|
80 def iri_url_template(self): |
|
81 return "${web_url}${media_url}media/ldt/" + unicode(self.iri_id) + "/" + os.path.basename(self.iriurl) |
|
82 |
|
83 |
|
84 class Project(Document): |
|
85 STATE_CHOICES=( |
|
86 (1, 'edition'), |
|
87 (2, 'published'), |
|
88 (3, 'moderated'), |
|
89 (4, 'rejected'), |
|
90 (5, 'deleted') |
|
91 ) |
|
92 ldt_id = models.CharField(max_length=1024, unique=True) |
|
93 ldt = models.TextField(null=True) |
|
94 title = models.CharField(max_length=1024) |
|
95 contents = models.ManyToManyField(Content) |
|
96 creation_date = models.DateTimeField(auto_now_add=True) |
|
97 modification_date = models.DateTimeField(auto_now=True) |
|
98 created_by = models.CharField(_("created by"), max_length=70) |
|
99 changed_by = models.CharField(_("changed by"), max_length=70) |
|
100 state = models.IntegerField(choices=STATE_CHOICES, default=1) |
|
101 |
|
102 def __unicode__(self): |
|
103 return unicode(self.id) + u": " + unicode(self.ldt_id) |
|
104 |
|
105 def get_description(self, doc=None): |
|
106 |
|
107 if doc is None: |
|
108 doc = xml.dom.minidom.parseString(self.ldt) |
|
109 doc = Ft.Xml.Domlette.ConvertDocument(doc) |
|
110 |
|
111 con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
112 res = xml.xpath.Evaluate("/iri/project", context=con) |
|
113 if len(res) > 0: |
|
114 return res[0].getAttributeNS(None, u'abstract') |
|
115 else: |
|
116 return None |
|
117 |
|
118 |
|
119 @staticmethod |
|
120 def create_project(user, title, contents): |
|
121 owner = Owner.objects.get(user=user) |
|
122 project = Project(title=title, owner=owner) |
|
123 project.ldt_id = str(uuid.uuid1()) |
|
124 project.created_by=user.username |
|
125 project.changed_by=user.username |
|
126 project.state = 1 |
|
127 project.save() |
|
128 for content in contents: |
|
129 project.contents.add(content) |
|
130 project.save() |
|
131 return create_ldt(project, user) |
|
132 |
|
133 def copy_project(self, user, title): |
|
134 owner = Owner.objects.get(user=user) |
|
135 project = Project(title=title, owner=owner) |
|
136 project = copy_ldt(self, project, user) |
|
137 project.save() |
|
138 for content in self.contents.all(): |
|
139 project.contents.add(content) |
|
140 project.save() |
|
141 return project |
|
142 |
|
143 class Segment(models.Model): |
|
144 |
|
145 project_obj = models.ForeignKey(Project, null=True) |
|
146 content = models.ForeignKey(Content) |
|
147 project_id = models.CharField(max_length=1024, unique=False, blank=True, null=True) |
|
148 iri_id = models.CharField(max_length=1024, unique=False) |
|
149 ensemble_id = models.CharField(max_length=1024, unique=False) |
|
150 cutting_id = models.CharField(max_length=1024, unique=False) |
|
151 element_id = models.CharField(max_length=1024, unique=False) |
|
152 tags = models.CharField(max_length=2048, unique=False, null=True, blank=True) |
|
153 title = models.CharField(max_length=2048, unique=False, null=True, blank=True) |
|
154 duration = models.IntegerField(null=True) |
|
155 start_ts = models.IntegerField(null=True) |
|
156 author = models.CharField(max_length=1024, unique=False, null=True, blank=True) |
|
157 date = models.CharField(max_length=128, unique=False, null=True, blank=True) |
|
158 abstract = models.TextField(null=True, blank=True) |
|
159 |
|
160 def __unicode__(self): |
|
161 return "/".join((unicode(self.project_id), unicode(self.iri_id), unicode(self.ensemble_id), unicode(self.cutting_id), unicode(self.element_id))) |
|
162 |
|
163 class Meta: |
|
164 unique_together = (('project_id', 'iri_id', 'ensemble_id', 'cutting_id', 'element_id'),) |
|
165 |