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