annotate contests/admin.py @ 1141:5a95815f147c

Convert submit news pages to V3 design.
author Brian Neal <bgneal@gmail.com>
date Tue, 01 Nov 2016 19:44:15 -0500 (2016-11-02)
parents 5977b43499f7
children
rev   line source
bgneal@540 1 """
bgneal@540 2 Admin definitions for the contest application.
bgneal@540 3
bgneal@540 4 """
bgneal@540 5 from django.contrib import admin
bgneal@540 6 from django.conf import settings
bgneal@540 7
bgneal@540 8 from contests.models import Contest
bgneal@540 9
bgneal@540 10
bgneal@540 11 class ContestAdmin(admin.ModelAdmin):
bgneal@540 12 list_display = ['title', 'is_public', 'creation_date', 'end_date',
bgneal@796 13 'contestant_count', 'winner_count']
bgneal@540 14 list_editable = ['is_public']
bgneal@540 15 date_hierarchy = 'creation_date'
bgneal@540 16 search_fields = ['title', 'description']
bgneal@540 17 prepopulated_fields = {'slug': ['title']}
bgneal@796 18 raw_id_fields = ['contestants', 'winners']
bgneal@796 19 actions = ['pick_winners']
bgneal@540 20
bgneal@540 21 class Media:
bgneal@540 22 js = (['js/contests/contests_admin.js'] +
bgneal@540 23 settings.GPP_THIRD_PARTY_JS['tiny_mce'])
bgneal@540 24
bgneal@540 25 def contestant_count(self, obj):
bgneal@540 26 return obj.contestants.count()
bgneal@540 27 contestant_count.short_description = '# Entries'
bgneal@540 28
bgneal@796 29 def winner_count(self, obj):
bgneal@796 30 return obj.winners.count()
bgneal@796 31 winner_count.short_description = '# Winners'
bgneal@796 32
bgneal@796 33 def pick_winners(self, request, qs):
bgneal@540 34 """
bgneal@540 35 Picks a winner on the contests selected by the admin. Note that for
bgneal@540 36 safety reasons, we only update those contests that don't have winners
bgneal@540 37 already.
bgneal@540 38
bgneal@540 39 """
bgneal@540 40 count = 0
bgneal@540 41 for contest in qs:
bgneal@796 42 if not contest.win_date:
bgneal@796 43 contest.pick_winners()
bgneal@540 44 contest.save()
bgneal@540 45 count += 1
bgneal@540 46
bgneal@540 47 self.message_user(request, "%d of %d winners picked" % (count,
bgneal@540 48 qs.count()))
bgneal@540 49
bgneal@796 50 pick_winners.short_description = "Pick winners for selected contests"
bgneal@540 51
bgneal@540 52
bgneal@540 53
bgneal@540 54 admin.site.register(Contest, ContestAdmin)