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
15 class ResultURLInline(admin.TabularInline):
17 readonly_fields = ('url',)
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'
32 results_template = 'admin/sobol/search/results.html'
35 urlpatterns = super(SearchAdmin, self).get_urls()
38 def wrapper(*args, **kwargs):
39 return self.admin_site.admin_view(view)(*args, **kwargs)
40 return update_wrapper(wrapper, view)
42 info = self.model._meta.app_label, self.model._meta.module_name
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)
50 def unique_urls(self, obj):
51 return obj.unique_urls
52 unique_urls.admin_order_field = 'unique_urls'
54 def total_clicks(self, obj):
55 return obj.total_clicks
56 total_clicks.admin_order_field = 'total_clicks'
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))
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,)))
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"
71 def results_view(self, request, object_id=None, extra_context=None):
72 if object_id is not None:
73 object_ids = [object_id]
75 object_ids = request.GET.get('ids').split(',')
77 if object_ids is None:
80 qs = self.queryset(request).filter(pk__in=object_ids)
81 opts = self.model._meta
83 if len(object_ids) == 1:
84 title = _(u"Search results for %s" % qs[0])
86 title = _(u"Search results for multiple objects")
92 'root_path': self.admin_site.root_path,
93 'app_label': opts.app_label
95 return render_to_response(self.results_template, context, context_instance=RequestContext(request))
98 class SearchViewAdmin(EntityAdmin):
99 raw_id_fields = ('results_page',)
100 related_lookup_fields = {'fk': raw_id_fields}
103 admin.site.register(Search, SearchAdmin)
104 admin.site.register(SearchView, SearchViewAdmin)