Merge remote-tracking branch 'ithinksw/develop' into develop
[philo.git] / philo / contrib / sobol / admin.py
1 from functools import update_wrapper
2
3 from django.conf import settings
4 from django.conf.urls.defaults import patterns, url
5 from django.contrib import admin
6 from django.core.urlresolvers import reverse
7 from django.db.models import Count
8 from django.http import HttpResponseRedirect, Http404
9 from django.shortcuts import render_to_response
10 from django.template import RequestContext
11 from django.utils.translation import ugettext_lazy as _
12
13 from philo.admin import EntityAdmin
14 from philo.contrib.sobol.models import Search, ResultURL, SearchView
15
16
17 class ResultURLInline(admin.TabularInline):
18         model = ResultURL
19         readonly_fields = ('url',)
20         can_delete = False
21         extra = 0
22         max_num = 0
23
24
25 class SearchAdmin(admin.ModelAdmin):
26         readonly_fields = ('string',)
27         inlines = [ResultURLInline]
28         list_display = ['string', 'unique_urls', 'total_clicks']
29         search_fields = ['string', 'result_urls__url']
30         actions = ['results_action']
31         if 'grappelli' in settings.INSTALLED_APPS:
32                 change_form_template = 'admin/sobol/search/grappelli_change_form.html'
33         
34         def unique_urls(self, obj):
35                 return obj.unique_urls
36         unique_urls.admin_order_field = 'unique_urls'
37         
38         def total_clicks(self, obj):
39                 return obj.total_clicks
40         total_clicks.admin_order_field = 'total_clicks'
41         
42         def queryset(self, request):
43                 qs = super(SearchAdmin, self).queryset(request)
44                 return qs.annotate(total_clicks=Count('result_urls__clicks', distinct=True), unique_urls=Count('result_urls', distinct=True))
45
46
47 class SearchViewAdmin(EntityAdmin):
48         raw_id_fields = ('results_page',)
49         related_lookup_fields = {'fk': raw_id_fields}
50
51
52 admin.site.register(Search, SearchAdmin)
53 admin.site.register(SearchView, SearchViewAdmin)