comparison contests/admin.py @ 796:5977b43499f7

Modified contests to have multiple winners.
author Brian Neal <bgneal@gmail.com>
date Fri, 04 Jul 2014 20:30:14 -0500
parents ee87ea74d46b
children
comparison
equal deleted inserted replaced
795:5d48fd80f27d 796:5977b43499f7
8 from contests.models import Contest 8 from contests.models import Contest
9 9
10 10
11 class ContestAdmin(admin.ModelAdmin): 11 class ContestAdmin(admin.ModelAdmin):
12 list_display = ['title', 'is_public', 'creation_date', 'end_date', 12 list_display = ['title', 'is_public', 'creation_date', 'end_date',
13 'contestant_count', 'winner'] 13 'contestant_count', 'winner_count']
14 list_editable = ['is_public'] 14 list_editable = ['is_public']
15 date_hierarchy = 'creation_date' 15 date_hierarchy = 'creation_date'
16 search_fields = ['title', 'description'] 16 search_fields = ['title', 'description']
17 prepopulated_fields = {'slug': ['title']} 17 prepopulated_fields = {'slug': ['title']}
18 raw_id_fields = ['winner', 'contestants'] 18 raw_id_fields = ['contestants', 'winners']
19 actions = ['pick_winner'] 19 actions = ['pick_winners']
20 20
21 class Media: 21 class Media:
22 js = (['js/contests/contests_admin.js'] + 22 js = (['js/contests/contests_admin.js'] +
23 settings.GPP_THIRD_PARTY_JS['tiny_mce']) 23 settings.GPP_THIRD_PARTY_JS['tiny_mce'])
24 24
25 def contestant_count(self, obj): 25 def contestant_count(self, obj):
26 return obj.contestants.count() 26 return obj.contestants.count()
27 contestant_count.short_description = '# Entries' 27 contestant_count.short_description = '# Entries'
28 28
29 def pick_winner(self, request, qs): 29 def winner_count(self, obj):
30 return obj.winners.count()
31 winner_count.short_description = '# Winners'
32
33 def pick_winners(self, request, qs):
30 """ 34 """
31 Picks a winner on the contests selected by the admin. Note that for 35 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 36 safety reasons, we only update those contests that don't have winners
33 already. 37 already.
34 38
35 """ 39 """
36 count = 0 40 count = 0
37 for contest in qs: 41 for contest in qs:
38 if not contest.winner: 42 if not contest.win_date:
39 contest.pick_winner() 43 contest.pick_winners()
40 contest.save() 44 contest.save()
41 count += 1 45 count += 1
42 46
43 self.message_user(request, "%d of %d winners picked" % (count, 47 self.message_user(request, "%d of %d winners picked" % (count,
44 qs.count())) 48 qs.count()))
45 49
46 pick_winner.short_description = "Pick winners for selected contests" 50 pick_winners.short_description = "Pick winners for selected contests"
47 51
48 52
49 53
50 admin.site.register(Contest, ContestAdmin) 54 admin.site.register(Contest, ContestAdmin)