Mercurial > public > sg101
comparison gpp/contests/admin.py @ 540:51fa1e0ca218
For #243, create a contests application.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 09 Jan 2012 01:13:08 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
539:2f0a372c92b4 | 540:51fa1e0ca218 |
---|---|
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) |