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