|
0
|
1 |
import django.core.urlresolvers |
|
|
2 |
from django.http import HttpResponse, HttpResponseRedirect |
|
|
3 |
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404 |
|
|
4 |
from django.template import RequestContext |
|
|
5 |
from django.core.urlresolvers import reverse |
|
|
6 |
from django.contrib.auth.decorators import login_required |
|
|
7 |
from django.conf import settings |
|
|
8 |
from django.core import serializers |
|
|
9 |
from django.utils import simplejson |
|
|
10 |
from django.utils.html import escape |
|
|
11 |
from fileimport import * |
|
41
|
12 |
from forms import LdtImportForm, LdtAddForm, SearchForm, AddProjectForm, CopyProjectForm, ContentForm |
|
0
|
13 |
from ldt.core.models import Owner |
|
|
14 |
from models import * |
|
|
15 |
from utils import * |
|
|
16 |
from contentindexer import * |
|
|
17 |
from projectserializer import * |
|
|
18 |
from string import Template |
|
|
19 |
from Ft.Xml import MarkupWriter |
|
|
20 |
import cgi |
|
|
21 |
import uuid |
|
|
22 |
import base64 |
|
|
23 |
import lucene |
|
|
24 |
import xml.dom |
|
|
25 |
import xml.dom.ext |
|
|
26 |
import xml.dom.minidom |
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
def searchForm(request): |
|
|
31 |
form = SearchForm() |
|
|
32 |
return render_to_response('ldt/ldt_utils_utils/search_form.html',{'form': form} , context_instance=RequestContext(request)) |
|
|
33 |
|
|
|
34 |
def searchIndex(request): |
|
|
35 |
|
|
|
36 |
sform = SearchForm(request.POST) |
|
|
37 |
if sform.is_valid(): |
|
|
38 |
search = sform.cleaned_data["search"] |
|
|
39 |
|
|
|
40 |
|
|
|
41 |
queryStr = base64.urlsafe_b64encode(search.encode('utf8')) |
|
|
42 |
field = request.POST["field"] |
|
|
43 |
language_code = request.LANGUAGE_CODE[:2] |
|
|
44 |
|
|
|
45 |
url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, queryStr]) |
|
|
46 |
return render_to_response('ldt/ldt_utils/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': url}, context_instance=RequestContext(request)) |
|
|
47 |
else: |
|
|
48 |
resp = HttpResponse() |
|
|
49 |
resp.write("<html><head></head><body>Error : No result</body></html>"); |
|
|
50 |
|
|
|
51 |
def searchIndexGet(request, field, query): |
|
|
52 |
|
|
|
53 |
language_code = request.LANGUAGE_CODE[:2] |
|
|
54 |
url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, query]) |
|
|
55 |
return render_to_response('irisuser/ldt/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': url}, context_instance=RequestContext(request)) |
|
|
56 |
|
|
|
57 |
def searchInit(request, field, query): |
|
|
58 |
|
|
|
59 |
ldtgen = LdtUtils() |
|
|
60 |
|
|
|
61 |
doc = ldtgen.generateInit([field,query], 'ldt.ldt_utils.views.searchLdt', 'ldt.ldt_utils.views.searchSegments') |
|
|
62 |
|
|
|
63 |
resp = HttpResponse(mimetype="text/xml;charset=utf-8") |
|
|
64 |
xml.dom.ext.PrettyPrint(doc, resp) |
|
|
65 |
return resp |
|
|
66 |
|
|
|
67 |
def searchLdt(request, field, query, edition=None): |
|
|
68 |
|
|
|
69 |
contentList = [] |
|
|
70 |
resp = HttpResponse(mimetype="text/xml") |
|
|
71 |
queryStr = "" |
|
|
72 |
|
|
|
73 |
if query and len(query)>0: |
|
|
74 |
queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8") |
|
|
75 |
searcher = LdtSearch() |
|
|
76 |
ids = {} |
|
|
77 |
|
|
|
78 |
for result in searcher.query(field, queryStr): |
|
|
79 |
ids[result["iri_id"]] = "" |
|
|
80 |
|
|
|
81 |
id_list = ids.keys() |
|
|
82 |
|
|
|
83 |
if edition is not None: |
|
|
84 |
ids_editions = map(lambda t:t[0], filter(lambda id: id[0] is not None, Speak.objects.filter(session__day__edition=edition).order_by("session__start_ts", "order").values_list("content__iri_id"))) |
|
|
85 |
id_list = filter(lambda id: id in id_list, ids_editions) |
|
|
86 |
|
|
|
87 |
contentList = Content.objects.filter(iri_id__in=id_list) |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
ldtgen = LdtUtils() |
|
|
91 |
ldtgen.generateLdt(contentList, file=resp, title = u"Recherche : " + queryStr) |
|
|
92 |
|
|
|
93 |
return resp |
|
|
94 |
|
|
|
95 |
|
|
|
96 |
def searchSegments(request, field, query, edition=None): |
|
|
97 |
|
|
|
98 |
if query and len(query)>0: |
|
|
99 |
searcher = LdtSearch() |
|
|
100 |
|
|
|
101 |
queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8") |
|
|
102 |
res = searcher.query(field, queryStr) |
|
|
103 |
else: |
|
|
104 |
res = [] |
|
|
105 |
|
|
|
106 |
iri_ids = None |
|
|
107 |
|
|
|
108 |
if edition is not None: |
|
|
109 |
iri_ids = map(lambda t:t[0], filter(lambda id: id[0] is not None, Speak.objects.filter(session__day__edition=edition).order_by("session__start_ts", "order").values_list("content__iri_id"))) |
|
|
110 |
|
|
|
111 |
doc = xml.dom.getDOMImplementation().createDocument(None, "iri", None) |
|
|
112 |
|
|
|
113 |
for resultMap in res: |
|
|
114 |
if iri_ids is None or resultMap['iri_id'] in iri_ids: |
|
|
115 |
elem = doc.createElement('seg') |
|
|
116 |
elem.setAttribute('idctt', resultMap['iri_id']) |
|
|
117 |
elem.setAttribute('idens', resultMap['ensemble_id']) |
|
|
118 |
elem.setAttribute('iddec', resultMap['decoupage_id']) |
|
|
119 |
elem.setAttribute('idseg', resultMap['element_id']) |
|
|
120 |
elem.setAttribute('idvue', "") |
|
|
121 |
elem.setAttribute('crit', "") |
|
|
122 |
doc.documentElement.appendChild(elem) |
|
|
123 |
|
|
|
124 |
return HttpResponse(doc.toprettyxml(encoding='utf-8'), mimetype="text/xml;charset=utf-8") |
|
|
125 |
|
|
|
126 |
@login_required |
|
|
127 |
def list_ldt(request): |
|
|
128 |
contents = Content.objects.all() |
|
|
129 |
try: |
|
|
130 |
owner = Owner.objects.get(user=request.user) |
|
|
131 |
except: |
|
|
132 |
return HttpResponseRedirect(settings.LOGIN_URL) |
|
|
133 |
ldtProjects = Project.objects.filter(owner=owner) |
|
|
134 |
context={ |
|
|
135 |
'contents': contents, |
|
40
|
136 |
'projects': ldtProjects.reverse(), |
|
0
|
137 |
} |
|
|
138 |
return render_to_response('ldt/ldt_utils/ldt_list.html', context, context_instance=RequestContext(request)) |
|
|
139 |
|
|
40
|
140 |
@login_required |
|
|
141 |
def list_content(request): |
|
|
142 |
contents = Content.objects.all() |
|
|
143 |
context={ |
|
|
144 |
'contents': contents, |
|
|
145 |
} |
|
|
146 |
return render_to_response('ldt/ldt_utils/content_list.html', context, context_instance=RequestContext(request)) |
|
|
147 |
|
|
41
|
148 |
@login_required |
|
0
|
149 |
def create_ldt_view(request): |
|
|
150 |
if request.method == "POST" : |
|
|
151 |
form = LdtAddForm(request.POST) |
|
|
152 |
if form.is_valid(): |
|
|
153 |
user = request.user |
|
|
154 |
Project.create_project(title=form.cleaned_data['title'], user=user, contents=form.cleaned_data['contents']) |
|
|
155 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
|
156 |
else: |
|
|
157 |
form = LdtAddForm() |
|
|
158 |
contents = Content.objects.all() |
|
|
159 |
return render_to_response('ldt/ldt_utils/create_ldt.html', {'contents': contents, 'form': form,'create_project_action':reverse(create_ldt_view)}, context_instance=RequestContext(request)) |
|
|
160 |
|
|
|
161 |
def created_ldt(request): |
|
|
162 |
return render_to_response('ldt/ldt_utils/done.html', context_instance=RequestContext(request)) |
|
|
163 |
|
|
|
164 |
def indexProject(request, id): |
|
|
165 |
|
|
|
166 |
urlStr = settings.WEB_URL + reverse("space_ldt_init", args=['ldtProject', id]) |
|
|
167 |
posturl= settings.WEB_URL + reverse("ldt.ldt_utils.views.save_ldtProject") |
|
|
168 |
language_code = request.LANGUAGE_CODE[:2] |
|
|
169 |
|
|
|
170 |
ldt = get_object_or_404(Project, ldt_id=id) |
|
|
171 |
if ldt.state ==2: #published |
|
|
172 |
readonly = 'true' |
|
|
173 |
else: |
|
|
174 |
readonly = 'false' |
|
|
175 |
|
|
|
176 |
return render_to_response('ldt/ldt_utils/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': urlStr, 'posturl': posturl, 'id': id, 'readonly': readonly}, context_instance=RequestContext(request)) |
|
|
177 |
|
|
|
178 |
def init(request, method, url): |
|
|
179 |
ldtgen = LdtUtils() |
|
|
180 |
|
|
|
181 |
doc = ldtgen.generateInit([url], 'ldt.ldt_utils.views.'+method, None) |
|
|
182 |
|
|
|
183 |
resp = HttpResponse(mimetype="text/xml") |
|
|
184 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
|
185 |
resp['Pragma']='no-cache' |
|
|
186 |
xml.dom.ext.PrettyPrint(doc, resp) |
|
|
187 |
return resp |
|
|
188 |
|
|
|
189 |
def ldtProject(request, id): |
|
|
190 |
resp = HttpResponse(mimetype="text/xml") |
|
|
191 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
|
192 |
resp['Pragma']='no-cache' |
|
|
193 |
|
|
|
194 |
project = Project.objects.get(ldt_id=id) |
|
|
195 |
resp.write(project.ldt) |
|
|
196 |
return resp |
|
|
197 |
|
|
|
198 |
|
|
|
199 |
def project_json_id(request, id): |
|
|
200 |
|
|
|
201 |
project = get_object_or_404(Project,ldt_id=id) |
|
|
202 |
|
|
|
203 |
return project_json(request, project) |
|
|
204 |
|
|
|
205 |
|
|
|
206 |
def project_json_externalid(request, id): |
|
|
207 |
|
|
|
208 |
res_proj = get_list_or_404(Project.objects.order_by('-modification_date'),contents__external_id = id) |
|
|
209 |
|
|
|
210 |
return project_json(request, res_proj[0]) |
|
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
|
214 |
def project_json(request, project): |
|
|
215 |
|
|
|
216 |
mimetype = request.REQUEST.get("mimetype") |
|
|
217 |
if mimetype is None: |
|
|
218 |
mimetype = "application/json; charset=utf-8" |
|
|
219 |
else: |
|
|
220 |
mimetype = mimetype.encode("utf-8") |
|
|
221 |
if "charset" not in mimetype: |
|
|
222 |
mimetype += "; charset=utf-8" |
|
|
223 |
resp = HttpResponse(mimetype=mimetype) |
|
|
224 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
|
225 |
resp['Pragma']='no-cache' |
|
|
226 |
|
|
|
227 |
indent = request.REQUEST.get("indent") |
|
|
228 |
if indent is None: |
|
|
229 |
indent = settings.LDT_JSON_DEFAULT_INDENT |
|
|
230 |
else: |
|
|
231 |
indent = int(indent) |
|
|
232 |
|
|
|
233 |
callback = request.REQUEST.get("callback") |
|
|
234 |
escape_str = request.REQUEST.get("escape") |
|
|
235 |
escape_bool = False |
|
|
236 |
if escape_str: |
|
|
237 |
escape_bool = {'true': True, 'false': False, "0": False, "1": True}.get(escape_str.lower()) |
|
|
238 |
|
|
|
239 |
|
|
|
240 |
ps = ProjectSerializer(project) |
|
|
241 |
project_dict = ps.serialize_to_cinelab() |
|
|
242 |
|
|
|
243 |
json_str = simplejson.dumps(project_dict, ensure_ascii=False, indent=indent) |
|
|
244 |
|
|
|
245 |
if callback is not None: |
|
|
246 |
json_str = "%s(%s)" % (callback,json_str) |
|
|
247 |
|
|
|
248 |
if escape_bool: |
|
|
249 |
json_str = escape(json_str) |
|
|
250 |
|
|
|
251 |
resp.write(json_str) |
|
|
252 |
|
|
|
253 |
return resp |
|
|
254 |
|
|
|
255 |
|
|
|
256 |
def save_ldtProject(request): |
|
|
257 |
if request.method=="POST": |
|
|
258 |
ldt = request.POST['ldt'] |
|
|
259 |
id = request.POST['id'] |
|
|
260 |
ldtproject=Project.objects.get(ldt_id=id) |
|
|
261 |
#save xml ldt |
|
|
262 |
ldtproject.ldt=ldt |
|
|
263 |
#get new title |
|
|
264 |
dom = xml.dom.minidom.parseString(ldt.encode( "utf-8" )) |
|
|
265 |
con = xml.xpath.Context.Context(dom, 1, 1, None) |
|
|
266 |
result = xml.xpath.Evaluate("/iri/project",context=con) |
|
|
267 |
for pnode in result: |
|
|
268 |
title=pnode.getAttribute("title") |
|
|
269 |
break |
|
|
270 |
#set new title |
|
|
271 |
ldtproject.title=title |
|
|
272 |
#get new content list |
|
|
273 |
new_contents=[] |
|
|
274 |
result = xml.xpath.Evaluate("/iri/medias/media", context=con) |
|
|
275 |
for medianode in result: |
|
|
276 |
id = medianode.attributes['id'].value |
|
|
277 |
new_contents.append(id) |
|
|
278 |
# set new content list |
|
|
279 |
for c in ldtproject.contents.all(): |
|
|
280 |
if not c.iri_id in new_contents: |
|
|
281 |
ldtproject.contents.remove(c) |
|
|
282 |
ldtproject.save() |
|
|
283 |
else: |
|
|
284 |
ldt = '' |
|
|
285 |
return render_to_response('ldt/ldt_utils/save_done.html', {'ldt': ldt, 'id':id, 'title':title, 'contents': new_contents}, context_instance=RequestContext(request)) |
|
|
286 |
|
|
|
287 |
@login_required |
|
40
|
288 |
def publish(request, id, redirect=True): |
|
0
|
289 |
ldt = get_object_or_404(Project, ldt_id=id) |
|
|
290 |
ldt.state = 2 #published |
|
|
291 |
ldt.save() |
|
40
|
292 |
redirect = boolean_convert(redirect) |
|
|
293 |
if redirect: |
|
|
294 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
|
295 |
else: |
|
|
296 |
return HttpResponse(simplejson.dumps({'res':True, 'ldt': {'id': ldt.id, 'state':ldt.state,'ldt_id': ldt.ldt_id}}, ensure_ascii=False),mimetype='application/json') |
|
0
|
297 |
|
|
|
298 |
@login_required |
|
40
|
299 |
def unpublish(request, id, redirect=True): |
|
0
|
300 |
ldt = get_object_or_404(Project, ldt_id=id) |
|
|
301 |
ldt.state = 1 #edition |
|
|
302 |
ldt.save() |
|
40
|
303 |
redirect = boolean_convert(redirect) |
|
|
304 |
if redirect: |
|
|
305 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
|
306 |
else: |
|
|
307 |
return HttpResponse(simplejson.dumps({'res':True, 'ldt': {'id': ldt.id, 'state':ldt.state,'ldt_id': ldt.ldt_id}}, ensure_ascii=False),mimetype='application/json') |
|
0
|
308 |
|
|
|
309 |
|
|
|
310 |
def index(request, url): |
|
|
311 |
|
|
|
312 |
urlStr = settings.WEB_URL + reverse("ldt_init", args=['ldt',url]) |
|
|
313 |
language_code = request.LANGUAGE_CODE[:2] |
|
|
314 |
|
|
|
315 |
return render_to_response('ldt/ldt_utils/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': urlStr, 'weburl':settings.WEB_URL+settings.BASE_URL}, context_instance=RequestContext(request)) |
|
|
316 |
|
|
|
317 |
|
|
|
318 |
def ldt(request, url, startSegment = None): |
|
|
319 |
|
|
|
320 |
import Ft |
|
|
321 |
from Ft.Xml import MarkupWriter |
|
|
322 |
|
|
|
323 |
resp = HttpResponse(mimetype="text/xml; charset=utf-8") |
|
|
324 |
resp['Cache-Control'] = 'no-cache' |
|
|
325 |
|
|
|
326 |
contentList = Content.objects.filter(iri_id=url) |
|
|
327 |
|
|
|
328 |
ldtgen = LdtUtils() |
|
|
329 |
ldtgen.generateLdt(contentList, file=resp, title = contentList[0].title, startSegment=startSegment) |
|
|
330 |
|
|
|
331 |
return resp |
|
|
332 |
|
|
|
333 |
|
|
|
334 |
def loading(request): |
|
|
335 |
return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request)) |
|
|
336 |
|
|
|
337 |
|
|
|
338 |
@login_required |
|
|
339 |
def create_project(request, iri_id): |
|
|
340 |
|
|
|
341 |
content = get_object_or_404(Content, iri_id=iri_id) |
|
|
342 |
contents = [ content, ] |
|
|
343 |
if request.method == "POST" : |
|
|
344 |
form = AddProjectForm(request.POST) |
|
|
345 |
if form.is_valid(): |
|
|
346 |
user=request.user |
|
|
347 |
project = Project.create_project(title=form.cleaned_data['title'], user=user, contents=contents) |
|
|
348 |
return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
|
349 |
else: |
|
|
350 |
form = AddProjectForm() |
|
|
351 |
return render_to_response('ldt/ldt_utils/create_ldt.html', {'form':form, 'contents':contents, 'iri_id':iri_id, 'create_project_action':reverse("ldt.ldt_utils.views.create_project",args=[iri_id])}, context_instance=RequestContext(request)) |
|
|
352 |
|
|
|
353 |
@login_required |
|
|
354 |
def copy_project(request, ldt_id): |
|
|
355 |
|
|
|
356 |
project = get_object_or_404(Project, ldt_id=ldt_id) |
|
|
357 |
if request.method == "POST" : |
|
|
358 |
form = CopyProjectForm(request.POST) |
|
|
359 |
if form.is_valid(): |
|
|
360 |
user=request.user |
|
|
361 |
project = project.copy_project(title=request.POST['title'], user=user) |
|
|
362 |
return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
|
363 |
else: |
|
|
364 |
form = CopyProjectForm |
|
|
365 |
return render_to_response('ldt/ldt_utils/copy_ldt.html', {'form':form, 'project':project}, context_instance=RequestContext(request)) |
|
|
366 |
|
|
41
|
367 |
|
|
|
368 |
def write_content_base(request): |
|
|
369 |
|
|
|
370 |
if request.method =="POST": |
|
|
371 |
form = ContentForm(request.POST) |
|
|
372 |
if form.is_valid(): |
|
|
373 |
content, created = Content.objects.get_or_create(iri_id = form.cleaned_data['iri_id'], defaults = form.cleaned_data) |
|
|
374 |
if not created: |
|
|
375 |
form = ContentForm(request.POST, instance=content) |
|
|
376 |
form.save() |
|
|
377 |
else: |
|
|
378 |
content.save() |
|
|
379 |
form = ContentForm(instance=content) |
|
|
380 |
else: |
|
|
381 |
form = ContentForm() |
|
|
382 |
|
|
|
383 |
return form |
|
|
384 |
|
|
|
385 |
|
|
|
386 |
def write_content(request): |
|
|
387 |
|
|
|
388 |
content_form = write_content_base(request) |
|
|
389 |
|
|
|
390 |
return render_to_response('ldt/ldt_utils/create_content.html', {'content_form': content_form, 'create_content_action': reverse(write_content)}, context_instance=RequestContext(request)) |
|
|
391 |
|
|
|
392 |
|
|
|
393 |
|