| author | ymh <ymh.work@gmail.com> |
| Thu, 19 May 2011 10:38:36 +0200 | |
| changeset 113 | cf3bdb2a4216 |
| parent 112 | 9886ab183b09 |
| child 114 | 279f1782c184 |
| permissions | -rw-r--r-- |
| 19 | 1 |
from django.conf import settings |
| 95 | 2 |
from ldt.indexation import get_searcher, get_query_parser |
| 19 | 3 |
import datetime |
| 0 | 4 |
import django.core.urlresolvers |
| 19 | 5 |
import lxml.etree |
| 0 | 6 |
import urllib |
| 19 | 7 |
import uuid |
| 0 | 8 |
|
9 |
__BOOLEAN_DICT = { |
|
10 |
'false':False, |
|
11 |
'true':True, |
|
12 |
'0':False, |
|
13 |
'1':True, |
|
14 |
't': True, |
|
15 |
'f':False |
|
16 |
} |
|
17 |
||
| 19 | 18 |
def reduce_text_node(element_node, xpath_str=None): |
19 |
node_list = [] |
|
20 |
if xpath_str is not None: |
|
21 |
node_list = element_node.xpath(xpath_str, smart_strings=False) |
|
22 |
else: |
|
23 |
node_list = [element_node.text] |
|
| 63 | 24 |
return reduce(lambda t, s: t + s, node_list , "") |
| 0 | 25 |
|
26 |
def boolean_convert(bool): |
|
27 |
if bool is None: |
|
28 |
return False |
|
29 |
if bool is True or bool is False: |
|
30 |
return bool |
|
31 |
key = str(bool).lower() |
|
32 |
return __BOOLEAN_DICT.get(key, False) |
|
33 |
||
34 |
def generate_uuid(): |
|
35 |
return unicode(uuid.uuid1()) |
|
36 |
||
37 |
class LdtSearch(object): |
|
38 |
||
39 |
def query(self, field, query): |
|
| 95 | 40 |
indexSearcher = get_searcher() |
|
97
10f69a5bd9e1
correct propagation of project id on indexation
ymh <ymh.work@gmail.com>
parents:
95
diff
changeset
|
41 |
queryParser = get_query_parser(field) |
| 0 | 42 |
queryObj = queryParser.parse(query) |
43 |
hits = indexSearcher.search(queryObj, settings.LDT_MAX_SEARCH_NUMBER) |
|
44 |
||
45 |
res = [] |
|
46 |
for hit in hits.scoreDocs: |
|
47 |
doc = indexSearcher.doc(hit.doc) |
|
|
97
10f69a5bd9e1
correct propagation of project id on indexation
ymh <ymh.work@gmail.com>
parents:
95
diff
changeset
|
48 |
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"), "project_id":doc.get("project_id")}) |
| 0 | 49 |
indexSearcher.close() |
50 |
return res |
|
51 |
||
52 |
def queryAll(self, query): |
|
53 |
return self.query("all", query) |
|
54 |
||
55 |
class LdtUtils(object): |
|
56 |
||
|
113
cf3bdb2a4216
improve ldt generation for merging projects
ymh <ymh.work@gmail.com>
parents:
112
diff
changeset
|
57 |
def generateLdt(self, contentList, title=u"", author=u"IRI Web", web_url=u"", startSegment=None, projects=None): |
| 0 | 58 |
|
59 |
iri = lxml.etree.Element(u'iri') |
|
60 |
doc = lxml.etree.ElementTree(iri) |
|
61 |
||
62 |
project = lxml.etree.SubElement(iri, u'project') |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
63 |
project.set(u"id", unicode(str(uuid.uuid1()))) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
64 |
project.set(u"title", unicode(title)) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
65 |
project.set(u"user", author) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
66 |
project.set(u"abstract", u"") |
| 0 | 67 |
|
68 |
medias = lxml.etree.SubElement(iri, u"medias") |
|
69 |
for content in contentList: |
|
70 |
videopath = unicode(settings.STREAM_URL) |
|
71 |
if content.videopath : |
|
72 |
videopath = unicode(content.videopath) |
|
73 |
media = lxml.etree.SubElement(medias, "media") |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
74 |
media.set(u"id", content.iri_id) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
75 |
media.set(u"src", content.iri_url(web_url)) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
76 |
media.set(u"video", videopath) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
77 |
media.set(u"pict", u"") |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
78 |
media.set(u"extra", u"") |
| 0 | 79 |
|
|
113
cf3bdb2a4216
improve ldt generation for merging projects
ymh <ymh.work@gmail.com>
parents:
112
diff
changeset
|
80 |
if projects is None: |
|
cf3bdb2a4216
improve ldt generation for merging projects
ymh <ymh.work@gmail.com>
parents:
112
diff
changeset
|
81 |
projects = [] |
| 0 | 82 |
annotations_nodes = {} |
|
113
cf3bdb2a4216
improve ldt generation for merging projects
ymh <ymh.work@gmail.com>
parents:
112
diff
changeset
|
83 |
for project in project: |
|
cf3bdb2a4216
improve ldt generation for merging projects
ymh <ymh.work@gmail.com>
parents:
112
diff
changeset
|
84 |
ldtdoc = lxml.etree.fromstring(project.ldt.encode("utf-8")) |
| 0 | 85 |
res = ldtdoc.xpath("/iri/annotations/content") |
86 |
||
87 |
for content in res: |
|
88 |
contentid = content.get("id") |
|
89 |
if annotations_nodes.has_key(contentid): |
|
90 |
contentnode = annotations_nodes[contentid] |
|
91 |
else: |
|
92 |
contentnode = {"id":contentid, "ensembles":[]} |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
93 |
annotations_nodes[contentid] = contentnode |
| 0 | 94 |
for ens in content.childNodes: |
95 |
if ens.tag.endswith("ensemble"): |
|
96 |
contentnode["ensembles"].append(ens.tag) |
|
97 |
||
| 19 | 98 |
annotations = lxml.etree.SubElement(iri, "annotations") |
99 |
if len(annotations_nodes) > 0: |
|
| 0 | 100 |
for content in contentList: |
101 |
if content.content_base.iri_id in annotations_nodes: |
|
102 |
contentnode = annotations_nodes[content.content_base.iri_id] |
|
103 |
if contentnode is not None: |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
104 |
if len(contentnode["ensembles"]) > 0: |
| 19 | 105 |
content = lxml.etree.SubElement(annotations, "content") |
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
106 |
content.set("id", contentnode["id"]) |
| 0 | 107 |
content.text = u"" |
108 |
else: |
|
| 19 | 109 |
content = lxml.etree.SubElement(annotations, "content") |
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
110 |
content.set("id", contentnode["id"]) |
| 0 | 111 |
|
112 |
||
113 |
displays = lxml.etree.SubElement(iri, "displays") |
|
114 |
if len(contentList) > 0: |
|
115 |
display = lxml.etree.SubElement(displays, "display") |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
116 |
display.set(u"id", u"0") |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
117 |
display.set(u"title", u"generated") |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
118 |
display.set(u"idsel", contentList[0].iri_id) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
119 |
display.set(u"tc", u"0") |
| 0 | 120 |
for content in contentList: |
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
121 |
contentd = lxml.etree.SubElement(display, "content") |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
122 |
contentd.set(u"id", content.iri_id) |
| 0 | 123 |
filepath = urllib.urlopen(content.iri_url()) |
124 |
||
125 |
udoc = lxml.etree.parse(filepath) |
|
126 |
res = udoc.xpath("/iri/body/ensembles/ensemble/decoupage") |
|
127 |
for decoupagenode in res: |
|
128 |
decoupage_id = decoupagenode.get(u"id") |
|
129 |
ensemble_id = decoupagenode.getparent().get(u"id") |
|
130 |
decoupage_id = decoupagenode.get(u"id") |
|
131 |
ensemble_id = decoupagenode.getparent().get(u"id") |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
132 |
decoupage = lxml.etree.SubElement(contentd, "decoupage") |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
133 |
decoupage.set(u"id", decoupage_id) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
134 |
decoupage.set(u"idens", ensemble_id) |
| 0 | 135 |
if startSegment is not None: |
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
136 |
activeSegment = lxml.etree.SubElement(display, "activeSegment") |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
137 |
idas = lxml.etree.SubElement(activeSegment, "id") |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
138 |
idas.set(u"idctt", startSegment["idcontent"]) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
139 |
idas.set(u"idens" , startSegment["idgroup"]) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
140 |
idas.set(u"idcut", startSegment["idcutting"]) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
141 |
idas.set(u"idseg", startSegment["idsegment"]) |
| 0 | 142 |
|
| 19 | 143 |
lxml.etree.SubElement(iri, "edits") |
|
112
9886ab183b09
add permalink + corrcetion config with static and media path. update urls
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
144 |
return doc |
| 0 | 145 |
|
146 |
||
147 |
def generateInit(self, url, method, search=None): |
|
148 |
||
149 |
iri = lxml.etree.Element('iri') |
|
150 |
||
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
151 |
elementFiles = lxml.etree.SubElement(iri, 'files') |
| 0 | 152 |
elementInit = lxml.etree.SubElement(elementFiles, 'init') |
153 |
elementfile = lxml.etree.SubElement(elementInit, 'file') |
|
154 |
||
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
155 |
elementfile.set('src', settings.WEB_URL + django.core.urlresolvers.reverse(method, args=url)) |
| 0 | 156 |
if(search): |
|
69
784ebba76424
enhance new media/content creation with url : external stream, local stream using STREAM_URL or youtube http.
cavaliet
parents:
63
diff
changeset
|
157 |
elementfile.set("segsel", settings.WEB_URL + django.core.urlresolvers.reverse(search, args=url)) |
| 0 | 158 |
|
| 19 | 159 |
lxml.etree.SubElement(elementFiles, 'recent') |
160 |
lxml.etree.SubElement(elementFiles, 'library') |
|
| 0 | 161 |
|
162 |
return iri |
|
163 |
||
164 |
||
165 |
||
166 |
def create_ldt(project, user): |
|
167 |
"""create xml""" |
|
168 |
||
169 |
||
170 |
contentList = project.contents.all() |
|
171 |
||
172 |
# create a dom |
|
173 |
iri = lxml.etree.Element('iri') |
|
174 |
doc = lxml.etree.ElementTree(iri) |
|
175 |
||
176 |
#node project |
|
177 |
elementProject = lxml.etree.SubElement(iri, 'project') |
|
178 |
||
179 |
elementProject.set('abstract', "") |
|
180 |
elementProject.set('title', project.title) |
|
181 |
elementProject.set('user', user.username) |
|
182 |
elementProject.set('id', project.ldt_id) |
|
183 |
||
184 |
#node medias |
|
185 |
elementMedias = lxml.etree.SubElement(iri, 'medias') |
|
186 |
||
187 |
idsel = None |
|
188 |
for content in contentList: |
|
189 |
if not idsel: |
|
190 |
idsel = content.iri_id |
|
191 |
elementMedia = lxml.etree.SubElement(elementMedias, 'media') |
|
192 |
elementMedia.set('id', content.iri_id) |
|
193 |
elementMedia.set('src', content.iri_url()) |
|
|
69
784ebba76424
enhance new media/content creation with url : external stream, local stream using STREAM_URL or youtube http.
cavaliet
parents:
63
diff
changeset
|
194 |
|
|
784ebba76424
enhance new media/content creation with url : external stream, local stream using STREAM_URL or youtube http.
cavaliet
parents:
63
diff
changeset
|
195 |
if content.videopath != None : |
| 0 | 196 |
elementMedia.set('video', content.videopath) |
197 |
else: |
|
198 |
elementMedia.set('video', settings.STREAM_URL) |
|
199 |
elementMedia.set('pict', "") |
|
200 |
elementMedia.set('extra', "") |
|
201 |
||
202 |
if not idsel: |
|
203 |
idsel = "" |
|
204 |
||
205 |
#node annotations |
|
| 19 | 206 |
lxml.etree.SubElement(iri, 'annotations') |
| 0 | 207 |
|
208 |
#node displays |
|
209 |
elementDisplays = lxml.etree.SubElement(iri, 'displays') |
|
210 |
elementDisplay = lxml.etree.SubElement(elementDisplays, 'display') |
|
211 |
elementDisplay.set('id', '0') |
|
212 |
elementDisplay.set('title', 'Init view') |
|
213 |
elementDisplay.set('idsel', idsel) |
|
214 |
elementDisplay.set('tc', '0') |
|
215 |
elementDisplay.set('zoom', '0') |
|
216 |
elementDisplay.set('scroll', '0') |
|
217 |
elementDisplay.set('infoBAB', '') |
|
218 |
||
219 |
||
220 |
#node content |
|
221 |
for content in contentList: |
|
222 |
elementContent = lxml.etree.SubElement(elementDisplay, 'content') |
|
223 |
elementContent.set('id', content.iri_id) |
|
224 |
||
225 |
if not 'http' in content.iriurl: |
|
226 |
#eg: "iiiielizabethrosse/ENMI08-III_elizabethrosse.iri" |
|
227 |
url = content.iri_url() |
|
228 |
else: |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
229 |
url = content.iriurl |
| 0 | 230 |
file = urllib.urlopen(url) |
231 |
doc = lxml.etree.parse(file) |
|
232 |
res = doc.xpath("/iri/body/ensembles/ensemble/decoupage") |
|
233 |
||
234 |
#node decoupage |
|
235 |
for decoupagenode in res: |
|
236 |
decoupage_id = decoupagenode.get(u"id") |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
237 |
parent = decoupagenode.getparent() |
| 0 | 238 |
ensemble_id = parent.get(u"id") |
239 |
elementDecoupage = lxml.etree.SubElement(elementContent, 'decoupage') |
|
240 |
elementDecoupage.set('idens', ensemble_id) |
|
241 |
elementDecoupage.set('id', decoupage_id) |
|
242 |
||
243 |
#node edits |
|
| 19 | 244 |
lxml.etree.SubElement(iri, 'edits') |
| 0 | 245 |
|
246 |
#write dom in Project.ldt |
|
247 |
project.ldt = lxml.etree.tostring(iri, pretty_print=True) |
|
248 |
||
249 |
#save Project |
|
250 |
project.save() |
|
251 |
return project |
|
252 |
||
253 |
||
254 |
def copy_ldt(project, new_project, user): |
|
255 |
new_project.ldt_id = str(uuid.uuid1()) |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
256 |
new_project.created_by = user.username |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
257 |
new_project.changed_by = user.username |
| 0 | 258 |
new_project.state = 1 |
259 |
|
|
260 |
|
|
261 |
"""create xml""" |
|
262 |
||
263 |
ldt = lxml.etree.fromstring(project.ldt.encode("utf-8")) |
|
264 |
res = ldt.xpath("/iri/project") |
|
265 |
for elementProject in res: |
|
266 |
elementProject.set('abstract', "") |
|
267 |
elementProject.set('title', new_project.title) |
|
268 |
elementProject.set('user', user.username) |
|
269 |
elementProject.set('id', new_project.ldt_id) |
|
270 |
||
271 |
new_project.ldt = lxml.etree.tostring(ldt, pretty_print=True) |
|
272 |
||
273 |
#save Project |
|
274 |
new_project.save() |
|
275 |
return new_project |
|
276 |
||
277 |
def create_empty_iri(file, content, username): |
|
278 |
||
279 |
iri = lxml.etree.Element('iri') |
|
280 |
doc = lxml.etree.ElementTree(iri) |
|
281 |
||
282 |
head = lxml.etree.SubElement(iri, 'head') |
|
283 |
meta_id = lxml.etree.SubElement(head, 'meta') |
|
284 |
meta_id.set(u'name', u'id') |
|
285 |
meta_id.set(u'content', unicode(content.iri_id)) |
|
286 |
meta_title = lxml.etree.SubElement(head, 'meta') |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
287 |
meta_title.set(u'name', u'title') |
| 0 | 288 |
meta_title.set(u'content', unicode(content.title)) |
289 |
meta_abstract = lxml.etree.SubElement(head, 'meta') |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
290 |
meta_abstract.set(u'name', u'abstract') |
| 0 | 291 |
meta_abstract.set(u'content', unicode(content.description)) |
292 |
meta_author = lxml.etree.SubElement(head, 'meta') |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
293 |
meta_author.set(u'name', u'author') |
| 0 | 294 |
meta_author.set(u'content', unicode(username)) |
295 |
meta_contributor = lxml.etree.SubElement(head, 'meta') |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
296 |
meta_contributor.set(u'name', u'contributor') |
| 0 | 297 |
meta_contributor.set(u'content', unicode(username)) |
298 |
meta_date = lxml.etree.SubElement(head, 'meta') |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
299 |
meta_date.set(u'name', u'date') |
| 0 | 300 |
meta_date.set(u'content', unicode(datetime.date.today().isoformat())) |
301 |
meta_copyright = lxml.etree.SubElement(head, 'meta') |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
302 |
meta_copyright.set(u'name', u'copyright') |
| 0 | 303 |
meta_copyright.set(u'content', u'IRI') |
304 |
meta_type = lxml.etree.SubElement(head, 'meta') |
|
305 |
meta_type.set(u'name', u'type') |
|
306 |
meta_type.set(u'content', u'video') |
|
307 |
||
308 |
body = lxml.etree.SubElement(iri, 'body') |
|
| 19 | 309 |
lxml.etree.SubElement(body, 'ensembles') |
310 |
lxml.etree.SubElement(body, 'links') |
|
| 0 | 311 |
|
312 |
medias = lxml.etree.SubElement(body, 'medias') |
|
313 |
||
314 |
media_video = lxml.etree.SubElement(medias, 'media') |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
315 |
media_video.set(u'id', u'video') |
| 0 | 316 |
video = lxml.etree.SubElement(media_video, 'video') |
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
317 |
video.set(u'src', unicode(content.stream_src)) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
318 |
video.set(u'id', unicode(content.iri_id)) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
319 |
video.set(u'dur', unicode(content.duration)) |
|
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
320 |
video.set(u'begin', u'0') |
| 0 | 321 |
|
322 |
media_tool = lxml.etree.SubElement(medias, 'media') |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
323 |
media_tool.set(u'id', u'tool') |
| 19 | 324 |
lxml.etree.SubElement(media_tool, 'tool') |
| 0 | 325 |
|
| 19 | 326 |
lxml.etree.SubElement(body, 'display') |
| 0 | 327 |
|
328 |
doc.write(file, pretty_print=True) |
|
329 |
||
330 |
||
331 |
def update_iri(filepath, content, username): |
|
332 |
||
333 |
# open xml |
|
334 |
doc = lxml.etree.parse(filepath) |
|
335 |
||
336 |
res = doc.xpath("/iri/head/meta") |
|
337 |
# update meta |
|
338 |
||
339 |
for meta_node in res: |
|
340 |
meta_name = meta_node.get("name") |
|
341 |
content_attr = None |
|
342 |
if meta_name == u'id': |
|
343 |
content_attr = unicode(content.iri_id) |
|
344 |
elif meta_name == u'title': |
|
345 |
content_attr = unicode(content.title) |
|
346 |
elif meta_name == u'abstract': |
|
347 |
content_attr = unicode(content.description) |
|
348 |
elif meta_name == u'contributor': |
|
349 |
content_attr = unicode(username) |
|
350 |
elif meta_name == u"date": |
|
351 |
content_attr = unicode(datetime.date.today().isoformat()) |
|
352 |
if content_attr is not None: |
|
353 |
meta_node.set(u"content", content_attr) |
|
354 |
||
355 |
res = doc.xpath("/iri/body/medias/media[@id='video']/video") |
|
356 |
||
357 |
if len(res) > 0: |
|
358 |
video_node = res[0] |
|
359 |
video_node.set(u'src', unicode(content.stream_src)) |
|
360 |
video_node.set(u'dur', unicode(content.duration)) |
|
361 |
video_node.set(u'id', unicode(content.iri_id)) |
|
362 |
# update video |
|
363 |
||
364 |
f = open(filepath, "w") |
|
365 |
try: |
|
366 |
doc.write(f, encoding="UTF-8", pretty_print=True, xml_declaration=True) |
|
367 |
finally: |
|
368 |
f.close() |