|
1 from django.conf import settings |
|
2 from django.contrib.auth.decorators import login_required |
|
3 from django.contrib.auth.models import User, Group |
|
4 from django.core.cache import cache |
|
5 from django.core.urlresolvers import reverse |
|
6 from django.core.paginator import Paginator, InvalidPage, EmptyPage |
|
7 from django.db.models import Q |
|
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 ldt.indexation import get_results_with_context, highlight_documents |
|
13 from django.shortcuts import (render_to_response, get_object_or_404, |
|
14 get_list_or_404) |
|
15 from django.template import RequestContext |
|
16 from django.template.loader import render_to_string |
|
17 from django.utils import simplejson |
|
18 from django.utils.html import escape |
|
19 from django.utils.translation import ugettext as _, ungettext |
|
20 from ldt.ldt_utils.forms import (LdtAddForm, SearchForm, AddProjectForm, CopyProjectForm, |
|
21 ContentForm, MediaForm, GroupAddForm) |
|
22 from guardian.shortcuts import remove_perm, get_objects_for_group, get_objects_for_user |
|
23 from ldt.ldt_utils.models import Content, Media, Project |
|
24 from ldt.ldt_utils.utils import boolean_convert, LdtUtils, LdtSearch |
|
25 from ldt.security.utils import (assign_perm_to_obj, set_forbidden_stream, |
|
26 add_change_attr, get_userlist, get_userlist_model, get_userlist_group) |
|
27 from ldt.security.cache import get_cached_checker, cached_assign |
|
28 from lxml.html import fragment_fromstring |
|
29 #from models import Media, Project |
|
30 from ldt.ldt_utils.projectserializer import ProjectSerializer |
|
31 from urllib2 import urlparse |
|
32 from operator import itemgetter |
|
33 from itertools import groupby |
|
34 import base64 |
|
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 lxml.etree |
|
40 import mimetypes |
|
41 import os |
|
42 import urllib2 |
|
43 import subprocess |
|
44 import re |
|
45 import datetime |
|
46 |
|
47 |
|
48 def project_json_id(request, id): |
|
49 |
|
50 project = get_object_or_404(Project.safe_objects, ldt_id=id) |
|
51 |
|
52 return project_json(request, project, False) |
|
53 |
|
54 |
|
55 def project_json_externalid(request, id): |
|
56 |
|
57 res_proj = get_list_or_404(Project.safe_objects.order_by('-modification_date'), contents__external_id=id) #@UndefinedVariable |
|
58 |
|
59 return project_json(request, res_proj[0], False) |
|
60 |
|
61 |
|
62 |
|
63 def project_json(request, project, serialize_contents=True): # Not checked |
|
64 |
|
65 if not ldt_auth.check_access(request.user, project): |
|
66 return HttpResponseForbidden(_("You can not access this project")) |
|
67 |
|
68 mimetype = request.REQUEST.get("mimetype") |
|
69 if mimetype is None: |
|
70 mimetype = "application/json; charset=utf-8" |
|
71 else: |
|
72 mimetype = mimetype.encode("utf-8") |
|
73 if "charset" not in mimetype: |
|
74 mimetype += "; charset=utf-8" |
|
75 resp = HttpResponse(mimetype=mimetype) |
|
76 resp['Cache-Control'] = 'no-cache, must-revalidate' |
|
77 resp['Pragma'] = 'no-cache' |
|
78 |
|
79 indent = request.REQUEST.get("indent") |
|
80 if indent is None: |
|
81 indent = settings.LDT_JSON_DEFAULT_INDENT |
|
82 else: |
|
83 indent = int(indent) |
|
84 |
|
85 callback = request.REQUEST.get("callback") |
|
86 escape_str = request.REQUEST.get("escape") |
|
87 escape_bool = False |
|
88 if escape_str: |
|
89 escape_bool = {'true': True, 'false': False, "0": False, "1": True}.get(escape_str.lower()) |
|
90 |
|
91 |
|
92 ps = ProjectSerializer(project, serialize_contents) |
|
93 project_dict = ps.serialize_to_cinelab() |
|
94 |
|
95 json_str = simplejson.dumps(project_dict, ensure_ascii=False, indent=indent) |
|
96 |
|
97 if callback is not None: |
|
98 json_str = "%s(%s)" % (callback, json_str) |
|
99 |
|
100 if escape_bool: |
|
101 json_str = escape(json_str) |
|
102 |
|
103 resp.write(json_str) |
|
104 |
|
105 return resp |