|
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 * |
|
12 from forms import LdtImportForm, LdtAddForm, SearchForm, AddProjectForm, CopyProjectForm |
|
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, |
|
136 'ldtProjects': ldtProjects.reverse(), |
|
137 } |
|
138 return render_to_response('ldt/ldt_utils/ldt_list.html', context, context_instance=RequestContext(request)) |
|
139 |
|
140 def create_ldt_view(request): |
|
141 if request.method == "POST" : |
|
142 form = LdtAddForm(request.POST) |
|
143 if form.is_valid(): |
|
144 user = request.user |
|
145 Project.create_project(title=form.cleaned_data['title'], user=user, contents=form.cleaned_data['contents']) |
|
146 return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
147 else: |
|
148 form = LdtAddForm() |
|
149 contents = Content.objects.all() |
|
150 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)) |
|
151 |
|
152 def created_ldt(request): |
|
153 return render_to_response('ldt/ldt_utils/done.html', context_instance=RequestContext(request)) |
|
154 |
|
155 def indexProject(request, id): |
|
156 |
|
157 urlStr = settings.WEB_URL + reverse("space_ldt_init", args=['ldtProject', id]) |
|
158 posturl= settings.WEB_URL + reverse("ldt.ldt_utils.views.save_ldtProject") |
|
159 language_code = request.LANGUAGE_CODE[:2] |
|
160 |
|
161 ldt = get_object_or_404(Project, ldt_id=id) |
|
162 if ldt.state ==2: #published |
|
163 readonly = 'true' |
|
164 else: |
|
165 readonly = 'false' |
|
166 |
|
167 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)) |
|
168 |
|
169 def init(request, method, url): |
|
170 ldtgen = LdtUtils() |
|
171 |
|
172 doc = ldtgen.generateInit([url], 'ldt.ldt_utils.views.'+method, None) |
|
173 |
|
174 resp = HttpResponse(mimetype="text/xml") |
|
175 resp['Cache-Control']='no-cache, must-revalidate' |
|
176 resp['Pragma']='no-cache' |
|
177 xml.dom.ext.PrettyPrint(doc, resp) |
|
178 return resp |
|
179 |
|
180 def ldtProject(request, id): |
|
181 resp = HttpResponse(mimetype="text/xml") |
|
182 resp['Cache-Control']='no-cache, must-revalidate' |
|
183 resp['Pragma']='no-cache' |
|
184 |
|
185 project = Project.objects.get(ldt_id=id) |
|
186 resp.write(project.ldt) |
|
187 return resp |
|
188 |
|
189 |
|
190 def project_json_id(request, id): |
|
191 |
|
192 project = get_object_or_404(Project,ldt_id=id) |
|
193 |
|
194 return project_json(request, project) |
|
195 |
|
196 |
|
197 def project_json_externalid(request, id): |
|
198 |
|
199 res_proj = get_list_or_404(Project.objects.order_by('-modification_date'),contents__external_id = id) |
|
200 |
|
201 return project_json(request, res_proj[0]) |
|
202 |
|
203 |
|
204 |
|
205 def project_json(request, project): |
|
206 |
|
207 mimetype = request.REQUEST.get("mimetype") |
|
208 if mimetype is None: |
|
209 mimetype = "application/json; charset=utf-8" |
|
210 else: |
|
211 mimetype = mimetype.encode("utf-8") |
|
212 if "charset" not in mimetype: |
|
213 mimetype += "; charset=utf-8" |
|
214 resp = HttpResponse(mimetype=mimetype) |
|
215 resp['Cache-Control']='no-cache, must-revalidate' |
|
216 resp['Pragma']='no-cache' |
|
217 |
|
218 indent = request.REQUEST.get("indent") |
|
219 if indent is None: |
|
220 indent = settings.LDT_JSON_DEFAULT_INDENT |
|
221 else: |
|
222 indent = int(indent) |
|
223 |
|
224 callback = request.REQUEST.get("callback") |
|
225 escape_str = request.REQUEST.get("escape") |
|
226 escape_bool = False |
|
227 if escape_str: |
|
228 escape_bool = {'true': True, 'false': False, "0": False, "1": True}.get(escape_str.lower()) |
|
229 |
|
230 |
|
231 ps = ProjectSerializer(project) |
|
232 project_dict = ps.serialize_to_cinelab() |
|
233 |
|
234 json_str = simplejson.dumps(project_dict, ensure_ascii=False, indent=indent) |
|
235 |
|
236 if callback is not None: |
|
237 json_str = "%s(%s)" % (callback,json_str) |
|
238 |
|
239 if escape_bool: |
|
240 json_str = escape(json_str) |
|
241 |
|
242 resp.write(json_str) |
|
243 |
|
244 return resp |
|
245 |
|
246 |
|
247 def save_ldtProject(request): |
|
248 if request.method=="POST": |
|
249 ldt = request.POST['ldt'] |
|
250 id = request.POST['id'] |
|
251 ldtproject=Project.objects.get(ldt_id=id) |
|
252 #save xml ldt |
|
253 ldtproject.ldt=ldt |
|
254 #get new title |
|
255 dom = xml.dom.minidom.parseString(ldt.encode( "utf-8" )) |
|
256 con = xml.xpath.Context.Context(dom, 1, 1, None) |
|
257 result = xml.xpath.Evaluate("/iri/project",context=con) |
|
258 for pnode in result: |
|
259 title=pnode.getAttribute("title") |
|
260 break |
|
261 #set new title |
|
262 ldtproject.title=title |
|
263 #get new content list |
|
264 new_contents=[] |
|
265 result = xml.xpath.Evaluate("/iri/medias/media", context=con) |
|
266 for medianode in result: |
|
267 id = medianode.attributes['id'].value |
|
268 new_contents.append(id) |
|
269 # set new content list |
|
270 for c in ldtproject.contents.all(): |
|
271 if not c.iri_id in new_contents: |
|
272 ldtproject.contents.remove(c) |
|
273 ldtproject.save() |
|
274 else: |
|
275 ldt = '' |
|
276 return render_to_response('ldt/ldt_utils/save_done.html', {'ldt': ldt, 'id':id, 'title':title, 'contents': new_contents}, context_instance=RequestContext(request)) |
|
277 |
|
278 @login_required |
|
279 def publish(request, id): |
|
280 ldt = get_object_or_404(Project, ldt_id=id) |
|
281 ldt.state = 2 #published |
|
282 ldt.save() |
|
283 return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
284 |
|
285 @login_required |
|
286 def unpublish(request, id): |
|
287 ldt = get_object_or_404(Project, ldt_id=id) |
|
288 ldt.state = 1 #edition |
|
289 ldt.save() |
|
290 return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
291 |
|
292 |
|
293 def index(request, url): |
|
294 |
|
295 urlStr = settings.WEB_URL + reverse("ldt_init", args=['ldt',url]) |
|
296 language_code = request.LANGUAGE_CODE[:2] |
|
297 |
|
298 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)) |
|
299 |
|
300 |
|
301 def ldt(request, url, startSegment = None): |
|
302 |
|
303 import Ft |
|
304 from Ft.Xml import MarkupWriter |
|
305 |
|
306 resp = HttpResponse(mimetype="text/xml; charset=utf-8") |
|
307 resp['Cache-Control'] = 'no-cache' |
|
308 |
|
309 contentList = Content.objects.filter(iri_id=url) |
|
310 |
|
311 ldtgen = LdtUtils() |
|
312 ldtgen.generateLdt(contentList, file=resp, title = contentList[0].title, startSegment=startSegment) |
|
313 |
|
314 return resp |
|
315 |
|
316 |
|
317 def loading(request): |
|
318 return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request)) |
|
319 |
|
320 |
|
321 @login_required |
|
322 def create_project(request, iri_id): |
|
323 |
|
324 content = get_object_or_404(Content, iri_id=iri_id) |
|
325 contents = [ content, ] |
|
326 if request.method == "POST" : |
|
327 form = AddProjectForm(request.POST) |
|
328 if form.is_valid(): |
|
329 user=request.user |
|
330 project = Project.create_project(title=form.cleaned_data['title'], user=user, contents=contents) |
|
331 return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
332 else: |
|
333 form = AddProjectForm() |
|
334 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)) |
|
335 |
|
336 @login_required |
|
337 def copy_project(request, ldt_id): |
|
338 |
|
339 project = get_object_or_404(Project, ldt_id=ldt_id) |
|
340 if request.method == "POST" : |
|
341 form = CopyProjectForm(request.POST) |
|
342 if form.is_valid(): |
|
343 user=request.user |
|
344 project = project.copy_project(title=request.POST['title'], user=user) |
|
345 return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
346 else: |
|
347 form = CopyProjectForm |
|
348 return render_to_response('ldt/ldt_utils/copy_ldt.html', {'form':form, 'project':project}, context_instance=RequestContext(request)) |
|
349 |