bgneal@540: """ bgneal@540: Admin definitions for the contest application. bgneal@540: bgneal@540: """ bgneal@540: from django.contrib import admin bgneal@540: from django.conf import settings bgneal@540: bgneal@540: from contests.models import Contest bgneal@540: bgneal@540: bgneal@540: class ContestAdmin(admin.ModelAdmin): bgneal@540: list_display = ['title', 'is_public', 'creation_date', 'end_date', bgneal@540: 'contestant_count', 'winner'] bgneal@540: list_editable = ['is_public'] bgneal@540: date_hierarchy = 'creation_date' bgneal@540: search_fields = ['title', 'description'] bgneal@540: prepopulated_fields = {'slug': ['title']} bgneal@540: raw_id_fields = ['winner', 'contestants'] bgneal@540: actions = ['pick_winner'] bgneal@540: bgneal@540: class Media: bgneal@540: js = (['js/contests/contests_admin.js'] + bgneal@540: settings.GPP_THIRD_PARTY_JS['tiny_mce']) bgneal@540: bgneal@540: def contestant_count(self, obj): bgneal@540: return obj.contestants.count() bgneal@540: contestant_count.short_description = '# Entries' bgneal@540: bgneal@540: def pick_winner(self, request, qs): bgneal@540: """ bgneal@540: Picks a winner on the contests selected by the admin. Note that for bgneal@540: safety reasons, we only update those contests that don't have winners bgneal@540: already. bgneal@540: bgneal@540: """ bgneal@540: count = 0 bgneal@540: for contest in qs: bgneal@540: if not contest.winner: bgneal@540: contest.pick_winner() bgneal@540: contest.save() bgneal@540: count += 1 bgneal@540: bgneal@540: self.message_user(request, "%d of %d winners picked" % (count, bgneal@540: qs.count())) bgneal@540: bgneal@540: pick_winner.short_description = "Pick winners for selected contests" bgneal@540: bgneal@540: bgneal@540: bgneal@540: admin.site.register(Contest, ContestAdmin)