| author | ymh <ymh.work@gmail.com> |
| Mon, 18 Oct 2010 01:44:12 +0200 | |
| changeset 97 | 66f6aff5c382 |
| parent 89 | 30c6e597a7de |
| child 98 | c9460033138f |
| permissions | -rw-r--r-- |
| 82 | 1 |
from Ft.Xml import MarkupWriter |
2 |
from contentindexer import * |
|
3 |
from django.conf import settings |
|
4 |
from django.contrib.auth.decorators import login_required |
|
5 |
from django.core import serializers |
|
6 |
from django.core.urlresolvers import reverse |
|
|
97
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
7 |
from django.forms.forms import get_declared_fields |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
8 |
from django.forms.models import model_to_dict |
| 82 | 9 |
from django.forms.util import ErrorList |
10 |
from django.http import HttpResponse, HttpResponseRedirect, \ |
|
11 |
HttpResponseForbidden |
|
12 |
from django.shortcuts import render_to_response, get_object_or_404, \ |
|
13 |
get_list_or_404 |
|
| 0 | 14 |
from django.template import RequestContext |
15 |
from django.utils import simplejson |
|
16 |
from django.utils.html import escape |
|
| 82 | 17 |
from django.utils.translation import ugettext as _, ungettext |
| 0 | 18 |
from fileimport import * |
| 82 | 19 |
from forms import LdtImportForm, LdtAddForm, SearchForm, AddProjectForm, \ |
20 |
CopyProjectForm, ContentForm, MediaForm |
|
| 0 | 21 |
from ldt.core.models import Owner |
| 83 | 22 |
from lxml import etree |
| 0 | 23 |
from models import * |
24 |
from projectserializer import * |
|
25 |
from string import Template |
|
| 82 | 26 |
from urllib2 import urlparse |
27 |
from utils import * |
|
| 83 | 28 |
import StringIO |
| 82 | 29 |
import base64 |
| 0 | 30 |
import cgi |
| 82 | 31 |
import django.core.urlresolvers |
32 |
import ldt.auth as ldt_auth |
|
33 |
import ldt.utils.path as ldt_utils_path |
|
34 |
import logging |
|
| 0 | 35 |
import lucene |
| 62 | 36 |
import tempfile |
37 |
import urllib2 |
|
| 0 | 38 |
import uuid |
39 |
||
40 |
def searchForm(request): |
|
41 |
form = SearchForm() |
|
42 |
return render_to_response('ldt/ldt_utils_utils/search_form.html',{'form': form} , context_instance=RequestContext(request)) |
|
43 |
||
44 |
def searchIndex(request): |
|
45 |
||
46 |
sform = SearchForm(request.POST) |
|
47 |
if sform.is_valid(): |
|
48 |
search = sform.cleaned_data["search"] |
|
49 |
||
50 |
||
51 |
queryStr = base64.urlsafe_b64encode(search.encode('utf8')) |
|
52 |
field = request.POST["field"] |
|
53 |
language_code = request.LANGUAGE_CODE[:2] |
|
54 |
||
55 |
url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, queryStr]) |
|
56 |
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)) |
|
57 |
else: |
|
58 |
resp = HttpResponse() |
|
59 |
resp.write("<html><head></head><body>Error : No result</body></html>"); |
|
60 |
||
61 |
def searchIndexGet(request, field, query): |
|
62 |
||
63 |
language_code = request.LANGUAGE_CODE[:2] |
|
64 |
url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, query]) |
|
65 |
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)) |
|
66 |
||
67 |
def searchInit(request, field, query): |
|
68 |
||
69 |
ldtgen = LdtUtils() |
|
70 |
||
71 |
doc = ldtgen.generateInit([field,query], 'ldt.ldt_utils.views.searchLdt', 'ldt.ldt_utils.views.searchSegments') |
|
72 |
||
73 |
resp = HttpResponse(mimetype="text/xml;charset=utf-8") |
|
| 80 | 74 |
doc.write(resp, pretty_print=True) |
| 0 | 75 |
return resp |
76 |
||
77 |
def searchLdt(request, field, query, edition=None): |
|
78 |
||
79 |
contentList = [] |
|
80 |
resp = HttpResponse(mimetype="text/xml") |
|
81 |
queryStr = "" |
|
82 |
||
83 |
if query and len(query)>0: |
|
84 |
queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8") |
|
85 |
searcher = LdtSearch() |
|
86 |
ids = {} |
|
87 |
||
88 |
for result in searcher.query(field, queryStr): |
|
89 |
ids[result["iri_id"]] = "" |
|
90 |
||
91 |
id_list = ids.keys() |
|
92 |
||
93 |
if edition is not None: |
|
94 |
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"))) |
|
95 |
id_list = filter(lambda id: id in id_list, ids_editions) |
|
96 |
||
97 |
contentList = Content.objects.filter(iri_id__in=id_list) |
|
98 |
||
99 |
||
100 |
ldtgen = LdtUtils() |
|
101 |
ldtgen.generateLdt(contentList, file=resp, title = u"Recherche : " + queryStr) |
|
102 |
||
103 |
return resp |
|
104 |
||
105 |
||
106 |
def searchSegments(request, field, query, edition=None): |
|
107 |
||
108 |
if query and len(query)>0: |
|
109 |
searcher = LdtSearch() |
|
110 |
||
111 |
queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8") |
|
112 |
res = searcher.query(field, queryStr) |
|
113 |
else: |
|
114 |
res = [] |
|
115 |
||
116 |
iri_ids = None |
|
117 |
||
118 |
if edition is not None: |
|
119 |
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"))) |
|
120 |
||
| 80 | 121 |
iri = lxml.etree.Element('iri') |
122 |
doc = lxml.etree.ElementTree(iri) |
|
| 0 | 123 |
|
124 |
for resultMap in res: |
|
125 |
if iri_ids is None or resultMap['iri_id'] in iri_ids: |
|
| 80 | 126 |
seg = etree.SubElement(iri, 'seg') |
127 |
seg.set('idctt', resultMap['iri_id']) |
|
128 |
seg.set('idens', resultMap['ensemble_id']) |
|
129 |
seg.set('iddec', resultMap['decoupage_id']) |
|
130 |
seg.set('idseg', resultMap['element_id']) |
|
131 |
seg.set('idvue', "") |
|
132 |
seg.set('crit', "") |
|
| 0 | 133 |
|
| 80 | 134 |
return doc |
135 |
||
136 |
return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
137 |
||
138 |
||
139 |
||
| 0 | 140 |
@login_required |
141 |
def list_ldt(request): |
|
142 |
contents = Content.objects.all() |
|
143 |
try: |
|
144 |
owner = Owner.objects.get(user=request.user) |
|
145 |
except: |
|
146 |
return HttpResponseRedirect(settings.LOGIN_URL) |
|
147 |
ldtProjects = Project.objects.filter(owner=owner) |
|
148 |
context={ |
|
149 |
'contents': contents, |
|
| 40 | 150 |
'projects': ldtProjects.reverse(), |
| 0 | 151 |
} |
152 |
return render_to_response('ldt/ldt_utils/ldt_list.html', context, context_instance=RequestContext(request)) |
|
153 |
||
| 40 | 154 |
@login_required |
155 |
def list_content(request): |
|
156 |
contents = Content.objects.all() |
|
157 |
context={ |
|
158 |
'contents': contents, |
|
159 |
} |
|
160 |
return render_to_response('ldt/ldt_utils/content_list.html', context, context_instance=RequestContext(request)) |
|
161 |
||
| 41 | 162 |
@login_required |
| 0 | 163 |
def create_ldt_view(request): |
164 |
if request.method == "POST" : |
|
165 |
form = LdtAddForm(request.POST) |
|
166 |
if form.is_valid(): |
|
167 |
user = request.user |
|
168 |
Project.create_project(title=form.cleaned_data['title'], user=user, contents=form.cleaned_data['contents']) |
|
| 82 | 169 |
form_status = "saved" |
170 |
contents=[] |
|
171 |
#return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
| 0 | 172 |
else: |
173 |
form = LdtAddForm() |
|
| 82 | 174 |
contents = Content.objects.all() |
175 |
form_status = "none" |
|
176 |
||
177 |
return render_to_response('ldt/ldt_utils/create_ldt.html', {'contents': contents, 'form': form, 'form_status':form_status,'create_project_action':reverse(create_ldt_view)}, context_instance=RequestContext(request)) |
|
| 0 | 178 |
|
179 |
def created_ldt(request): |
|
180 |
return render_to_response('ldt/ldt_utils/done.html', context_instance=RequestContext(request)) |
|
181 |
||
182 |
def indexProject(request, id): |
|
183 |
||
184 |
urlStr = settings.WEB_URL + reverse("space_ldt_init", args=['ldtProject', id]) |
|
185 |
posturl= settings.WEB_URL + reverse("ldt.ldt_utils.views.save_ldtProject") |
|
186 |
language_code = request.LANGUAGE_CODE[:2] |
|
187 |
||
188 |
ldt = get_object_or_404(Project, ldt_id=id) |
|
189 |
if ldt.state ==2: #published |
|
190 |
readonly = 'true' |
|
191 |
else: |
|
192 |
readonly = 'false' |
|
193 |
||
194 |
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)) |
|
195 |
||
196 |
def init(request, method, url): |
|
197 |
ldtgen = LdtUtils() |
|
198 |
||
199 |
doc = ldtgen.generateInit([url], 'ldt.ldt_utils.views.'+method, None) |
|
200 |
||
201 |
resp = HttpResponse(mimetype="text/xml") |
|
202 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
203 |
resp['Pragma']='no-cache' |
|
| 85 | 204 |
|
205 |
resp.write( etree.tostring(doc, pretty_print=True, xml_declaration=True, encoding="utf-8")) |
|
| 0 | 206 |
return resp |
207 |
||
208 |
def ldtProject(request, id): |
|
209 |
resp = HttpResponse(mimetype="text/xml") |
|
210 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
211 |
resp['Pragma']='no-cache' |
|
212 |
||
213 |
project = Project.objects.get(ldt_id=id) |
|
214 |
resp.write(project.ldt) |
|
215 |
return resp |
|
216 |
||
217 |
||
218 |
def project_json_id(request, id): |
|
219 |
||
220 |
project = get_object_or_404(Project,ldt_id=id) |
|
221 |
||
| 85 | 222 |
return project_json(request, project, False) |
| 0 | 223 |
|
224 |
||
225 |
def project_json_externalid(request, id): |
|
226 |
||
227 |
res_proj = get_list_or_404(Project.objects.order_by('-modification_date'),contents__external_id = id) |
|
228 |
||
| 85 | 229 |
return project_json(request, res_proj[0], False) |
| 0 | 230 |
|
231 |
||
232 |
||
| 85 | 233 |
def project_json(request, project, serialize_contents = True): |
|
81
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
234 |
|
|
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
235 |
if not ldt_auth.checkAccess(request.user, project): |
|
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
236 |
return HttpResponseForbidden(_("You can not access this project")) |
|
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
237 |
|
| 0 | 238 |
mimetype = request.REQUEST.get("mimetype") |
239 |
if mimetype is None: |
|
240 |
mimetype = "application/json; charset=utf-8" |
|
241 |
else: |
|
242 |
mimetype = mimetype.encode("utf-8") |
|
243 |
if "charset" not in mimetype: |
|
244 |
mimetype += "; charset=utf-8" |
|
245 |
resp = HttpResponse(mimetype=mimetype) |
|
246 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
247 |
resp['Pragma']='no-cache' |
|
248 |
||
249 |
indent = request.REQUEST.get("indent") |
|
250 |
if indent is None: |
|
251 |
indent = settings.LDT_JSON_DEFAULT_INDENT |
|
252 |
else: |
|
253 |
indent = int(indent) |
|
254 |
||
255 |
callback = request.REQUEST.get("callback") |
|
256 |
escape_str = request.REQUEST.get("escape") |
|
257 |
escape_bool = False |
|
258 |
if escape_str: |
|
259 |
escape_bool = {'true': True, 'false': False, "0": False, "1": True}.get(escape_str.lower()) |
|
260 |
||
261 |
||
| 85 | 262 |
ps = ProjectSerializer(project, serialize_contents) |
| 0 | 263 |
project_dict = ps.serialize_to_cinelab() |
264 |
||
265 |
json_str = simplejson.dumps(project_dict, ensure_ascii=False, indent=indent) |
|
266 |
||
267 |
if callback is not None: |
|
268 |
json_str = "%s(%s)" % (callback,json_str) |
|
269 |
||
270 |
if escape_bool: |
|
271 |
json_str = escape(json_str) |
|
272 |
||
273 |
resp.write(json_str) |
|
274 |
||
275 |
return resp |
|
276 |
||
| 83 | 277 |
def project_annotations_rdf(request, ldt_id): |
278 |
||
279 |
project = Project.objects.get(ldt_id=ldt_id); |
|
280 |
||
281 |
if not ldt_auth.checkAccess(request.user, project): |
|
282 |
return HttpResponseForbidden(_("You can not access this project")) |
|
283 |
||
284 |
mimetype = request.REQUEST.get("mimetype") |
|
285 |
if mimetype is None: |
|
286 |
mimetype = "application/rdf+xml; charset=utf-8" |
|
287 |
else: |
|
288 |
mimetype = mimetype.encode("utf-8") |
|
289 |
if "charset" not in mimetype: |
|
290 |
mimetype += "; charset=utf-8" |
|
291 |
resp = HttpResponse(mimetype=mimetype) |
|
292 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
293 |
resp['Pragma']='no-cache' |
|
294 |
||
|
89
30c6e597a7de
correct project serializer and take into account the display + new version
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
295 |
ps = ProjectSerializer(project, from_contents=False, from_display=True) |
| 83 | 296 |
annotations = project.getAnnotations(first_cutting=True) |
| 88 | 297 |
|
| 83 | 298 |
rdf_ns = u"http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
299 |
dc_ns = u"http://purl.org/dc/elements/1.1/" |
|
300 |
rdf = u"{%s}" % rdf_ns |
|
301 |
dc = u"{%s}" % dc_ns |
|
302 |
nsmap = {u'rdf' : rdf_ns, u'dc':dc_ns} |
|
303 |
||
304 |
rdf_root = etree.Element(rdf+u"RDF", nsmap=nsmap) |
|
305 |
||
306 |
logging.debug("RDF annotations : " + repr(annotations)) |
|
307 |
||
308 |
for annotation in annotations: |
|
309 |
uri = u"" |
|
310 |
if 'uri' in annotation and annotation['uri']: |
|
311 |
uri = unicode(annotation['uri']) |
|
312 |
annot_desc = etree.SubElement(rdf_root, rdf+u"Description") |
|
313 |
annot_desc.set(rdf+u'about',uri) |
|
314 |
if annotation['title']: |
|
315 |
etree.SubElement(annot_desc, dc+'title').text = etree.CDATA(unicode(annotation['title'])) |
|
316 |
if annotation['desc']: |
|
317 |
etree.SubElement(annot_desc, dc+'description').text = etree.CDATA(unicode(annotation['desc'])) |
|
318 |
if annotation['tags']: |
|
319 |
for tag in annotation['tags']: |
|
320 |
etree.SubElement(annot_desc, dc+'subject').text = etree.CDATA(unicode(tag)) |
|
321 |
etree.SubElement(annot_desc,dc+'coverage').text = u"start=%s, duration=%s" % (annotation['begin'], annotation['duration']) |
|
322 |
||
323 |
resp.write(u"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n") |
|
324 |
resp.write(u"<!DOCTYPE rdf:RDF PUBLIC \"-//DUBLIN CORE//DCMES DTD 2002/07/31//EN\" \"http://dublincore.org/documents/2002/07/31/dcmes-xml/dcmes-xml-dtd.dtd\">\n") |
|
325 |
||
326 |
resp.write(etree.tostring(rdf_root, xml_declaration=False, encoding="utf-8", pretty_print=True)) |
|
327 |
||
328 |
return resp |
|
| 0 | 329 |
|
330 |
def save_ldtProject(request): |
|
331 |
if request.method=="POST": |
|
332 |
ldt = request.POST['ldt'] |
|
333 |
id = request.POST['id'] |
|
334 |
ldtproject=Project.objects.get(ldt_id=id) |
|
|
84
6c3162d9e632
Merge with 1e7732f40eee37ec5413bde828a78503fae33b68
ymh <ymh.work@gmail.com>
diff
changeset
|
335 |
|
|
6c3162d9e632
Merge with 1e7732f40eee37ec5413bde828a78503fae33b68
ymh <ymh.work@gmail.com>
diff
changeset
|
336 |
#save xml ldt |
| 0 | 337 |
ldtproject.ldt=ldt |
| 80 | 338 |
|
| 85 | 339 |
|
340 |
doc = lxml.etree.fromstring(ldtproject.ldt.encode( "utf-8" )) |
|
341 |
result = doc.xpath("/iri/project") |
|
342 |
||
343 |
#set new title |
|
344 |
ldtproject.title = result[0].get("title") |
|
345 |
||
346 |
#get new content list |
|
347 |
new_contents=[] |
|
348 |
result = doc.xpath("/iri/medias/media") |
|
349 |
for medianode in result: |
|
350 |
id = medianode.get("id") |
|
351 |
new_contents.append(id) |
|
352 |
||
353 |
#set new content list |
|
354 |
for c in ldtproject.contents.all(): |
|
355 |
if not c.iri_id in new_contents: |
|
356 |
ldtproject.contents.remove(c) |
|
357 |
||
358 |
ldtproject.save() |
|
| 80 | 359 |
else: |
360 |
ldt = '' |
|
| 85 | 361 |
new_contents=[] |
| 80 | 362 |
|
| 85 | 363 |
return render_to_response('ldt/ldt_utils/save_done.html', {'ldt': ldt, 'id':id, 'title':ldtproject.title, 'contents': new_contents}, context_instance=RequestContext(request)) |
| 80 | 364 |
|
365 |
||
366 |
||
| 0 | 367 |
@login_required |
| 40 | 368 |
def publish(request, id, redirect=True): |
| 0 | 369 |
ldt = get_object_or_404(Project, ldt_id=id) |
370 |
ldt.state = 2 #published |
|
371 |
ldt.save() |
|
| 40 | 372 |
redirect = boolean_convert(redirect) |
373 |
if redirect: |
|
374 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
375 |
else: |
|
376 |
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 | 377 |
|
378 |
@login_required |
|
| 40 | 379 |
def unpublish(request, id, redirect=True): |
| 0 | 380 |
ldt = get_object_or_404(Project, ldt_id=id) |
381 |
ldt.state = 1 #edition |
|
382 |
ldt.save() |
|
| 40 | 383 |
redirect = boolean_convert(redirect) |
384 |
if redirect: |
|
385 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
386 |
else: |
|
387 |
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 | 388 |
|
389 |
||
390 |
def index(request, url): |
|
391 |
||
392 |
urlStr = settings.WEB_URL + reverse("ldt_init", args=['ldt',url]) |
|
393 |
language_code = request.LANGUAGE_CODE[:2] |
|
394 |
||
395 |
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)) |
|
396 |
||
397 |
||
398 |
def ldt(request, url, startSegment = None): |
|
399 |
||
400 |
||
401 |
resp = HttpResponse(mimetype="text/xml; charset=utf-8") |
|
402 |
resp['Cache-Control'] = 'no-cache' |
|
403 |
||
404 |
contentList = Content.objects.filter(iri_id=url) |
|
405 |
||
406 |
ldtgen = LdtUtils() |
|
407 |
ldtgen.generateLdt(contentList, file=resp, title = contentList[0].title, startSegment=startSegment) |
|
408 |
||
409 |
return resp |
|
410 |
||
411 |
||
412 |
def loading(request): |
|
413 |
return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request)) |
|
414 |
||
415 |
||
416 |
@login_required |
|
417 |
def create_project(request, iri_id): |
|
418 |
||
419 |
content = get_object_or_404(Content, iri_id=iri_id) |
|
420 |
contents = [ content, ] |
|
421 |
if request.method == "POST" : |
|
422 |
form = AddProjectForm(request.POST) |
|
423 |
if form.is_valid(): |
|
424 |
user=request.user |
|
425 |
project = Project.create_project(title=form.cleaned_data['title'], user=user, contents=contents) |
|
426 |
return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
427 |
else: |
|
428 |
form = AddProjectForm() |
|
| 82 | 429 |
return render_to_response('ldt/ldt_utils/create_ldt.html', {'form':form, 'contents':contents, 'create_project_action':reverse("ldt.ldt_utils.views.create_project",args=[iri_id])}, context_instance=RequestContext(request)) |
430 |
||
431 |
@login_required |
|
432 |
def update_project(request, ldt_id): |
|
433 |
||
434 |
project = get_object_or_404(Project, ldt_id=ldt_id) |
|
435 |
contents = project.contents.all() |
|
436 |
if request.method == "POST" : |
|
437 |
submit_action = request.REQUEST.get("submit_button",False) |
|
438 |
if submit_action == "prepare_delete": |
|
439 |
errors = [] |
|
440 |
if project.state == 2: |
|
441 |
errors.append(_("the project %(title)s is published. please unpublish before deleting.")%{'title':project.title}) |
|
442 |
message = _("can not delete the project. Please correct the following error") |
|
443 |
title = _('title error deleting project') |
|
444 |
else: |
|
445 |
message = _("please confirm deleting project %(title)s")%{'title':project.title} |
|
446 |
title = _("confirm deletion") |
|
447 |
return render_to_response('ldt/ldt_utils/error_confirm.html', {'errors':errors, 'message':message, 'title': title}, context_instance=RequestContext(request)) |
|
448 |
elif submit_action == "delete": |
|
449 |
if project.state != 2: |
|
450 |
project.delete() |
|
451 |
form_status= 'deleted' |
|
452 |
form = AddProjectForm() |
|
453 |
else: |
|
454 |
form_status= 'saved' |
|
455 |
form = AddProjectForm(request.POST) |
|
456 |
if form.is_valid(): |
|
457 |
project.title=form.cleaned_data['title'] |
|
458 |
project.save() |
|
459 |
else: |
|
460 |
form = AddProjectForm({'title':unicode(project.title)}) |
|
461 |
form_status= 'none' |
|
462 |
||
463 |
return render_to_response('ldt/ldt_utils/create_ldt.html', {'form':form, 'form_status':form_status, 'ldt_id': ldt_id, 'contents':contents, 'create_project_action':reverse("ldt.ldt_utils.views.update_project",args=[ldt_id])}, context_instance=RequestContext(request)) |
|
464 |
||
| 0 | 465 |
|
466 |
@login_required |
|
467 |
def copy_project(request, ldt_id): |
|
468 |
||
469 |
project = get_object_or_404(Project, ldt_id=ldt_id) |
|
470 |
if request.method == "POST" : |
|
471 |
form = CopyProjectForm(request.POST) |
|
472 |
if form.is_valid(): |
|
473 |
user=request.user |
|
474 |
project = project.copy_project(title=request.POST['title'], user=user) |
|
475 |
return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
476 |
else: |
|
477 |
form = CopyProjectForm |
|
478 |
return render_to_response('ldt/ldt_utils/copy_ldt.html', {'form':form, 'project':project}, context_instance=RequestContext(request)) |
|
479 |
||
| 41 | 480 |
|
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
481 |
def write_content_base(request, iri_id=None): |
| 67 | 482 |
|
| 82 | 483 |
logging.debug(repr(request)) |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
484 |
if iri_id: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
485 |
instance_content = Content.objects.get(iri_id=iri_id) |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
486 |
instance_media = instance_content.media_obj |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
487 |
logging.debug("write_content_base : valid form: for instance : media -> " + repr(instance_media) + " content : for instance : " + repr(instance_content) ) |
| 67 | 488 |
else: |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
489 |
logging.debug("No iri_id") |
| 67 | 490 |
instance_content = None |
491 |
instance_media = None |
|
| 42 | 492 |
|
493 |
form_status= 'none' |
|
| 41 | 494 |
if request.method =="POST": |
|
97
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
495 |
|
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
496 |
content_instance_val = model_to_dict(instance_content,exclude=ContentForm.Meta.exclude) |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
497 |
media_instance_val = model_to_dict(instance_media, exclude=MediaForm.Meta.exclude) |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
498 |
#add prefix |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
499 |
|
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
500 |
def add_prefix(dict, prefix): |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
501 |
for key,value in dict.items(): |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
502 |
dict['%s-%s' % (prefix, key)] = value |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
503 |
del(dict[key]) |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
504 |
|
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
505 |
add_prefix(content_instance_val, "content") |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
506 |
add_prefix(media_instance_val, "media") |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
507 |
|
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
508 |
for k in request.POST.keys(): |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
509 |
value = request.POST.get(k) |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
510 |
content_instance_val[k] = value |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
511 |
media_instance_val[k] = value |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
512 |
|
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
513 |
content_form = ContentForm(content_instance_val, prefix="content", instance=instance_content) |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
514 |
media_form = MediaForm(media_instance_val, request.FILES, prefix="media", instance=instance_media) |
|
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
515 |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
516 |
media_valid = media_form.is_valid() |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
517 |
content_valid = content_form.is_valid() |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
518 |
|
|
97
66f6aff5c382
corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents:
89
diff
changeset
|
519 |
logging.debug("write_content_base : valid form: for instance : " + repr(instance_media) + " -> media " + str(media_valid) +" content : for instance : " + repr(instance_content) + " : " + str(content_valid) ) |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
520 |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
521 |
if media_valid and content_valid : |
| 62 | 522 |
|
523 |
# see if media must be created |
|
524 |
cleaned_data = {} |
|
525 |
cleaned_data.update(media_form.cleaned_data) |
|
526 |
||
527 |
media_input_type = content_form.cleaned_data["media_input_type"] |
|
528 |
||
529 |
if media_input_type == "none": |
|
530 |
media = None |
|
531 |
elif media_input_type == "link": |
|
532 |
media = content_form.cleaned_data["media_obj"] |
|
|
81
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
533 |
created = False |
| 82 | 534 |
elif media_input_type == "create": |
535 |
del cleaned_data["media_file"] |
|
536 |
if not cleaned_data['videopath']: |
|
537 |
cleaned_data['videopath'] = settings.STREAM_URL |
|
538 |
media, created = Media.objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data) |
|
539 |
elif media_input_type == "url" or media_input_type == "upload" : |
|
| 62 | 540 |
# copy file |
541 |
#complet src |
|
542 |
destination_file = None |
|
543 |
source_file = None |
|
544 |
destination_file_path = None |
|
| 82 | 545 |
try: |
546 |
if media_input_type == "url": |
|
547 |
url = cleaned_data["external_src_url"] |
|
548 |
source_file = urllib2.urlopen(url) |
|
549 |
source_filename = source_file.info().get('Content-Disposition', None) |
|
550 |
if not source_filename: |
|
551 |
source_filename = urlparse.urlparse(url).path.rstrip("/").split('/')[-1] |
|
552 |
elif media_input_type == "upload": |
|
553 |
source_file = request.FILES['media-media_file'] |
|
554 |
source_filename = source_file.name |
|
555 |
||
556 |
source_filename = ldt_utils_path.sanitize_filename(source_filename) |
|
557 |
destination_filepath = os.path.join(settings.STREAM_PATH, source_filename) |
|
558 |
base_source_filename = source_filename |
|
559 |
extension = base_source_filename.split(".")[-1] |
|
560 |
if extension == base_source_filename: |
|
561 |
extension = "" |
|
562 |
base_basename_filename = base_source_filename |
|
563 |
else: |
|
564 |
base_basename_filename = base_source_filename[:-1 *(len(extension)+1)] |
|
565 |
i = 0 |
|
566 |
while os.path.exists(destination_filepath): |
|
567 |
base_source_filename = "%s.%d.%s" % (base_basename_filename,i,extension) |
|
568 |
destination_filepath = os.path.join(settings.STREAM_PATH, base_source_filename) |
|
569 |
i += 1 |
|
| 62 | 570 |
|
| 82 | 571 |
destination_file = open(destination_filepath, "w") |
572 |
src_prefix = settings.STREAM_SRC_PREFIX.rstrip("/") |
|
573 |
if len(src_prefix) > 0: |
|
574 |
cleaned_data["src"] = src_prefix + "/" + base_source_filename |
|
575 |
else: |
|
576 |
cleaned_data["src"] = base_source_filename |
|
577 |
||
578 |
chunck = source_file.read(2048) |
|
579 |
while chunck: |
|
580 |
destination_file.write(chunck) |
|
| 62 | 581 |
chunck = source_file.read(2048) |
| 82 | 582 |
|
583 |
except Exception as inst: |
|
584 |
logging.debug("write_content_base : POST error when processing file:" + str(inst)) |
|
585 |
form_status = "error" |
|
586 |
#set error for form |
|
587 |
if media_input_type == "url": |
|
588 |
errors = media_form._errors.setdefault("external_src_url", ErrorList()) |
|
589 |
errors.append(_("Problem when downloading file from url : ")+str(inst)) |
|
590 |
elif media_input_type == "upload": |
|
591 |
errors = media_form._errors.setdefault("media_file", ErrorList()) |
|
592 |
errors.append(_("Problem when uploading file : ") + str(inst)) |
|
593 |
finally: |
|
594 |
if destination_file: |
|
595 |
destination_file.close() |
|
596 |
if source_file: |
|
597 |
source_file.close() |
|
| 62 | 598 |
|
599 |
if form_status != "error": |
|
600 |
#try: |
|
601 |
del cleaned_data["media_file"] |
|
602 |
if not cleaned_data['videopath']: |
|
603 |
cleaned_data['videopath'] = settings.STREAM_URL |
|
604 |
media, created = Media.objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data) |
|
|
81
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
605 |
else: |
|
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
606 |
media = None |
|
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
607 |
|
|
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
608 |
if media and not created: |
|
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
609 |
for attribute in ('external_id', 'external_permalink', 'external_publication_url', 'external_src_url', 'media_creation_date', 'videopath', 'duration', 'description', 'title'): |
|
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
610 |
setattr(media, attribute, cleaned_data.get(attribute)) |
|
97b12f5f2c7a
first version of embed and auth. plan to implement rbac
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
611 |
media.save() |
| 62 | 612 |
#except Exception as inst: |
613 |
# logging.debug("write_content_base : POST error when saving media:" + str(inst)) |
|
614 |
# form_status = "error" |
|
615 |
#TODO: set error message |
|
616 |
#media_form.errors = _("Error when saving the media : " + e.message) |
|
617 |
||
618 |
#if needed preparetemp file and copy temp file to destination |
|
619 |
||
620 |
||
621 |
if form_status != "error": |
|
622 |
#try: |
|
623 |
content_defaults = {} |
|
624 |
content_defaults.update(content_form.cleaned_data) |
|
625 |
content_defaults['media_obj'] = media |
|
626 |
del content_defaults["media_input_type"] |
|
627 |
content, created = Content.objects.get_or_create(iri_id = content_form.cleaned_data['iri_id'], defaults = content_defaults) |
|
628 |
if not created: |
|
629 |
||
630 |
for attribute in ('iriurl', 'title', 'description', 'duration', 'content_creation_date', 'tags', 'media_obj'): |
|
631 |
setattr(content, attribute, content_defaults[attribute]) |
|
632 |
content.save() |
|
633 |
form_status = 'saved' |
|
634 |
media_form = MediaForm(instance=media, prefix="media") |
|
635 |
content_form = ContentForm(instance=content, prefix="content") |
|
636 |
#except: |
|
637 |
#logging.debug("write_content_base : POST error when saving content:" + str(inst)) |
|
638 |
#form_status = "error" |
|
639 |
#TODO : set error on content form |
|
| 42 | 640 |
else: |
641 |
form_status = 'error' |
|
| 41 | 642 |
else: |
| 42 | 643 |
form_status = 'empty' |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
644 |
initial = { 'media_input_type':"link"} |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
645 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
646 |
content_form = ContentForm(prefix="content", instance=instance_content, initial = initial ) |
| 67 | 647 |
media_form = MediaForm(prefix="media", instance=instance_media) |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
648 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
649 |
if instance_content is not None: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
650 |
content_form.media_input_type = "link" |
| 41 | 651 |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
652 |
return content_form, media_form, form_status |
| 41 | 653 |
|
| 82 | 654 |
@login_required |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
655 |
def write_content(request, iri_id=None): |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
656 |
|
| 82 | 657 |
submit_action = request.REQUEST.get("submit_button",False) |
658 |
||
659 |
if submit_action == "prepare_delete": |
|
660 |
errors, titles = prepare_delete_content(request, iri_id) |
|
661 |
if errors and len(errors) > 0: |
|
662 |
message = ungettext("There is %(count)d error when deleting content", "There are %(count)d errors when deleting content", len(errors)) % { 'count': len(errors)} |
|
663 |
title_msg = _('title error deleting content') |
|
664 |
else: |
|
665 |
message = _("Confirm delete content %(titles)s") % { 'titles' : ",".join(titles) } |
|
666 |
title_msg = _("confirm delete content") |
|
667 |
return render_to_response('ldt/ldt_utils/error_confirm.html', {'errors':errors, 'message':message, 'title': title_msg}, context_instance=RequestContext(request)) |
|
668 |
elif submit_action == "delete": |
|
669 |
delete_content(request, iri_id) |
|
670 |
form_status = "deleted" |
|
671 |
content_form = ContentForm() |
|
672 |
media_form = MediaForm() |
|
673 |
else: |
|
674 |
content_form, media_form, form_status = write_content_base(request, iri_id) |
|
675 |
||
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
676 |
if iri_id: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
677 |
create_content_action = reverse(write_content, kwargs={'iri_id':iri_id}) |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
678 |
else: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
679 |
create_content_action = reverse(write_content) |
| 82 | 680 |
|
| 41 | 681 |
|
| 82 | 682 |
return render_to_response('ldt/ldt_utils/create_content.html', {'content_form': content_form, 'media_form': media_form,'form_status': form_status,'create_content_action': create_content_action, 'iri_id': iri_id}, context_instance=RequestContext(request)) |
683 |
||
684 |
@login_required |
|
685 |
def prepare_delete_content(request, iri_id=None): |
|
686 |
errors = [] |
|
687 |
titles = [] |
|
688 |
if not iri_id: |
|
689 |
iri_id = request.REQUEST.get("iri_id", None) |
|
690 |
||
691 |
if iri_id: |
|
692 |
for content in Content.objects.filter(iri_id=iri_id): |
|
693 |
titles.append(unicode(content.title)) |
|
694 |
projects = Content.objects.all()[0].project_set.all() |
|
695 |
projects_nb = len(projects) |
|
696 |
if projects_nb > 0: |
|
697 |
project_titles = [lambda p: p.title for p in projects] |
|
698 |
errors.append(ungettext("Content '%(title)s' is referenced by this project : %(project_titles)s. Please delete it beforehand.", "Content '%(title)s' is referenced by %(count)d projects: %(project_titles)s. Please delete them beforehand.", projects_nb)%{'title':content.title,'count':projects_nb, 'project_titles': ",".join(project_titles)}) |
|
699 |
||
700 |
return errors, titles |
|
| 41 | 701 |
|
702 |
||
| 82 | 703 |
@login_required |
704 |
def delete_content(request, iri_id=None): |
|
705 |
if not iri_id: |
|
706 |
iri_id = request.REQUEST.get("iri_id", None) |
|
707 |
||
708 |
if iri_id: |
|
709 |
Content.objects.filter(iri_id=iri_id).delete() |
|
| 80 | 710 |