| author | ymh <ymh.work@gmail.com> |
| Tue, 21 Sep 2010 06:34:28 +0200 | |
| changeset 63 | 7b721b427b73 |
| parent 62 | 39b2dab4f939 |
| child 67 | 90fd14c649bb |
| permissions | -rw-r--r-- |
| 32 | 1 |
from django.conf import settings |
| 0 | 2 |
from django.db import models |
| 32 | 3 |
from django.utils.translation import ugettext_lazy as _ |
| 0 | 4 |
from ldt.core.models import Document, Owner |
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
5 |
from django.contrib.auth.models import User |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
6 |
import tagging.fields |
| 41 | 7 |
from utils import create_ldt, copy_ldt, create_empty_iri, update_iri, generate_uuid |
| 32 | 8 |
import lxml.etree |
| 0 | 9 |
import os.path |
10 |
import uuid |
|
11 |
||
12 |
class Author(models.Model): |
|
13 |
||
14 |
handle = models.CharField(max_length=512, unique=True, blank=True, null=True) |
|
15 |
email = models.EmailField(unique=False, blank=True, null=True) |
|
16 |
firstname = models.CharField(max_length=512, blank=True, null=True) |
|
17 |
lastname = models.CharField(max_length=512, blank=True, null=True) |
|
18 |
||
19 |
def __unicode__(self): |
|
20 |
return unicode(self.id) + " - " + self.handle + ", " + self.email + ", " + self.firstname + " " + self.lastname |
|
21 |
||
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
22 |
class Media(models.Model): |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
23 |
external_id = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('media.external_id')) |
| 62 | 24 |
external_permalink = models.URLField(max_length=1024, null=True, blank=True, verbose_name=_('content.external_permalink')) |
25 |
external_publication_url = models.URLField(max_length=1024, null=True, blank=True, verbose_name=_('content.external_publication_url')) |
|
26 |
external_src_url = models.URLField(max_length=1024, null=True, blank=True, verbose_name=_('content.external_publication_url')) |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
27 |
creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('media.creation_date')) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
28 |
media_creation_date = models.DateTimeField(null=True, blank=True, verbose_name=_('media.media_creation_date')) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
29 |
update_date = models.DateTimeField(auto_now=True, verbose_name=_('media.update_date')) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
30 |
videopath = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('media.videopath')) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
31 |
duration = models.IntegerField(null=True, blank=True, verbose_name=_('media.duration')) |
| 62 | 32 |
creator = models.ForeignKey(User, blank=True, null=True, verbose_name=_('media.creator')) |
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
33 |
description = models.TextField(null=True, blank=True, verbose_name=_('description')) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
34 |
title = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('title')) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
35 |
src = models.CharField(max_length=1024, unique=True, verbose_name=_('media.src')) |
| 62 | 36 |
|
37 |
def __unicode__(self): |
|
38 |
strings = [] |
|
39 |
if self.title: |
|
40 |
strings.append(unicode(self.title)) |
|
41 |
else: |
|
42 |
strings.append(unicode(self.src)) |
|
43 |
if self.external_id: |
|
44 |
strings.append(unicode(self.external_id)) |
|
45 |
return "|".join(strings) |
|
| 0 | 46 |
|
47 |
class Content(models.Model): |
|
| 44 | 48 |
iri_id = models.CharField(max_length=1024, unique=True, default=generate_uuid, verbose_name=_('content.iri_id')) |
49 |
iriurl = models.CharField(max_length=1024, verbose_name=_('content.iriurl')) |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
50 |
# src = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('content.src')) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
51 |
# videopath = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('content.videopath')) |
| 44 | 52 |
creation_date = models.DateTimeField(auto_now_add=True, verbose_name=_('content.creation_date')) |
53 |
update_date = models.DateTimeField(auto_now=True, verbose_name=_('content.update_date')) |
|
54 |
title = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('content.title')) |
|
55 |
description = models.TextField(null=True, blank=True, verbose_name=_('content.description')) |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
56 |
# external_id = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('content.external_id')) |
| 44 | 57 |
authors = models.ManyToManyField(Author, blank=True, verbose_name=_('content.authors')) |
58 |
duration = models.IntegerField(null=True, blank=True, verbose_name=_('content.duration')) |
|
59 |
content_creation_date = models.DateTimeField(null=True, blank=True, verbose_name=_('content.content_creation_date')) |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
60 |
tags = tagging.fields.TagField(max_length=2048, null=True, blank=True ) |
| 62 | 61 |
media_obj = models.ForeignKey('Media', blank=True, null=True ) |
| 0 | 62 |
|
63 |
def get_duration(self): |
|
64 |
if self.duration is None: |
|
| 32 | 65 |
doc = lxml.etree.parse(self.iri_file_path()) |
66 |
res = doc.xpath("/iri/body/medias/media[@id='video']/video") |
|
67 |
self.duration = int(res[0].get(u'dur') or 0) |
|
| 0 | 68 |
self.save() |
69 |
return self.duration |
|
70 |
||
71 |
def delete(self): |
|
72 |
super(Content, self).delete() |
|
73 |
writer = lucene.IndexWriter(STORE, ANALYZER, True, lucene.IndexWriter.MaxFieldLength.UNLIMITED) |
|
74 |
writer.deleteDocuments(lucene.Term("iri_id", self.iri_id)) |
|
75 |
writer.commit() |
|
76 |
||
| 5 | 77 |
#TODO: better manage the change in .iri name and error scenario (save in temp file + rename |
| 32 | 78 |
def save(self, *args, **kwargs): |
| 41 | 79 |
|
| 0 | 80 |
# create iri file if needed |
| 5 | 81 |
created = False |
| 0 | 82 |
try: |
83 |
iri_file_path = self.iri_file_path() |
|
84 |
if not os.path.exists(iri_file_path): |
|
85 |
dir = os.path.dirname(iri_file_path) |
|
86 |
if not os.path.exists(dir): |
|
87 |
os.makedirs(dir) |
|
| 5 | 88 |
created = True |
| 0 | 89 |
file = open(iri_file_path,"w") |
90 |
create_empty_iri(file, self, "IRI") |
|
| 5 | 91 |
else: |
92 |
created = False |
|
93 |
update_iri(iri_file_path, self, "IRI") |
|
94 |
||
| 0 | 95 |
except Exception, e: |
| 5 | 96 |
if created: |
97 |
if os.path.exists(iri_file_path): |
|
98 |
os.remove(iri_file_path) |
|
99 |
raise e |
|
| 41 | 100 |
|
| 0 | 101 |
# update it |
| 32 | 102 |
super(Content, self).save(*args, **kwargs) |
| 0 | 103 |
|
104 |
def __unicode__(self): |
|
105 |
return str(self.id) + ": " + self.iri_id |
|
106 |
||
107 |
def iri_url(self, web_url=settings.WEB_URL): |
|
108 |
if 'http' in self.iriurl or 'https' in self.iriurl: |
|
109 |
return self.iriurl |
|
110 |
else: |
|
111 |
return unicode(web_url) + unicode(settings.MEDIA_URL)+u"media/ldt/"+unicode(self.iriurl) |
|
112 |
||
113 |
def iri_file_path(self): |
|
114 |
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)) |
|
115 |
||
116 |
def iri_url_template(self): |
|
117 |
return "${web_url}${media_url}media/ldt/" + unicode(self.iri_id) + "/" + os.path.basename(self.iriurl) |
|
| 62 | 118 |
|
119 |
||
120 |
def __get_empty_media(self): |
|
121 |
if settings.EMPTY_MEDIA_EXTERNALID: |
|
122 |
empty_media = Media.objects.get(externalid=settings.EMPTY_MEDIA_EXTERNALID) |
|
123 |
return empty_media |
|
124 |
else: |
|
125 |
return None |
|
126 |
||
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
127 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
128 |
def videopath(): #@NoSelf |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
129 |
doc = """simulate videopath""" #@UnusedVariable |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
130 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
131 |
def fget(self): |
| 62 | 132 |
if self.media_obj is None: |
133 |
empty_media = self.__get_empty_media() |
|
134 |
if empty_media: |
|
135 |
return empty_media.videopath |
|
136 |
else: |
|
137 |
return None |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
138 |
else: |
| 62 | 139 |
return self.media_obj.videopath |
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
140 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
141 |
def fset(self, value): |
| 62 | 142 |
if self.media_obj is not None: |
143 |
self.media_obj.videopath = value |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
144 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
145 |
return locals() |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
146 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
147 |
videopath = property(**videopath()) |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
148 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
149 |
def src(): #@NoSelf |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
150 |
doc = """simulate videopath""" #@UnusedVariable |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
151 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
152 |
def fget(self): |
| 62 | 153 |
if self.media_obj is None: |
154 |
empty_media = self.__get_empty_media() |
|
155 |
if empty_media: |
|
156 |
return empty_media.src |
|
157 |
else: |
|
158 |
return None |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
159 |
else: |
| 62 | 160 |
return self.media_obj.src |
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
161 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
162 |
def fset(self, value): |
| 62 | 163 |
if self.media_obj is None or self.media_obj.src != value: |
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
164 |
media, created = Media.objects.get_or_create(src=value, defaults={'src':value}) |
| 62 | 165 |
self.media_obj = media |
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
166 |
self.save() |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
167 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
168 |
return locals() |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
169 |
|
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
170 |
src = property(**src()) |
| 63 | 171 |
|
172 |
def externalid(): #@NoSelf |
|
173 |
doc = """simulate externalid""" #@UnusedVariable |
|
174 |
||
175 |
def fget(self): |
|
176 |
if self.media_obj is None: |
|
177 |
empty_media = self.__get_empty_media() |
|
178 |
if empty_media: |
|
179 |
return empty_media.externalid |
|
180 |
else: |
|
181 |
return None |
|
182 |
else: |
|
183 |
return self.media_obj.externalid |
|
184 |
||
185 |
def fset(self, value): |
|
186 |
if self.media_obj is not None: |
|
187 |
self.media_obj.externalid = value |
|
188 |
||
189 |
return locals() |
|
190 |
||
191 |
externalid = property(**externalid()) |
|
192 |
||
| 0 | 193 |
|
194 |
||
195 |
class Project(Document): |
|
196 |
STATE_CHOICES=( |
|
197 |
(1, 'edition'), |
|
198 |
(2, 'published'), |
|
199 |
(3, 'moderated'), |
|
200 |
(4, 'rejected'), |
|
201 |
(5, 'deleted') |
|
202 |
) |
|
203 |
ldt_id = models.CharField(max_length=1024, unique=True) |
|
204 |
ldt = models.TextField(null=True) |
|
205 |
title = models.CharField(max_length=1024) |
|
206 |
contents = models.ManyToManyField(Content) |
|
207 |
creation_date = models.DateTimeField(auto_now_add=True) |
|
208 |
modification_date = models.DateTimeField(auto_now=True) |
|
209 |
created_by = models.CharField(_("created by"), max_length=70) |
|
210 |
changed_by = models.CharField(_("changed by"), max_length=70) |
|
211 |
state = models.IntegerField(choices=STATE_CHOICES, default=1) |
|
212 |
||
213 |
def __unicode__(self): |
|
214 |
return unicode(self.id) + u": " + unicode(self.ldt_id) |
|
215 |
||
216 |
def get_description(self, doc=None): |
|
217 |
||
218 |
if doc is None: |
|
| 32 | 219 |
doc = lxml.etree.fromstring(self.ldt) |
| 0 | 220 |
|
| 32 | 221 |
res = doc.xpath("/iri/project") |
| 0 | 222 |
if len(res) > 0: |
| 32 | 223 |
return res[0].get(u'abstract') |
| 0 | 224 |
else: |
225 |
return None |
|
226 |
||
227 |
||
228 |
@staticmethod |
|
229 |
def create_project(user, title, contents): |
|
230 |
owner = Owner.objects.get(user=user) |
|
231 |
project = Project(title=title, owner=owner) |
|
232 |
project.ldt_id = str(uuid.uuid1()) |
|
233 |
project.created_by=user.username |
|
234 |
project.changed_by=user.username |
|
235 |
project.state = 1 |
|
236 |
project.save() |
|
237 |
for content in contents: |
|
238 |
project.contents.add(content) |
|
239 |
project.save() |
|
240 |
return create_ldt(project, user) |
|
241 |
||
242 |
def copy_project(self, user, title): |
|
243 |
owner = Owner.objects.get(user=user) |
|
244 |
project = Project(title=title, owner=owner) |
|
245 |
project = copy_ldt(self, project, user) |
|
246 |
project.save() |
|
247 |
for content in self.contents.all(): |
|
248 |
project.contents.add(content) |
|
249 |
project.save() |
|
250 |
return project |
|
251 |
||
252 |
class Segment(models.Model): |
|
253 |
||
254 |
project_obj = models.ForeignKey(Project, null=True) |
|
255 |
content = models.ForeignKey(Content) |
|
256 |
project_id = models.CharField(max_length=1024, unique=False, blank=True, null=True) |
|
257 |
iri_id = models.CharField(max_length=1024, unique=False) |
|
258 |
ensemble_id = models.CharField(max_length=1024, unique=False) |
|
259 |
cutting_id = models.CharField(max_length=1024, unique=False) |
|
260 |
element_id = models.CharField(max_length=1024, unique=False) |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
261 |
tags = tagging.fields.TagField(max_length=2048, null=True, blank=True, unique=False) |
| 0 | 262 |
title = models.CharField(max_length=2048, unique=False, null=True, blank=True) |
263 |
duration = models.IntegerField(null=True) |
|
264 |
start_ts = models.IntegerField(null=True) |
|
265 |
author = models.CharField(max_length=1024, unique=False, null=True, blank=True) |
|
266 |
date = models.CharField(max_length=128, unique=False, null=True, blank=True) |
|
267 |
abstract = models.TextField(null=True, blank=True) |
|
268 |
||
269 |
def __unicode__(self): |
|
270 |
return "/".join((unicode(self.project_id), unicode(self.iri_id), unicode(self.ensemble_id), unicode(self.cutting_id), unicode(self.element_id))) |
|
271 |
||
272 |
class Meta: |
|
273 |
unique_together = (('project_id', 'iri_id', 'ensemble_id', 'cutting_id', 'element_id'),) |
|
274 |
||
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
44
diff
changeset
|
275 |