Installation du nouveau QBE ici
[auf_rh_dae.git] / src / qbe / django_qbe / admin.py
1 from hashlib import md5
2 from django.conf import settings
3 from django.http import HttpResponseRedirect
4 from django.core.urlresolvers import reverse
5 from django.utils.translation import ugettext_lazy as _
6 from django.contrib import admin
7 from django.contrib.admin.util import unquote
8 from django_qbe.utils import pickle_encode
9 from django_qbe.models import SavedQuery
10
11 class SavedQueryAdmin(admin.ModelAdmin):
12 list_display = ('name', 'description', 'date_created', 'run_link')
13 exclude = ('user_created', 'user_updated')
14
15 def run_link(self, obj):
16 info = self.model._meta.app_label, self.model._meta.module_name
17 pickled = pickle_encode(obj.query_data)
18 query_hash = md5(pickled + settings.SECRET_KEY).hexdigest()
19 return u'<span class="nowrap"><a href="%s">%s</a> | <a href="%s?hash=%s">%s</a></span>' % \
20 (reverse("admin:%s_%s_run" % info, args=(obj.pk,)), _("Run"),
21 reverse("qbe_form"), query_hash, _("Edit & Run"))
22 run_link.short_description = _("query")
23 run_link.allow_tags = True
24
25 def get_urls(self):
26 from django.conf.urls.defaults import patterns, url
27 from django.utils.functional import update_wrapper
28
29 def wrap(view):
30 def wrapper(*args, **kwargs):
31 return self.admin_site.admin_view(view)(*args, **kwargs)
32 return update_wrapper(wrapper, view)
33
34 info = self.model._meta.app_label, self.model._meta.module_name
35 urlpatterns = patterns('',
36 url(r'^(.+)/run/$',
37 wrap(self.run_view),
38 name='%s_%s_run' % info,
39 kwargs={'extra_context': {
40 'test': 'test',
41 }}),
42 )
43 return urlpatterns + super(SavedQueryAdmin, self).get_urls()
44
45 def save_model(self, request, obj, form, change):
46 query_hash = request.GET.get("query_hash", "")
47 query_key = "qbe_query_%s" % query_hash
48 obj.query_data = request.session[query_key]
49
50 if not change:
51 obj.user_created = request.user
52
53 obj.user_updated = request.user
54 obj.save()
55
56 def add_view(self, request, form_url='', extra_context=None):
57 query_hash = request.GET.get("query_hash", "")
58 query_key = "qbe_query_%s" % query_hash
59 if not query_key in request.session:
60 return HttpResponseRedirect(reverse("qbe_form"))
61 return super(SavedQueryAdmin, self).add_view(request, form_url=form_url, extra_context=extra_context)
62
63 def run_view(self, request, object_id, extra_context=None):
64 obj = self.get_object(request, unquote(object_id))
65 data = obj.query_data
66 pickled = pickle_encode(data)
67 query_hash = md5(pickled + settings.SECRET_KEY).hexdigest()
68 query_key = "qbe_query_%s" % query_hash
69 if not query_key in request.session:
70 request.session[query_key] = data
71 return HttpResponseRedirect(reverse("qbe_results", args=(query_hash, )))
72
73 admin.site.register(SavedQuery, SavedQueryAdmin)