Fix for cmp items
[auf_rh_dae.git] / src / qbe / django_qbe / admin.py
CommitLineData
5cf90361
PP
1from hashlib import md5
2from django.conf import settings
3from django.http import HttpResponseRedirect
4from django.core.urlresolvers import reverse
5from django.utils.translation import ugettext_lazy as _
6from django.contrib import admin
7from django.contrib.admin.util import unquote
8from django_qbe.utils import pickle_encode
9from django_qbe.models import SavedQuery
10
11class 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", "")
ab83a5da
PP
47 if query_hash:
48 query_key = "qbe_query_%s" % query_hash
49 obj.query_data = request.session[query_key]
5cf90361
PP
50
51 if not change:
52 obj.user_created = request.user
53
54 obj.user_updated = request.user
55 obj.save()
56
57 def add_view(self, request, form_url='', extra_context=None):
58 query_hash = request.GET.get("query_hash", "")
59 query_key = "qbe_query_%s" % query_hash
60 if not query_key in request.session:
61 return HttpResponseRedirect(reverse("qbe_form"))
62 return super(SavedQueryAdmin, self).add_view(request, form_url=form_url, extra_context=extra_context)
63
64 def run_view(self, request, object_id, extra_context=None):
65 obj = self.get_object(request, unquote(object_id))
66 data = obj.query_data
67 pickled = pickle_encode(data)
68 query_hash = md5(pickled + settings.SECRET_KEY).hexdigest()
69 query_key = "qbe_query_%s" % query_hash
70 if not query_key in request.session:
71 request.session[query_key] = data
72 return HttpResponseRedirect(reverse("qbe_results", args=(query_hash, )))
73
74admin.site.register(SavedQuery, SavedQueryAdmin)