Routeur: Ajout temporaire des modules workflow et dae en écriture
[auf_rh_dae.git] / src / qbe / django_qbe / views.py
CommitLineData
5cf90361
PP
1# -*- coding: utf-8 -*-
2from hashlib import md5
3
4from django.db.models import get_apps
5from django.conf import settings
6from django.contrib.auth.decorators import user_passes_test
7from django.core.urlresolvers import reverse
8from django.http import HttpResponse, HttpResponseRedirect
9from django.shortcuts import render_to_response
10from django.template import RequestContext
11from django.utils import simplejson
12from django.utils.translation import ugettext as _
13
14from django_qbe.forms import QueryByExampleFormSet, DATABASES
15from django_qbe.utils import (autocomplete_graph, qbe_models, formats,
16 pickle_encode, pickle_decode)
17
18try:
19 from django.utils.importlib import import_module
20 # Default value to backwards compatibility
21 qbe_admin_site = getattr(settings, "QBE_ADMIN_SITE", "admin.admin_site")
22 qbe_admin_site_splits = qbe_admin_site.rsplit(".", 1)
23 qbe_admin_module = qbe_admin_site_splits[0]
24 qbe_admin_object = qbe_admin_site_splits[1]
25 admin_site = getattr(import_module(qbe_admin_module), qbe_admin_object)
26except (AttributeError, ImportError):
27 from django.contrib.admin import site as admin_site
28
29qbe_access_for = getattr(settings, "QBE_ACCESS_FOR", lambda u: u.is_staff)
30
31
32@user_passes_test(qbe_access_for)
33def qbe_form(request):
34 query_hash = request.GET.get("hash", "")
35 query_key = "qbe_query_%s" % query_hash
36 db_alias = request.session.get("qbe_database", "default")
37 formset = QueryByExampleFormSet(using=db_alias)
38 json_data = None
39 if query_key in request.session:
40 data = request.session[query_key]
41 db_alias = data.get("database_alias", "default")
42 formset = QueryByExampleFormSet(data=data, using=db_alias)
43 if not formset.is_valid():
44 formset = QueryByExampleFormSet(using=db_alias)
45 else:
46 json_data = simplejson.dumps(data)
47 apps = get_apps()
48 models = getattr(settings, "QBE_CUSTOM_MODELS", None)
49 json_allowed_fields = simplejson.dumps(getattr(settings, "QBE_ALLOWED_FIELDS", None))
50 if models is None:
51 models = qbe_models(admin_site=admin_site, only_admin_models=False)
52 json_models = qbe_models(admin_site=admin_site, json=True)
53 admin_media_prefix = settings.ADMIN_MEDIA_PREFIX
54 display_databases = getattr(settings, 'QBE_DISPLAY_DATABASES', True)
55 return render_to_response('qbe.html',
56 {'apps': apps,
57 'models': models,
58 'formset': formset,
59 'databases': DATABASES,
60 'database_alias': db_alias,
61 'title': _(u"Query by Example"),
62 'json_models': json_models,
63 'json_allowed_fields': json_allowed_fields,
64 'json_data': json_data,
65 'ADMIN_MEDIA_PREFIX': admin_media_prefix,
66 'display_databases': display_databases},
67 context_instance=RequestContext(request))
68
69
70@user_passes_test(qbe_access_for)
71def qbe_proxy(request):
72 if request.POST:
73 data = request.POST.copy()
74 db_alias = request.session.get("qbe_database", "default")
75 formset = QueryByExampleFormSet(data=data, using=db_alias)
76 if formset.is_valid():
77 pickled = pickle_encode(data)
78 query_hash = md5(pickled + settings.SECRET_KEY).hexdigest()
79 query_key = "qbe_query_%s" % query_hash
80 request.session[query_key] = data
81 reverse_url = reverse("qbe_results", args=(query_hash, ))
82 return HttpResponseRedirect(reverse_url)
83 return HttpResponseRedirect(reverse("qbe_form"))
84
85
86@user_passes_test(qbe_access_for)
87def qbe_results(request, query_hash):
88 query_key = "qbe_query_%s" % (query_hash or "")
89 if query_key in request.session:
90 query_key = "qbe_query_%s" % query_hash
91 data = request.session[query_key]
92 else:
93 return HttpResponseRedirect(reverse("qbe_form"))
94 db_alias = data.get("database_alias", "default")
95 if db_alias in DATABASES:
96 request.session["qbe_database"] = db_alias
97 else:
98 db_alias = request.session.get("qbe_database", "default")
99 formset = QueryByExampleFormSet(data=data, using=db_alias)
100 if formset.is_valid():
101 row_number = True
102 admin_name = getattr(settings, "QBE_ADMIN", "admin")
103 labels = formset.get_labels(row_number=row_number)
104 count = formset.get_count()
105 limit = count
106 try:
107 page = int(request.GET.get("p", 0))
108 except ValueError:
109 page = 0
110 if not request.GET.get("show", None):
111 try:
112 limit = int(data.get("limit", 100))
113 except ValueError:
114 limit = 100
115 offset = limit * page
116 results = formset.get_results(limit=limit, offset=offset,
117 admin_name=admin_name,
118 row_number=row_number)
119 query = formset.get_raw_query(add_params=True)
120 pickled = pickle_encode(data)
121 admin_media_prefix = settings.ADMIN_MEDIA_PREFIX
122 return render_to_response('qbe_results.html',
123 {'formset': formset,
124 'title': _(u"Query by Example"),
125 'results': results,
126 'labels': labels,
127 'query': query,
128 'count': count,
129 'limit': limit,
130 'page': page,
131 'offset': offset + 1,
132 'offset_limit': offset + limit,
133 'pickled': pickled,
134 'query_hash': query_hash,
135 'admin_urls': (admin_name != None),
136 'formats': formats,
137 'ADMIN_MEDIA_PREFIX': admin_media_prefix},
138 context_instance=RequestContext(request))
139
140
141@user_passes_test(qbe_access_for)
142def qbe_bookmark(request):
143 data = request.GET.get("data", None)
144 if data:
145 query_hash = md5(data + settings.SECRET_KEY).hexdigest()
146 query_key = "qbe_query_%s" % query_hash
147 request.session[query_key] = pickle_decode(data)
148 reverse_url = reverse("qbe_results", args=(query_hash, ))
149 return HttpResponseRedirect(reverse_url)
150 else:
151 return HttpResponseRedirect(reverse("qbe_form"))
152
153
154@user_passes_test(qbe_access_for)
155def qbe_export(request, format=None):
156 query_hash = request.GET.get("hash", "")
157 query_key = "qbe_query_%s" % query_hash
158 if format and format in formats and query_key in request.session:
159 data = request.session[query_key]
160 db_alias = request.session.get("qbe_database", "default")
161 formset = QueryByExampleFormSet(data=data, using=db_alias)
162 if formset.is_valid():
163 labels = formset.get_labels()
164 query = formset.get_raw_query()
165 results = formset.get_results(query)
166 return formats[format](labels, results)
167 return HttpResponseRedirect(reverse("qbe_form"))
168
169
170# @user_passes_test(qbe_access_for)
171def qbe_js(request):
172 user_passed_test = request.user and qbe_access_for(request.user)
173 return render_to_response('qbe_index.js',
174 {'qbe_url': reverse("qbe_form"),
175 'reports_label': _(u"Reports"),
176 'qbe_label': _(u"Query by Example"),
177 'user_passes_test': user_passed_test},
178 context_instance=RequestContext(request))
179
180
181@user_passes_test(qbe_access_for)
182def qbe_autocomplete(request):
183 nodes = None
184 if request.is_ajax() and request.POST:
185 models = request.POST.get('models', []).split(",")
186 nodes = autocomplete_graph(admin_site, models)
187 json_nodes = simplejson.dumps(nodes)
188 return HttpResponse(json_nodes, mimetype="application/json")