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