comparison contests/admin.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/contests/admin.py@51fa1e0ca218
children 5977b43499f7
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Admin definitions for the contest application.
3
4 """
5 from django.contrib import admin
6 from django.conf import settings
7
8 from contests.models import Contest
9
10
11 class ContestAdmin(admin.ModelAdmin):
12 list_display = ['title', 'is_public', 'creation_date', 'end_date',
13 'contestant_count', 'winner']
14 list_editable = ['is_public']
15 date_hierarchy = 'creation_date'
16 search_fields = ['title', 'description']
17 prepopulated_fields = {'slug': ['title']}
18 raw_id_fields = ['winner', 'contestants']
19 actions = ['pick_winner']
20
21 class Media:
22 js = (['js/contests/contests_admin.js'] +
23 settings.GPP_THIRD_PARTY_JS['tiny_mce'])
24
25 def contestant_count(self, obj):
26 return obj.contestants.count()
27 contestant_count.short_description = '# Entries'
28
29 def pick_winner(self, request, qs):
30 """
31 Picks a winner on the contests selected by the admin. Note that for
32 safety reasons, we only update those contests that don't have winners
33 already.
34
35 """
36 count = 0
37 for contest in qs:
38 if not contest.winner:
39 contest.pick_winner()
40 contest.save()
41 count += 1
42
43 self.message_user(request, "%d of %d winners picked" % (count,
44 qs.count()))
45
46 pick_winner.short_description = "Pick winners for selected contests"
47
48
49
50 admin.site.register(Contest, ContestAdmin)