| author | wakimd |
| Tue, 28 Sep 2010 18:24:14 +0200 | |
| changeset 80 | 1e7732f40eee |
| parent 71 | fc1210bbb854 |
| child 84 | 6c3162d9e632 |
| child 91 | 9c83809fda01 |
| permissions | -rw-r--r-- |
| 0 | 1 |
import django.core.urlresolvers |
2 |
from django.http import HttpResponse, HttpResponseRedirect |
|
3 |
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404 |
|
4 |
from django.template import RequestContext |
|
5 |
from django.core.urlresolvers import reverse |
|
6 |
from django.contrib.auth.decorators import login_required |
|
7 |
from django.conf import settings |
|
8 |
from django.core import serializers |
|
9 |
from django.utils import simplejson |
|
10 |
from django.utils.html import escape |
|
11 |
from fileimport import * |
|
| 62 | 12 |
from forms import LdtImportForm, LdtAddForm, SearchForm, AddProjectForm, CopyProjectForm, ContentForm, MediaForm |
| 0 | 13 |
from ldt.core.models import Owner |
14 |
from models import * |
|
15 |
from utils import * |
|
16 |
from contentindexer import * |
|
17 |
from projectserializer import * |
|
18 |
from string import Template |
|
19 |
import cgi |
|
20 |
import uuid |
|
21 |
import base64 |
|
22 |
import lucene |
|
| 62 | 23 |
import tempfile |
24 |
import urllib2 |
|
25 |
from urllib2 import urlparse |
|
26 |
from jogging import logging |
|
| 80 | 27 |
import lxml.etree |
| 0 | 28 |
|
29 |
||
30 |
def searchForm(request): |
|
31 |
form = SearchForm() |
|
32 |
return render_to_response('ldt/ldt_utils_utils/search_form.html',{'form': form} , context_instance=RequestContext(request)) |
|
33 |
||
34 |
def searchIndex(request): |
|
35 |
||
36 |
sform = SearchForm(request.POST) |
|
37 |
if sform.is_valid(): |
|
38 |
search = sform.cleaned_data["search"] |
|
39 |
||
40 |
||
41 |
queryStr = base64.urlsafe_b64encode(search.encode('utf8')) |
|
42 |
field = request.POST["field"] |
|
43 |
language_code = request.LANGUAGE_CODE[:2] |
|
44 |
||
45 |
url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, queryStr]) |
|
46 |
return render_to_response('ldt/ldt_utils/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': url}, context_instance=RequestContext(request)) |
|
47 |
else: |
|
48 |
resp = HttpResponse() |
|
49 |
resp.write("<html><head></head><body>Error : No result</body></html>"); |
|
50 |
||
51 |
def searchIndexGet(request, field, query): |
|
52 |
||
53 |
language_code = request.LANGUAGE_CODE[:2] |
|
54 |
url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, query]) |
|
55 |
return render_to_response('irisuser/ldt/init_ldt.html', {'MEDIA_URL': settings.MEDIA_URL, 'colorurl': settings.MEDIA_URL+'swf/ldt/pkg/color.xml', 'i18nurl': settings.MEDIA_URL+'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.MEDIA_URL+'swf/ldt/', 'url': url}, context_instance=RequestContext(request)) |
|
56 |
||
57 |
def searchInit(request, field, query): |
|
58 |
||
59 |
ldtgen = LdtUtils() |
|
60 |
||
61 |
doc = ldtgen.generateInit([field,query], 'ldt.ldt_utils.views.searchLdt', 'ldt.ldt_utils.views.searchSegments') |
|
62 |
||
63 |
resp = HttpResponse(mimetype="text/xml;charset=utf-8") |
|
| 80 | 64 |
doc.write(resp, pretty_print=True) |
| 0 | 65 |
return resp |
66 |
||
67 |
def searchLdt(request, field, query, edition=None): |
|
68 |
||
69 |
contentList = [] |
|
70 |
resp = HttpResponse(mimetype="text/xml") |
|
71 |
queryStr = "" |
|
72 |
||
73 |
if query and len(query)>0: |
|
74 |
queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8") |
|
75 |
searcher = LdtSearch() |
|
76 |
ids = {} |
|
77 |
||
78 |
for result in searcher.query(field, queryStr): |
|
79 |
ids[result["iri_id"]] = "" |
|
80 |
||
81 |
id_list = ids.keys() |
|
82 |
||
83 |
if edition is not None: |
|
84 |
ids_editions = map(lambda t:t[0], filter(lambda id: id[0] is not None, Speak.objects.filter(session__day__edition=edition).order_by("session__start_ts", "order").values_list("content__iri_id"))) |
|
85 |
id_list = filter(lambda id: id in id_list, ids_editions) |
|
86 |
||
87 |
contentList = Content.objects.filter(iri_id__in=id_list) |
|
88 |
||
89 |
||
90 |
ldtgen = LdtUtils() |
|
91 |
ldtgen.generateLdt(contentList, file=resp, title = u"Recherche : " + queryStr) |
|
92 |
||
93 |
return resp |
|
94 |
||
95 |
||
96 |
def searchSegments(request, field, query, edition=None): |
|
97 |
||
98 |
if query and len(query)>0: |
|
99 |
searcher = LdtSearch() |
|
100 |
||
101 |
queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8") |
|
102 |
res = searcher.query(field, queryStr) |
|
103 |
else: |
|
104 |
res = [] |
|
105 |
||
106 |
iri_ids = None |
|
107 |
||
108 |
if edition is not None: |
|
109 |
iri_ids = map(lambda t:t[0], filter(lambda id: id[0] is not None, Speak.objects.filter(session__day__edition=edition).order_by("session__start_ts", "order").values_list("content__iri_id"))) |
|
110 |
||
| 80 | 111 |
iri = lxml.etree.Element('iri') |
112 |
doc = lxml.etree.ElementTree(iri) |
|
| 0 | 113 |
|
114 |
for resultMap in res: |
|
115 |
if iri_ids is None or resultMap['iri_id'] in iri_ids: |
|
| 80 | 116 |
seg = etree.SubElement(iri, 'seg') |
117 |
seg.set('idctt', resultMap['iri_id']) |
|
118 |
seg.set('idens', resultMap['ensemble_id']) |
|
119 |
seg.set('iddec', resultMap['decoupage_id']) |
|
120 |
seg.set('idseg', resultMap['element_id']) |
|
121 |
seg.set('idvue', "") |
|
122 |
seg.set('crit', "") |
|
| 0 | 123 |
|
| 80 | 124 |
return doc |
125 |
||
126 |
return HttpResponse(lxml.etree.tostring(doc, pretty_print=True), mimetype="text/xml;charset=utf-8") |
|
127 |
||
128 |
||
129 |
||
| 0 | 130 |
@login_required |
131 |
def list_ldt(request): |
|
132 |
contents = Content.objects.all() |
|
133 |
try: |
|
134 |
owner = Owner.objects.get(user=request.user) |
|
135 |
except: |
|
136 |
return HttpResponseRedirect(settings.LOGIN_URL) |
|
137 |
ldtProjects = Project.objects.filter(owner=owner) |
|
138 |
context={ |
|
139 |
'contents': contents, |
|
| 40 | 140 |
'projects': ldtProjects.reverse(), |
| 0 | 141 |
} |
142 |
return render_to_response('ldt/ldt_utils/ldt_list.html', context, context_instance=RequestContext(request)) |
|
143 |
||
| 40 | 144 |
@login_required |
145 |
def list_content(request): |
|
146 |
contents = Content.objects.all() |
|
147 |
context={ |
|
148 |
'contents': contents, |
|
149 |
} |
|
150 |
return render_to_response('ldt/ldt_utils/content_list.html', context, context_instance=RequestContext(request)) |
|
151 |
||
| 41 | 152 |
@login_required |
| 0 | 153 |
def create_ldt_view(request): |
154 |
if request.method == "POST" : |
|
155 |
form = LdtAddForm(request.POST) |
|
156 |
if form.is_valid(): |
|
157 |
user = request.user |
|
158 |
Project.create_project(title=form.cleaned_data['title'], user=user, contents=form.cleaned_data['contents']) |
|
159 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
160 |
else: |
|
161 |
form = LdtAddForm() |
|
162 |
contents = Content.objects.all() |
|
163 |
return render_to_response('ldt/ldt_utils/create_ldt.html', {'contents': contents, 'form': form,'create_project_action':reverse(create_ldt_view)}, context_instance=RequestContext(request)) |
|
164 |
||
165 |
def created_ldt(request): |
|
166 |
return render_to_response('ldt/ldt_utils/done.html', context_instance=RequestContext(request)) |
|
167 |
||
168 |
def indexProject(request, id): |
|
169 |
||
170 |
urlStr = settings.WEB_URL + reverse("space_ldt_init", args=['ldtProject', id]) |
|
171 |
posturl= settings.WEB_URL + reverse("ldt.ldt_utils.views.save_ldtProject") |
|
172 |
language_code = request.LANGUAGE_CODE[:2] |
|
173 |
||
174 |
ldt = get_object_or_404(Project, ldt_id=id) |
|
175 |
if ldt.state ==2: #published |
|
176 |
readonly = 'true' |
|
177 |
else: |
|
178 |
readonly = 'false' |
|
179 |
||
180 |
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)) |
|
181 |
||
182 |
def init(request, method, url): |
|
183 |
ldtgen = LdtUtils() |
|
184 |
||
185 |
doc = ldtgen.generateInit([url], 'ldt.ldt_utils.views.'+method, None) |
|
186 |
||
187 |
resp = HttpResponse(mimetype="text/xml") |
|
188 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
189 |
resp['Pragma']='no-cache' |
|
| 80 | 190 |
doc.write(resp, pretty_print=True) |
| 0 | 191 |
return resp |
192 |
||
193 |
def ldtProject(request, id): |
|
194 |
resp = HttpResponse(mimetype="text/xml") |
|
195 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
196 |
resp['Pragma']='no-cache' |
|
197 |
||
198 |
project = Project.objects.get(ldt_id=id) |
|
199 |
resp.write(project.ldt) |
|
200 |
return resp |
|
201 |
||
202 |
||
203 |
def project_json_id(request, id): |
|
204 |
||
205 |
project = get_object_or_404(Project,ldt_id=id) |
|
206 |
||
207 |
return project_json(request, project) |
|
208 |
||
209 |
||
210 |
def project_json_externalid(request, id): |
|
211 |
||
212 |
res_proj = get_list_or_404(Project.objects.order_by('-modification_date'),contents__external_id = id) |
|
213 |
||
214 |
return project_json(request, res_proj[0]) |
|
215 |
||
216 |
||
217 |
||
218 |
def project_json(request, project): |
|
219 |
||
220 |
mimetype = request.REQUEST.get("mimetype") |
|
221 |
if mimetype is None: |
|
222 |
mimetype = "application/json; charset=utf-8" |
|
223 |
else: |
|
224 |
mimetype = mimetype.encode("utf-8") |
|
225 |
if "charset" not in mimetype: |
|
226 |
mimetype += "; charset=utf-8" |
|
227 |
resp = HttpResponse(mimetype=mimetype) |
|
228 |
resp['Cache-Control']='no-cache, must-revalidate' |
|
229 |
resp['Pragma']='no-cache' |
|
230 |
||
231 |
indent = request.REQUEST.get("indent") |
|
232 |
if indent is None: |
|
233 |
indent = settings.LDT_JSON_DEFAULT_INDENT |
|
234 |
else: |
|
235 |
indent = int(indent) |
|
236 |
||
237 |
callback = request.REQUEST.get("callback") |
|
238 |
escape_str = request.REQUEST.get("escape") |
|
239 |
escape_bool = False |
|
240 |
if escape_str: |
|
241 |
escape_bool = {'true': True, 'false': False, "0": False, "1": True}.get(escape_str.lower()) |
|
242 |
||
243 |
||
244 |
ps = ProjectSerializer(project) |
|
245 |
project_dict = ps.serialize_to_cinelab() |
|
246 |
||
247 |
json_str = simplejson.dumps(project_dict, ensure_ascii=False, indent=indent) |
|
248 |
||
249 |
if callback is not None: |
|
250 |
json_str = "%s(%s)" % (callback,json_str) |
|
251 |
||
252 |
if escape_bool: |
|
253 |
json_str = escape(json_str) |
|
254 |
||
255 |
resp.write(json_str) |
|
256 |
||
257 |
return resp |
|
258 |
||
259 |
||
260 |
def save_ldtProject(request): |
|
261 |
if request.method=="POST": |
|
262 |
ldt = request.POST['ldt'] |
|
263 |
id = request.POST['id'] |
|
264 |
ldtproject=Project.objects.get(ldt_id=id) |
|
| 80 | 265 |
|
266 |
#save xml ldt |
|
| 0 | 267 |
ldtproject.ldt=ldt |
| 80 | 268 |
|
269 |
ldtproj = get_attrib(ldtproject) |
|
270 |
ldtproj.save() |
|
271 |
||
272 |
else: |
|
273 |
ldt = '' |
|
274 |
||
275 |
return render_to_response('ldt/ldt_utils/save_done.html', {'ldt': ldt, 'id':id, 'title':title, 'contents': new_contents}, context_instance=RequestContext(request)) |
|
276 |
||
277 |
||
278 |
def get_attrib(ldtproject): |
|
279 |
doc = lxml.etree.fromstring(ldtproject.ldt.encode( "utf-8" )) |
|
280 |
result = doc.xpath("/iri/project") |
|
281 |
||
282 |
for pnode in result: |
|
283 |
title = pnode.get("title") |
|
| 0 | 284 |
break |
| 80 | 285 |
|
286 |
#set new title |
|
| 0 | 287 |
ldtproject.title=title |
| 80 | 288 |
|
289 |
#get new content list |
|
| 0 | 290 |
new_contents=[] |
| 80 | 291 |
result = doc.xpath("/iri/medias/media") |
| 0 | 292 |
for medianode in result: |
| 80 | 293 |
id = medianode.get("id") |
| 0 | 294 |
new_contents.append(id) |
| 80 | 295 |
|
296 |
#set new content list |
|
| 0 | 297 |
for c in ldtproject.contents.all(): |
298 |
if not c.iri_id in new_contents: |
|
| 80 | 299 |
ldtproject.contents.remove(c) |
300 |
||
301 |
return ldtproject |
|
302 |
||
303 |
||
304 |
||
| 0 | 305 |
@login_required |
| 40 | 306 |
def publish(request, id, redirect=True): |
| 0 | 307 |
ldt = get_object_or_404(Project, ldt_id=id) |
308 |
ldt.state = 2 #published |
|
309 |
ldt.save() |
|
| 40 | 310 |
redirect = boolean_convert(redirect) |
311 |
if redirect: |
|
312 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
313 |
else: |
|
314 |
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 | 315 |
|
316 |
@login_required |
|
| 40 | 317 |
def unpublish(request, id, redirect=True): |
| 0 | 318 |
ldt = get_object_or_404(Project, ldt_id=id) |
319 |
ldt.state = 1 #edition |
|
320 |
ldt.save() |
|
| 40 | 321 |
redirect = boolean_convert(redirect) |
322 |
if redirect: |
|
323 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
324 |
else: |
|
325 |
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 | 326 |
|
327 |
||
328 |
def index(request, url): |
|
329 |
||
330 |
urlStr = settings.WEB_URL + reverse("ldt_init", args=['ldt',url]) |
|
331 |
language_code = request.LANGUAGE_CODE[:2] |
|
332 |
||
333 |
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)) |
|
334 |
||
335 |
||
336 |
def ldt(request, url, startSegment = None): |
|
337 |
||
| 80 | 338 |
##import Ft |
339 |
##from Ft.Xml import MarkupWriter ## ? |
|
| 0 | 340 |
|
341 |
resp = HttpResponse(mimetype="text/xml; charset=utf-8") |
|
342 |
resp['Cache-Control'] = 'no-cache' |
|
343 |
||
344 |
contentList = Content.objects.filter(iri_id=url) |
|
345 |
||
346 |
ldtgen = LdtUtils() |
|
347 |
ldtgen.generateLdt(contentList, file=resp, title = contentList[0].title, startSegment=startSegment) |
|
348 |
||
349 |
return resp |
|
350 |
||
351 |
||
352 |
def loading(request): |
|
353 |
return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request)) |
|
354 |
||
355 |
||
356 |
@login_required |
|
357 |
def create_project(request, iri_id): |
|
358 |
||
359 |
content = get_object_or_404(Content, iri_id=iri_id) |
|
360 |
contents = [ content, ] |
|
361 |
if request.method == "POST" : |
|
362 |
form = AddProjectForm(request.POST) |
|
363 |
if form.is_valid(): |
|
364 |
user=request.user |
|
365 |
project = Project.create_project(title=form.cleaned_data['title'], user=user, contents=contents) |
|
366 |
return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
367 |
else: |
|
368 |
form = AddProjectForm() |
|
369 |
return render_to_response('ldt/ldt_utils/create_ldt.html', {'form':form, 'contents':contents, 'iri_id':iri_id, 'create_project_action':reverse("ldt.ldt_utils.views.create_project",args=[iri_id])}, context_instance=RequestContext(request)) |
|
370 |
||
371 |
@login_required |
|
372 |
def copy_project(request, ldt_id): |
|
373 |
||
374 |
project = get_object_or_404(Project, ldt_id=ldt_id) |
|
375 |
if request.method == "POST" : |
|
376 |
form = CopyProjectForm(request.POST) |
|
377 |
if form.is_valid(): |
|
378 |
user=request.user |
|
379 |
project = project.copy_project(title=request.POST['title'], user=user) |
|
380 |
return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
381 |
else: |
|
382 |
form = CopyProjectForm |
|
383 |
return render_to_response('ldt/ldt_utils/copy_ldt.html', {'form':form, 'project':project}, context_instance=RequestContext(request)) |
|
384 |
||
| 41 | 385 |
|
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
386 |
def write_content_base(request, iri_id=None): |
| 67 | 387 |
|
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
388 |
if iri_id: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
389 |
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
|
390 |
instance_media = instance_content.media_obj |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
391 |
logging.debug("write_content_base : valid form: for instance : media -> " + repr(instance_media) + " content : for instance : " + repr(instance_content) ) |
| 67 | 392 |
else: |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
393 |
logging.debug("No iri_id") |
| 67 | 394 |
instance_content = None |
395 |
instance_media = None |
|
| 42 | 396 |
|
397 |
form_status= 'none' |
|
| 41 | 398 |
if request.method =="POST": |
| 67 | 399 |
content_form = ContentForm(request.POST, prefix="content", instance=instance_content) |
400 |
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
|
401 |
media_valid = media_form.is_valid() |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
402 |
content_valid = content_form.is_valid() |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
403 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
404 |
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
|
405 |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
406 |
if media_valid and content_valid : |
| 62 | 407 |
|
408 |
# see if media must be created |
|
409 |
cleaned_data = {} |
|
410 |
cleaned_data.update(media_form.cleaned_data) |
|
411 |
||
412 |
media_input_type = content_form.cleaned_data["media_input_type"] |
|
413 |
||
414 |
if media_input_type == "none": |
|
415 |
media = None |
|
416 |
elif media_input_type == "link": |
|
417 |
media = content_form.cleaned_data["media_obj"] |
|
418 |
elif media_input_type == "url" or media_input_type == "upload" or media_input_type == "create": |
|
419 |
# copy file |
|
420 |
#complet src |
|
421 |
destination_file = None |
|
422 |
source_file = None |
|
423 |
destination_file_path = None |
|
424 |
if media_input_type != "create": |
|
425 |
try: |
|
426 |
if media_input_type == "url": |
|
427 |
url = cleaned_data["external_src_url"] |
|
428 |
source_file = urllib2.urlopen(url) |
|
429 |
source_filename = source_file.info().get('Content-Disposition', None) |
|
430 |
if not source_filename: |
|
431 |
source_filename = urlparse.urlparse(url).path.rstrip("/").split('/')[-1] |
|
432 |
elif media_input_type == "upload": |
|
433 |
source_file = request.FILES['media-media_file'] |
|
434 |
source_filename = source_file.name |
|
435 |
||
436 |
||
437 |
destination_filepath = os.path.join(settings.STREAM_PATH, source_filename) |
|
438 |
base_source_filename = source_filename |
|
439 |
i = 0 |
|
440 |
while os.path.exists(destination_filepath): |
|
441 |
base_source_filename = source_filename+"(%d)" % (i) |
|
442 |
destination_filepath = os.path.join(settings.STREAM_PATH, base_source_filename) |
|
443 |
i += 1 |
|
444 |
||
445 |
destination_file = open(destination_filepath, "w") |
|
446 |
src_prefix = settings.STREAM_SRC_PREFIX.rstrip("/") |
|
447 |
if len(src_prefix) > 0: |
|
448 |
cleaned_data["src"] = src_prefix + "/" + base_source_filename |
|
449 |
else: |
|
450 |
cleaned_data["src"] = base_source_filename |
|
451 |
||
452 |
chunck = source_file.read(2048) |
|
453 |
while chunck: |
|
454 |
destination_file.write(chunck) |
|
455 |
chunck = source_file.read(2048) |
|
456 |
||
457 |
except Exception as inst: |
|
458 |
logging.debug("write_content_base : POST error when processing file:" + str(inst)) |
|
459 |
form_status = "error" |
|
460 |
finally: |
|
461 |
if destination_file: |
|
462 |
destination_file.close() |
|
463 |
if source_file: |
|
464 |
source_file.close() |
|
465 |
||
466 |
if form_status != "error": |
|
467 |
#try: |
|
468 |
del cleaned_data["media_file"] |
|
469 |
if not cleaned_data['videopath']: |
|
470 |
cleaned_data['videopath'] = settings.STREAM_URL |
|
471 |
media, created = Media.objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data) |
|
472 |
if not created: |
|
473 |
for attribute in ('external_id', 'external_permalink', 'external_publication_url', 'external_src_url', 'media_creation_date', 'videopath', 'duration', 'description', 'title'): |
|
474 |
setattr(media, attribute, cleaned_data.get(attribute)) |
|
475 |
media.save() |
|
476 |
#except Exception as inst: |
|
477 |
# logging.debug("write_content_base : POST error when saving media:" + str(inst)) |
|
478 |
# form_status = "error" |
|
479 |
#TODO: set error message |
|
480 |
#media_form.errors = _("Error when saving the media : " + e.message) |
|
481 |
||
482 |
#if needed preparetemp file and copy temp file to destination |
|
483 |
||
484 |
||
485 |
if form_status != "error": |
|
486 |
#try: |
|
487 |
content_defaults = {} |
|
488 |
content_defaults.update(content_form.cleaned_data) |
|
489 |
content_defaults['media_obj'] = media |
|
490 |
del content_defaults["media_input_type"] |
|
491 |
content, created = Content.objects.get_or_create(iri_id = content_form.cleaned_data['iri_id'], defaults = content_defaults) |
|
492 |
if not created: |
|
493 |
||
494 |
for attribute in ('iriurl', 'title', 'description', 'duration', 'content_creation_date', 'tags', 'media_obj'): |
|
495 |
setattr(content, attribute, content_defaults[attribute]) |
|
496 |
content.save() |
|
497 |
form_status = 'saved' |
|
498 |
media_form = MediaForm(instance=media, prefix="media") |
|
499 |
content_form = ContentForm(instance=content, prefix="content") |
|
500 |
#except: |
|
501 |
#logging.debug("write_content_base : POST error when saving content:" + str(inst)) |
|
502 |
#form_status = "error" |
|
503 |
#TODO : set error on content form |
|
| 42 | 504 |
else: |
505 |
form_status = 'error' |
|
| 41 | 506 |
else: |
| 42 | 507 |
form_status = 'empty' |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
508 |
initial = { 'media_input_type':"link"} |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
509 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
510 |
content_form = ContentForm(prefix="content", instance=instance_content, initial = initial ) |
| 67 | 511 |
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
|
512 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
513 |
if instance_content is not None: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
514 |
content_form.media_input_type = "link" |
| 41 | 515 |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
516 |
return content_form, media_form, form_status |
| 41 | 517 |
|
518 |
||
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
519 |
def write_content(request, iri_id=None): |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
520 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
521 |
logging.debug("write_content : " + str(iri_id) ) |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
522 |
content_form, media_form, form_status = write_content_base(request, iri_id) |
| 41 | 523 |
|
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
524 |
if iri_id: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
525 |
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
|
526 |
else: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
527 |
create_content_action = reverse(write_content) |
| 41 | 528 |
|
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
529 |
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}, context_instance=RequestContext(request)) |
| 41 | 530 |
|
531 |
||
| 80 | 532 |