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@796: 'contestant_count', 'winner_count'] 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@796: raw_id_fields = ['contestants', 'winners'] bgneal@796: actions = ['pick_winners'] 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@796: def winner_count(self, obj): bgneal@796: return obj.winners.count() bgneal@796: winner_count.short_description = '# Winners' bgneal@796: bgneal@796: def pick_winners(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@796: if not contest.win_date: bgneal@796: contest.pick_winners() 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@796: pick_winners.short_description = "Pick winners for selected contests" bgneal@540: bgneal@540: bgneal@540: bgneal@540: admin.site.register(Contest, ContestAdmin)