Merge branch 'sobol-templates-hotfix'
[philo.git] / contrib / sobol / admin.py
1 from django.conf import settings
2 from django.conf.urls.defaults import patterns, url
3 from django.contrib import admin
4 from django.core.urlresolvers import reverse
5 from django.db.models import Count
6 from django.http import HttpResponseRedirect, Http404
7 from django.shortcuts import render_to_response
8 from django.template import RequestContext
9 from django.utils.translation import ugettext_lazy as _
10 from philo.admin import EntityAdmin
11 from philo.contrib.sobol.models import Search, ResultURL, SearchView
12 from functools import update_wrapper
13
14
15 class ResultURLInline(admin.TabularInline):
16         model = ResultURL
17         readonly_fields = ('url',)
18         can_delete = False
19         extra = 0
20         max_num = 0
21
22
23 class SearchAdmin(admin.ModelAdmin):
24         readonly_fields = ('string',)
25         inlines = [ResultURLInline]
26         list_display = ['string', 'unique_urls', 'total_clicks']
27         search_fields = ['string', 'result_urls__url']
28         actions = ['results_action']
29         if 'grappelli' in settings.INSTALLED_APPS:
30                 results_template = 'admin/sobol/search/grappelli_results.html'
31         else:
32                 results_template = 'admin/sobol/search/results.html'
33         
34         def get_urls(self):
35                 urlpatterns = super(SearchAdmin, self).get_urls()
36                 
37                 def wrap(view):
38                         def wrapper(*args, **kwargs):
39                                 return self.admin_site.admin_view(view)(*args, **kwargs)
40                         return update_wrapper(wrapper, view)
41                 
42                 info = self.model._meta.app_label, self.model._meta.module_name
43                 
44                 urlpatterns = patterns('',
45                         url(r'^results/$', wrap(self.results_view), name="%s_%s_selected_results" % info),
46                         url(r'^(.+)/results/$', wrap(self.results_view), name="%s_%s_results" % info)
47                 ) + urlpatterns
48                 return urlpatterns
49         
50         def unique_urls(self, obj):
51                 return obj.unique_urls
52         unique_urls.admin_order_field = 'unique_urls'
53         
54         def total_clicks(self, obj):
55                 return obj.total_clicks
56         total_clicks.admin_order_field = 'total_clicks'
57         
58         def queryset(self, request):
59                 qs = super(SearchAdmin, self).queryset(request)
60                 return qs.annotate(total_clicks=Count('result_urls__clicks', distinct=True), unique_urls=Count('result_urls', distinct=True))
61         
62         def results_action(self, request, queryset):
63                 info = self.model._meta.app_label, self.model._meta.module_name
64                 if len(queryset) == 1:
65                         return HttpResponseRedirect(reverse("admin:%s_%s_results" % info, args=(queryset[0].pk,)))
66                 else:
67                         url = reverse("admin:%s_%s_selected_results" % info)
68                         return HttpResponseRedirect("%s?ids=%s" % (url, ','.join([str(item.pk) for item in queryset])))
69         results_action.short_description = "View results for selected %(verbose_name_plural)s"
70         
71         def results_view(self, request, object_id=None, extra_context=None):
72                 if object_id is not None:
73                         object_ids = [object_id]
74                 else:
75                         object_ids = request.GET.get('ids').split(',')
76                         
77                         if object_ids is None:
78                                 raise Http404
79                 
80                 qs = self.queryset(request).filter(pk__in=object_ids)
81                 opts = self.model._meta
82                 
83                 if len(object_ids) == 1:
84                         title = _(u"Search results for %s" % qs[0])
85                 else:
86                         title = _(u"Search results for multiple objects")
87                 
88                 context = {
89                         'title': title,
90                         'queryset': qs,
91                         'opts': opts,
92                         'root_path': self.admin_site.root_path,
93                         'app_label': opts.app_label
94                 }
95                 return render_to_response(self.results_template, context, context_instance=RequestContext(request))
96
97
98 class SearchViewAdmin(EntityAdmin):
99         raw_id_fields = ('results_page',)
100         related_lookup_fields = {'fk': raw_id_fields}
101
102
103 admin.site.register(Search, SearchAdmin)
104 admin.site.register(SearchView, SearchViewAdmin)