54
|
1 |
import lucene |
|
2 |
from ldt.ldt_utils import STORE |
|
3 |
from ldt.ldt_utils import ANALYZER |
|
4 |
from Ft.Xml import MarkupWriter |
|
5 |
import uuid |
|
6 |
import django.core.urlresolvers |
|
7 |
from django.conf import settings |
|
8 |
import urllib |
|
9 |
import xml.dom |
|
10 |
import xml.dom.minidom |
|
11 |
import xml.dom.ext |
|
12 |
import xml.xpath |
|
13 |
import datetime |
|
14 |
import Ft |
|
15 |
|
|
16 |
class LdtSearch(object): |
|
17 |
|
|
18 |
def query(self, field, query): |
|
19 |
indexSearcher = lucene.IndexSearcher(STORE) |
|
20 |
queryParser = lucene.QueryParser(lucene.Version.LUCENE_30, field, lucene.FrenchAnalyzer(lucene.Version.LUCENE_30)) |
|
21 |
queryParser.setDefaultOperator(lucene.QueryParser.Operator.AND) |
|
22 |
queryObj = queryParser.parse(query) |
|
23 |
hits = indexSearcher.search(queryObj, settings.LDT_MAX_SEARCH_NUMBER) |
|
24 |
|
|
25 |
res = [] |
|
26 |
for hit in hits.scoreDocs: |
|
27 |
doc = indexSearcher.doc(hit.doc) |
|
28 |
res.append({"iri_id":doc.get("iri_id"),"ensemble_id":doc.get("ensemble_id"),"decoupage_id":doc.get("decoupage_id"), "element_id":doc.get("element_id")}) |
|
29 |
indexSearcher.close() |
|
30 |
return res |
|
31 |
|
|
32 |
def queryAll(self, query): |
|
33 |
return self.query("all", query) |
|
34 |
|
|
35 |
class LdtUtils(object): |
|
36 |
|
|
37 |
def generateLdt(self, contentList, file, title = u"", author=u"IRI Web", web_url=u"", media_url="", startSegment = None, contributions=None): |
|
38 |
|
|
39 |
writer = MarkupWriter(file, indent = u"yes") |
|
40 |
writer.startDocument() |
|
41 |
writer.startElement(u"iri") |
|
42 |
writer.simpleElement(u"project", attributes={u"id":unicode(str(uuid.uuid1())), u"title":unicode(title) , u"user":author, u"abstract":u""}) |
|
43 |
writer.startElement(u"medias") |
|
44 |
for content in contentList: |
|
45 |
videopath = unicode(settings.STREAM_URL) |
|
46 |
if content.videopath : |
|
47 |
videopath = unicode(content.videopath) |
|
48 |
writer.simpleElement(u"media", attributes={u"id":content.iri_id,u"src":content.iri_url(web_url),u"video":videopath,u"pict":u"",u"extra":u""}) |
|
49 |
writer.endElement(u"medias") |
|
50 |
|
|
51 |
if contributions is None: |
|
52 |
contributions = [] |
|
53 |
annotations_nodes = {} |
|
54 |
for contrib in contributions: |
|
55 |
doc = xml.dom.minidom.parseString(contrib.ldtproject.ldt.encode("utf-8")) |
|
56 |
con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
57 |
res = xml.xpath.Evaluate("/iri/annotations/content", context=con) |
|
58 |
for content in res: |
|
59 |
contentid = content.getAttribute("id") |
|
60 |
if annotations_nodes.has_key(contentid): |
|
61 |
contentnode = annotations_nodes[contentid] |
|
62 |
else: |
|
63 |
contentnode = {"id":contentid, "ensembles":[]} |
|
64 |
annotations_nodes[contentid]=contentnode |
|
65 |
for ens in content.childNodes: |
|
66 |
if ens.nodeType == xml.dom.Node.ELEMENT_NODE and ens.tagName.endswith("ensemble"): |
|
67 |
contentnode["ensembles"].append(ens.toprettyxml()) |
|
68 |
|
|
69 |
if len(annotations_nodes) > 0: |
|
70 |
writer.startElement(u"annotations") |
|
71 |
for content in contentList: |
|
72 |
if content.content_base.iri_id in annotations_nodes: |
|
73 |
contentnode = annotations_nodes[content.content_base.iri_id] |
|
74 |
if contentnode is not None: |
|
75 |
if len(contentnode["ensembles"])>0: |
|
76 |
writer.startElement(u"content", attributes={"id":contentnode["id"]}) |
|
77 |
writer.text(u"") |
|
78 |
for ens in contentnode["ensembles"]: |
|
79 |
writer.xmlFragment(ens.encode("utf-8")) |
|
80 |
writer.endElement(u"content") |
|
81 |
else: |
|
82 |
writer.simpleElement(u"content", attributes={"id":contentnode["id"]}) |
|
83 |
writer.endElement(u"annotations") |
|
84 |
else: |
|
85 |
writer.simpleElement(u"annotations") |
|
86 |
|
|
87 |
|
|
88 |
writer.startElement(u"displays") |
|
89 |
if len(contentList) > 0: |
|
90 |
writer.startElement(u"display", attributes={u"id":u"0",u"title":u"generated",u"idsel":contentList[0].iri_id,u"tc":u"0"}) |
|
91 |
for content in contentList: |
|
92 |
writer.startElement(u"content", attributes={u"id":content.iri_id}) |
|
93 |
filepath = urllib.urlopen(content.iri_url()) |
|
94 |
doc = xml.dom.minidom.parse(filepath) |
|
95 |
con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
96 |
res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble/decoupage", context=con) |
|
97 |
for decoupagenode in res: |
|
98 |
decoupage_id = decoupagenode.getAttribute(u"id") |
|
99 |
ensemble_id = decoupagenode.parentNode.getAttribute(u"id") |
|
100 |
writer.simpleElement(u"decoupage", attributes={u"id":decoupage_id,u"idens":ensemble_id}) |
|
101 |
writer.endElement(u"content") |
|
102 |
if startSegment is not None: |
|
103 |
writer.startElement(u"activeSegment") |
|
104 |
writer.simpleElement(u"id",attributes={u"idctt" : startSegment["idcontent"],u"idens" : startSegment["idgroup"], u"idcut" : startSegment["idcutting"], u"idseg" : startSegment["idsegment"]}) |
|
105 |
writer.endElement(u"activeSegment") |
|
106 |
|
|
107 |
writer.endElement(u"display") |
|
108 |
writer.endElement(u"displays") |
|
109 |
writer.simpleElement(u"edits") |
|
110 |
writer.endElement(u"iri") |
|
111 |
|
|
112 |
def generateInit(self, url, method, search=None): |
|
113 |
|
|
114 |
import xml.dom |
|
115 |
import xml.dom.ext |
|
116 |
|
|
117 |
impl = xml.dom.getDOMImplementation() |
|
118 |
doc = impl.createDocument(xml.dom.EMPTY_NAMESPACE, 'iri', None) |
|
119 |
|
|
120 |
elementFiles = doc.createElement('files') |
|
121 |
doc.documentElement.appendChild(elementFiles) |
|
122 |
|
|
123 |
elementInit = doc.createElement('init') |
|
124 |
elementFiles.appendChild(elementInit) |
|
125 |
|
|
126 |
elementfile = doc.createElement('file') |
|
127 |
|
|
128 |
elementfile.setAttribute('src',settings.WEB_URL + django.core.urlresolvers.reverse(method, args=url)) |
|
129 |
elementfile.setAttribute('display', '1') |
|
130 |
if(search): |
|
131 |
elementfile.setAttribute("segsel",settings.WEB_URL + django.core.urlresolvers.reverse(search, args=url)) |
|
132 |
|
|
133 |
|
|
134 |
# /*chemin video : tant que le serveur de media n'est pas up, */ |
|
135 |
elementfile.setAttribute('video', settings.STREAM_URL) |
|
136 |
elementfile.setAttribute('pict', "") |
|
137 |
elementfile.setAttribute('extra', "") |
|
138 |
|
|
139 |
elementInit.appendChild(elementfile); |
|
140 |
|
|
141 |
elementRecent = doc.createElement('recent'); |
|
142 |
elementFiles.appendChild(elementRecent); |
|
143 |
|
|
144 |
|
|
145 |
elementLibrary = doc.createElement('library'); |
|
146 |
elementFiles.appendChild(elementLibrary); |
|
147 |
|
|
148 |
username = '' |
|
149 |
id = '' |
|
150 |
elementUser = doc.createElement('user') |
|
151 |
elementUser.setAttribute('name', username) |
|
152 |
elementUser.setAttribute('id', id) |
|
153 |
doc.documentElement.appendChild(elementUser) |
|
154 |
|
|
155 |
return doc |
|
156 |
|
|
157 |
def create_ldt(project, user): |
|
158 |
|
|
159 |
contentList=project.contents.all() |
|
160 |
|
|
161 |
"""create xml""" |
|
162 |
|
|
163 |
# create a dom |
|
164 |
impl = xml.dom.getDOMImplementation() |
|
165 |
dom = impl.createDocument(xml.dom.EMPTY_NAMESPACE, 'iri', None) |
|
166 |
#node project |
|
167 |
elementProject = dom.createElement('project') |
|
168 |
dom.documentElement.appendChild(elementProject) |
|
169 |
|
|
170 |
|
|
171 |
elementProject.setAttribute('abstract', "") |
|
172 |
elementProject.setAttribute('title', project.title) |
|
173 |
elementProject.setAttribute('user', user.username) |
|
174 |
elementProject.setAttribute('id', project.ldt_id) |
|
175 |
#node medias |
|
176 |
elementMedias = dom.createElement('medias') |
|
177 |
dom.documentElement.appendChild(elementMedias) |
|
178 |
|
|
179 |
idsel = None |
|
180 |
for content in contentList: |
|
181 |
if not idsel: |
|
182 |
idsel = content.iri_id |
|
183 |
elementMedia = dom.createElement('media') |
|
184 |
elementMedia.setAttribute('id', content.iri_id) |
|
185 |
elementMedia.setAttribute('src', content.iri_url()) |
|
186 |
if content.videopath and content.videopath !="": |
|
187 |
elementMedia.setAttribute('video', content.videopath) |
|
188 |
else: |
|
189 |
elementMedia.setAttribute('video', settings.STREAM_URL) |
|
190 |
elementMedia.setAttribute('pict', "") |
|
191 |
elementMedia.setAttribute('extra', "") |
|
192 |
elementMedias.appendChild(elementMedia) |
|
193 |
if not idsel: |
|
194 |
idsel = "" |
|
195 |
|
|
196 |
#node annotations |
|
197 |
elementAnnotations = dom.createElement('annotations') |
|
198 |
dom.documentElement.appendChild(elementAnnotations) |
|
199 |
#node displays |
|
200 |
elementDisplays = dom.createElement('displays') |
|
201 |
elementDisplay = dom.createElement('display') |
|
202 |
elementDisplay.setAttribute('id', '0') |
|
203 |
elementDisplay.setAttribute('title', 'Init view') |
|
204 |
elementDisplay.setAttribute('idsel', idsel) |
|
205 |
elementDisplay.setAttribute('tc', '0') |
|
206 |
elementDisplay.setAttribute('zoom', '0') |
|
207 |
elementDisplay.setAttribute('scroll', '0') |
|
208 |
elementDisplay.setAttribute('infoBAB', '') |
|
209 |
#node content |
|
210 |
for content in contentList: |
|
211 |
elementContent = dom.createElement('content') |
|
212 |
elementContent.setAttribute('id', content.iri_id) |
|
213 |
if not 'http' in content.iriurl: |
|
214 |
#eg: "iiiielizabethrosse/ENMI08-III_elizabethrosse.iri" |
|
215 |
url = content.iri_url() |
|
216 |
else: |
|
217 |
url =content.iriurl |
|
218 |
file = urllib.urlopen(url) |
|
219 |
doc = xml.dom.minidom.parse(file) |
|
220 |
con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
221 |
res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble/decoupage", context=con) |
|
222 |
#node decoupage |
|
223 |
for decoupagenode in res: |
|
224 |
decoupage_id = decoupagenode.getAttribute(u"id") |
|
225 |
ensemble_id = decoupagenode.parentNode.getAttribute(u"id") |
|
226 |
elementDecoupage = dom.createElement('decoupage') |
|
227 |
elementDecoupage.setAttribute('idens', ensemble_id) |
|
228 |
elementDecoupage.setAttribute('id', decoupage_id) |
|
229 |
elementContent.appendChild(elementDecoupage) |
|
230 |
elementDisplay.appendChild(elementContent) |
|
231 |
|
|
232 |
elementDisplays.appendChild(elementDisplay) |
|
233 |
dom.documentElement.appendChild(elementDisplays) |
|
234 |
|
|
235 |
elementEdits = dom.createElement('edits') |
|
236 |
dom.documentElement.appendChild(elementEdits) |
|
237 |
# write dom in Project.ldt |
|
238 |
project.ldt = dom.documentElement.toprettyxml() |
|
239 |
#save Project |
|
240 |
project.save() |
|
241 |
return project |
|
242 |
|
|
243 |
|
|
244 |
def copy_ldt(project, new_project, user): |
|
245 |
new_project.ldt_id = str(uuid.uuid1()) |
|
246 |
new_project.created_by=user.username |
|
247 |
new_project.changed_by=user.username |
|
248 |
new_project.state = 1 |
|
249 |
|
|
250 |
contentList=project.contents.all() |
|
251 |
|
|
252 |
"""create xml""" |
|
253 |
|
|
254 |
# create a dom |
|
255 |
dom = xml.dom.minidom.parseString(project.ldt.encode("utf-8")) |
|
256 |
con = xml.xpath.Context.Context(dom, 1, 1, None) |
|
257 |
res = xml.xpath.Evaluate("iri/project", context=con) |
|
258 |
for elementProject in res: |
|
259 |
elementProject.setAttribute('abstract', "") |
|
260 |
elementProject.setAttribute('title', new_project.title) |
|
261 |
elementProject.setAttribute('user', user.username) |
|
262 |
elementProject.setAttribute('id', new_project.ldt_id) |
|
263 |
|
|
264 |
new_project.ldt = dom.documentElement.toprettyxml() |
|
265 |
#save Project |
|
266 |
new_project.save() |
|
267 |
return new_project |
|
268 |
|
|
269 |
def create_empty_iri(file, content, username): |
|
270 |
|
|
271 |
writer = MarkupWriter(file, indent = u"yes") |
|
272 |
writer.startDocument() |
|
273 |
writer.startElement(u"iri") |
|
274 |
|
|
275 |
writer.startElement(u"head") |
|
276 |
|
|
277 |
writer.simpleElement(u'meta', attributes={u'name':u'id', 'content':unicode(content.iri_id)}) |
|
278 |
writer.simpleElement(u'meta', attributes={u'name':u'title', 'content':unicode(content.title)}) |
|
279 |
writer.simpleElement(u'meta', attributes={u'name':u'abstract', 'content':unicode(content.description)}) |
|
280 |
writer.simpleElement(u'meta', attributes={u'name':u'author', 'content':unicode(username)}) |
|
281 |
writer.simpleElement(u'meta', attributes={u'name':u'contributor', 'content':unicode(username)}) |
|
282 |
writer.simpleElement(u'meta', attributes={u'name':u'date', 'content':unicode(datetime.date.today().isoformat())}) |
|
283 |
writer.simpleElement(u'meta', attributes={u'name':u'copyright', 'content':u'IRI'}) |
|
284 |
writer.simpleElement(u'meta', attributes={u'name':u'type', 'content':u'video'}) |
|
285 |
|
|
286 |
writer.endElement(u"head") |
|
287 |
|
|
288 |
writer.startElement(u"body") |
|
289 |
|
|
290 |
writer.startElement(u"ensembles") |
|
291 |
writer.endElement(u"ensembles") |
|
292 |
|
|
293 |
writer.simpleElement(u'links') |
|
294 |
writer.startElement(u"medias") |
|
295 |
|
|
296 |
writer.startElement(u"media", attributes={u'id':u'video'}) |
|
297 |
writer.simpleElement(u'video', attributes={u'src':unicode(content.src),u'id':unicode(content.iri_id),u'dur':unicode(content.duration),u'begin':u'0'}) |
|
298 |
writer.endElement(u"media") |
|
299 |
|
|
300 |
writer.startElement(u"media", attributes={u'id':u'tool'}) |
|
301 |
writer.simpleElement(u'tool') |
|
302 |
writer.endElement(u"media") |
|
303 |
|
|
304 |
writer.endElement(u"medias") |
|
305 |
|
|
306 |
#writer.startElement(u'display', attributes={u'id':unicode(uuid.uuid1()), u'title':u"default", u"idsel":unicode(content.iri_id), u"tc":u"0", u"zoom":u"0", u"scroll":u"0", u"infoBAB":u""}) |
|
307 |
writer.simpleElement(u'display') |
|
308 |
|
|
309 |
writer.endElement(u"body") |
|
310 |
|
|
311 |
|
|
312 |
writer.endElement(u"iri") |
|
313 |
writer.endDocument() |
|
314 |
|
|
315 |
def update_iri(filepath, content, username): |
|
316 |
|
|
317 |
# open xml |
|
318 |
doc = xml.dom.minidom.parse(filepath) |
|
319 |
doc = Ft.Xml.Domlette.ConvertDocument(doc) |
|
320 |
|
|
321 |
con = xml.xpath.Context.Context(doc, 1, 1, None) |
|
322 |
res = xml.xpath.Evaluate("/iri/heqd/meta", context=con) |
|
323 |
# update meta |
|
324 |
|
|
325 |
for meta_node in res: |
|
326 |
meta_name = meta_node.getAttributeNS(None,"name") |
|
327 |
content_attr = None |
|
328 |
if meta_name == u'id': |
|
329 |
content_attr = unicode(content.iri_id) |
|
330 |
elif meta_name == u'title': |
|
331 |
content_attr = unicode(content.title) |
|
332 |
elif meta_name == u'abstract': |
|
333 |
content_attr = unicode(content.description) |
|
334 |
elif meta_name == u'contributor': |
|
335 |
content_attr = unicode(username) |
|
336 |
elif meta_name == u"date": |
|
337 |
content_attr = unicode(datetime.date.today().isoformat()) |
|
338 |
if content_attr is not None: |
|
339 |
meta_node.setAttributeNS(None, u"content", content_attr) |
|
340 |
|
|
341 |
res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con) |
|
342 |
|
|
343 |
if len(res) > 0: |
|
344 |
video_node = res[0] |
|
345 |
video_node.setAttributeNS(None, u'src', unicode(content.src)) |
|
346 |
video_node.setAttributeNS(None, u'dur', unicode(content.duration)) |
|
347 |
video_node.setAttributeNS(None, u'id', unicode(content.iri_id)) |
|
348 |
# update video |
|
349 |
|
|
350 |
f = open(filepath, "w") |
|
351 |
try: |
|
352 |
xml.dom.ext.Print(doc, stream=f) |
|
353 |
finally: |
|
354 |
f.close() |