| author | ymh <ymh.work@gmail.com> |
| Thu, 23 Sep 2010 13:45:21 +0200 | |
| changeset 71 | fc1210bbb854 |
| parent 67 | 90fd14c649bb |
| child 80 | 1e7732f40eee |
| child 81 | 97b12f5f2c7a |
| 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 |
from Ft.Xml import MarkupWriter |
|
20 |
import cgi |
|
21 |
import uuid |
|
22 |
import base64 |
|
23 |
import lucene |
|
24 |
import xml.dom |
|
25 |
import xml.dom.ext |
|
26 |
import xml.dom.minidom |
|
| 62 | 27 |
import tempfile |
28 |
import urllib2 |
|
29 |
from urllib2 import urlparse |
|
30 |
from jogging import logging |
|
| 0 | 31 |
|
32 |
||
33 |
||
34 |
def searchForm(request): |
|
35 |
form = SearchForm() |
|
36 |
return render_to_response('ldt/ldt_utils_utils/search_form.html',{'form': form} , context_instance=RequestContext(request)) |
|
37 |
||
38 |
def searchIndex(request): |
|
39 |
||
40 |
sform = SearchForm(request.POST) |
|
41 |
if sform.is_valid(): |
|
42 |
search = sform.cleaned_data["search"] |
|
43 |
||
44 |
||
45 |
queryStr = base64.urlsafe_b64encode(search.encode('utf8')) |
|
46 |
field = request.POST["field"] |
|
47 |
language_code = request.LANGUAGE_CODE[:2] |
|
48 |
||
49 |
url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, queryStr]) |
|
50 |
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)) |
|
51 |
else: |
|
52 |
resp = HttpResponse() |
|
53 |
resp.write("<html><head></head><body>Error : No result</body></html>"); |
|
54 |
||
55 |
def searchIndexGet(request, field, query): |
|
56 |
||
57 |
language_code = request.LANGUAGE_CODE[:2] |
|
58 |
url = settings.WEB_URL + django.core.urlresolvers.reverse("ldt.ldt_utils.views.searchInit", args=[field, query]) |
|
59 |
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)) |
|
60 |
||
61 |
def searchInit(request, field, query): |
|
62 |
||
63 |
ldtgen = LdtUtils() |
|
64 |
||
65 |
doc = ldtgen.generateInit([field,query], 'ldt.ldt_utils.views.searchLdt', 'ldt.ldt_utils.views.searchSegments') |
|
66 |
||
67 |
resp = HttpResponse(mimetype="text/xml;charset=utf-8") |
|
68 |
xml.dom.ext.PrettyPrint(doc, resp) |
|
69 |
return resp |
|
70 |
||
71 |
def searchLdt(request, field, query, edition=None): |
|
72 |
||
73 |
contentList = [] |
|
74 |
resp = HttpResponse(mimetype="text/xml") |
|
75 |
queryStr = "" |
|
76 |
||
77 |
if query and len(query)>0: |
|
78 |
queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8") |
|
79 |
searcher = LdtSearch() |
|
80 |
ids = {} |
|
81 |
||
82 |
for result in searcher.query(field, queryStr): |
|
83 |
ids[result["iri_id"]] = "" |
|
84 |
||
85 |
id_list = ids.keys() |
|
86 |
||
87 |
if edition is not None: |
|
88 |
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"))) |
|
89 |
id_list = filter(lambda id: id in id_list, ids_editions) |
|
90 |
||
91 |
contentList = Content.objects.filter(iri_id__in=id_list) |
|
92 |
||
93 |
||
94 |
ldtgen = LdtUtils() |
|
95 |
ldtgen.generateLdt(contentList, file=resp, title = u"Recherche : " + queryStr) |
|
96 |
||
97 |
return resp |
|
98 |
||
99 |
||
100 |
def searchSegments(request, field, query, edition=None): |
|
101 |
||
102 |
if query and len(query)>0: |
|
103 |
searcher = LdtSearch() |
|
104 |
||
105 |
queryStr = base64.urlsafe_b64decode(query.encode("ascii")).decode("utf8") |
|
106 |
res = searcher.query(field, queryStr) |
|
107 |
else: |
|
108 |
res = [] |
|
109 |
||
110 |
iri_ids = None |
|
111 |
||
112 |
if edition is not None: |
|
113 |
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"))) |
|
114 |
||
115 |
doc = xml.dom.getDOMImplementation().createDocument(None, "iri", None) |
|
116 |
||
117 |
for resultMap in res: |
|
118 |
if iri_ids is None or resultMap['iri_id'] in iri_ids: |
|
119 |
elem = doc.createElement('seg') |
|
120 |
elem.setAttribute('idctt', resultMap['iri_id']) |
|
121 |
elem.setAttribute('idens', resultMap['ensemble_id']) |
|
122 |
elem.setAttribute('iddec', resultMap['decoupage_id']) |
|
123 |
elem.setAttribute('idseg', resultMap['element_id']) |
|
124 |
elem.setAttribute('idvue', "") |
|
125 |
elem.setAttribute('crit', "") |
|
126 |
doc.documentElement.appendChild(elem) |
|
127 |
||
128 |
return HttpResponse(doc.toprettyxml(encoding='utf-8'), mimetype="text/xml;charset=utf-8") |
|
129 |
||
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' |
|
190 |
xml.dom.ext.PrettyPrint(doc, resp) |
|
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) |
|
265 |
#save xml ldt |
|
266 |
ldtproject.ldt=ldt |
|
267 |
#get new title |
|
268 |
dom = xml.dom.minidom.parseString(ldt.encode( "utf-8" )) |
|
269 |
con = xml.xpath.Context.Context(dom, 1, 1, None) |
|
270 |
result = xml.xpath.Evaluate("/iri/project",context=con) |
|
271 |
for pnode in result: |
|
272 |
title=pnode.getAttribute("title") |
|
273 |
break |
|
274 |
#set new title |
|
275 |
ldtproject.title=title |
|
276 |
#get new content list |
|
277 |
new_contents=[] |
|
278 |
result = xml.xpath.Evaluate("/iri/medias/media", context=con) |
|
279 |
for medianode in result: |
|
280 |
id = medianode.attributes['id'].value |
|
281 |
new_contents.append(id) |
|
282 |
# set new content list |
|
283 |
for c in ldtproject.contents.all(): |
|
284 |
if not c.iri_id in new_contents: |
|
285 |
ldtproject.contents.remove(c) |
|
286 |
ldtproject.save() |
|
287 |
else: |
|
288 |
ldt = '' |
|
289 |
return render_to_response('ldt/ldt_utils/save_done.html', {'ldt': ldt, 'id':id, 'title':title, 'contents': new_contents}, context_instance=RequestContext(request)) |
|
290 |
||
291 |
@login_required |
|
| 40 | 292 |
def publish(request, id, redirect=True): |
| 0 | 293 |
ldt = get_object_or_404(Project, ldt_id=id) |
294 |
ldt.state = 2 #published |
|
295 |
ldt.save() |
|
| 40 | 296 |
redirect = boolean_convert(redirect) |
297 |
if redirect: |
|
298 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
299 |
else: |
|
300 |
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 | 301 |
|
302 |
@login_required |
|
| 40 | 303 |
def unpublish(request, id, redirect=True): |
| 0 | 304 |
ldt = get_object_or_404(Project, ldt_id=id) |
305 |
ldt.state = 1 #edition |
|
306 |
ldt.save() |
|
| 40 | 307 |
redirect = boolean_convert(redirect) |
308 |
if redirect: |
|
309 |
return HttpResponseRedirect(reverse("ldt.ldt_utils.views.list_ldt")) |
|
310 |
else: |
|
311 |
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 | 312 |
|
313 |
||
314 |
def index(request, url): |
|
315 |
||
316 |
urlStr = settings.WEB_URL + reverse("ldt_init", args=['ldt',url]) |
|
317 |
language_code = request.LANGUAGE_CODE[:2] |
|
318 |
||
319 |
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)) |
|
320 |
||
321 |
||
322 |
def ldt(request, url, startSegment = None): |
|
323 |
||
324 |
import Ft |
|
325 |
from Ft.Xml import MarkupWriter |
|
326 |
||
327 |
resp = HttpResponse(mimetype="text/xml; charset=utf-8") |
|
328 |
resp['Cache-Control'] = 'no-cache' |
|
329 |
||
330 |
contentList = Content.objects.filter(iri_id=url) |
|
331 |
||
332 |
ldtgen = LdtUtils() |
|
333 |
ldtgen.generateLdt(contentList, file=resp, title = contentList[0].title, startSegment=startSegment) |
|
334 |
||
335 |
return resp |
|
336 |
||
337 |
||
338 |
def loading(request): |
|
339 |
return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request)) |
|
340 |
||
341 |
||
342 |
@login_required |
|
343 |
def create_project(request, iri_id): |
|
344 |
||
345 |
content = get_object_or_404(Content, iri_id=iri_id) |
|
346 |
contents = [ content, ] |
|
347 |
if request.method == "POST" : |
|
348 |
form = AddProjectForm(request.POST) |
|
349 |
if form.is_valid(): |
|
350 |
user=request.user |
|
351 |
project = Project.create_project(title=form.cleaned_data['title'], user=user, contents=contents) |
|
352 |
return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
353 |
else: |
|
354 |
form = AddProjectForm() |
|
355 |
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)) |
|
356 |
||
357 |
@login_required |
|
358 |
def copy_project(request, ldt_id): |
|
359 |
||
360 |
project = get_object_or_404(Project, ldt_id=ldt_id) |
|
361 |
if request.method == "POST" : |
|
362 |
form = CopyProjectForm(request.POST) |
|
363 |
if form.is_valid(): |
|
364 |
user=request.user |
|
365 |
project = project.copy_project(title=request.POST['title'], user=user) |
|
366 |
return HttpResponseRedirect(reverse('ldt.ldt_utils.views.indexProject', args=[project.ldt_id])) |
|
367 |
else: |
|
368 |
form = CopyProjectForm |
|
369 |
return render_to_response('ldt/ldt_utils/copy_ldt.html', {'form':form, 'project':project}, context_instance=RequestContext(request)) |
|
370 |
||
| 41 | 371 |
|
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
372 |
def write_content_base(request, iri_id=None): |
| 67 | 373 |
|
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
374 |
if iri_id: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
375 |
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
|
376 |
instance_media = instance_content.media_obj |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
377 |
logging.debug("write_content_base : valid form: for instance : media -> " + repr(instance_media) + " content : for instance : " + repr(instance_content) ) |
| 67 | 378 |
else: |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
379 |
logging.debug("No iri_id") |
| 67 | 380 |
instance_content = None |
381 |
instance_media = None |
|
| 42 | 382 |
|
383 |
form_status= 'none' |
|
| 41 | 384 |
if request.method =="POST": |
| 67 | 385 |
content_form = ContentForm(request.POST, prefix="content", instance=instance_content) |
386 |
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
|
387 |
media_valid = media_form.is_valid() |
|
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
388 |
content_valid = content_form.is_valid() |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
389 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
390 |
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
|
391 |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
392 |
if media_valid and content_valid : |
| 62 | 393 |
|
394 |
# see if media must be created |
|
395 |
cleaned_data = {} |
|
396 |
cleaned_data.update(media_form.cleaned_data) |
|
397 |
||
398 |
media_input_type = content_form.cleaned_data["media_input_type"] |
|
399 |
||
400 |
if media_input_type == "none": |
|
401 |
media = None |
|
402 |
elif media_input_type == "link": |
|
403 |
media = content_form.cleaned_data["media_obj"] |
|
404 |
elif media_input_type == "url" or media_input_type == "upload" or media_input_type == "create": |
|
405 |
# copy file |
|
406 |
#complet src |
|
407 |
destination_file = None |
|
408 |
source_file = None |
|
409 |
destination_file_path = None |
|
410 |
if media_input_type != "create": |
|
411 |
try: |
|
412 |
if media_input_type == "url": |
|
413 |
url = cleaned_data["external_src_url"] |
|
414 |
source_file = urllib2.urlopen(url) |
|
415 |
source_filename = source_file.info().get('Content-Disposition', None) |
|
416 |
if not source_filename: |
|
417 |
source_filename = urlparse.urlparse(url).path.rstrip("/").split('/')[-1] |
|
418 |
elif media_input_type == "upload": |
|
419 |
source_file = request.FILES['media-media_file'] |
|
420 |
source_filename = source_file.name |
|
421 |
||
422 |
||
423 |
destination_filepath = os.path.join(settings.STREAM_PATH, source_filename) |
|
424 |
base_source_filename = source_filename |
|
425 |
i = 0 |
|
426 |
while os.path.exists(destination_filepath): |
|
427 |
base_source_filename = source_filename+"(%d)" % (i) |
|
428 |
destination_filepath = os.path.join(settings.STREAM_PATH, base_source_filename) |
|
429 |
i += 1 |
|
430 |
||
431 |
destination_file = open(destination_filepath, "w") |
|
432 |
src_prefix = settings.STREAM_SRC_PREFIX.rstrip("/") |
|
433 |
if len(src_prefix) > 0: |
|
434 |
cleaned_data["src"] = src_prefix + "/" + base_source_filename |
|
435 |
else: |
|
436 |
cleaned_data["src"] = base_source_filename |
|
437 |
||
438 |
chunck = source_file.read(2048) |
|
439 |
while chunck: |
|
440 |
destination_file.write(chunck) |
|
441 |
chunck = source_file.read(2048) |
|
442 |
||
443 |
except Exception as inst: |
|
444 |
logging.debug("write_content_base : POST error when processing file:" + str(inst)) |
|
445 |
form_status = "error" |
|
446 |
finally: |
|
447 |
if destination_file: |
|
448 |
destination_file.close() |
|
449 |
if source_file: |
|
450 |
source_file.close() |
|
451 |
||
452 |
if form_status != "error": |
|
453 |
#try: |
|
454 |
del cleaned_data["media_file"] |
|
455 |
if not cleaned_data['videopath']: |
|
456 |
cleaned_data['videopath'] = settings.STREAM_URL |
|
457 |
media, created = Media.objects.get_or_create(src=cleaned_data['src'], defaults=cleaned_data) |
|
458 |
if not created: |
|
459 |
for attribute in ('external_id', 'external_permalink', 'external_publication_url', 'external_src_url', 'media_creation_date', 'videopath', 'duration', 'description', 'title'): |
|
460 |
setattr(media, attribute, cleaned_data.get(attribute)) |
|
461 |
media.save() |
|
462 |
#except Exception as inst: |
|
463 |
# logging.debug("write_content_base : POST error when saving media:" + str(inst)) |
|
464 |
# form_status = "error" |
|
465 |
#TODO: set error message |
|
466 |
#media_form.errors = _("Error when saving the media : " + e.message) |
|
467 |
||
468 |
#if needed preparetemp file and copy temp file to destination |
|
469 |
||
470 |
||
471 |
if form_status != "error": |
|
472 |
#try: |
|
473 |
content_defaults = {} |
|
474 |
content_defaults.update(content_form.cleaned_data) |
|
475 |
content_defaults['media_obj'] = media |
|
476 |
del content_defaults["media_input_type"] |
|
477 |
content, created = Content.objects.get_or_create(iri_id = content_form.cleaned_data['iri_id'], defaults = content_defaults) |
|
478 |
if not created: |
|
479 |
||
480 |
for attribute in ('iriurl', 'title', 'description', 'duration', 'content_creation_date', 'tags', 'media_obj'): |
|
481 |
setattr(content, attribute, content_defaults[attribute]) |
|
482 |
content.save() |
|
483 |
form_status = 'saved' |
|
484 |
media_form = MediaForm(instance=media, prefix="media") |
|
485 |
content_form = ContentForm(instance=content, prefix="content") |
|
486 |
#except: |
|
487 |
#logging.debug("write_content_base : POST error when saving content:" + str(inst)) |
|
488 |
#form_status = "error" |
|
489 |
#TODO : set error on content form |
|
| 42 | 490 |
else: |
491 |
form_status = 'error' |
|
| 41 | 492 |
else: |
| 42 | 493 |
form_status = 'empty' |
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
494 |
initial = { 'media_input_type':"link"} |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
495 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
496 |
content_form = ContentForm(prefix="content", instance=instance_content, initial = initial ) |
| 67 | 497 |
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
|
498 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
499 |
if instance_content is not None: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
500 |
content_form.media_input_type = "link" |
| 41 | 501 |
|
|
60
a8ad7ebf5902
various update and splitmedia from content
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
502 |
return content_form, media_form, form_status |
| 41 | 503 |
|
504 |
||
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
505 |
def write_content(request, iri_id=None): |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
506 |
|
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
507 |
logging.debug("write_content : " + str(iri_id) ) |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
508 |
content_form, media_form, form_status = write_content_base(request, iri_id) |
| 41 | 509 |
|
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
510 |
if iri_id: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
511 |
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
|
512 |
else: |
|
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
513 |
create_content_action = reverse(write_content) |
| 41 | 514 |
|
|
71
fc1210bbb854
correct translation +some bug on update
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
515 |
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 | 516 |
|
517 |
||
518 |