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