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