annotate contests/admin.py @ 697:67f8d49a9377

Cleaned up the code a bit. Separated the S3 stuff out into its own class. This class maybe should be in core. Still want to do some kind of context manager around the temporary file we are creating to ensure it gets deleted.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Sep 2013 21:02:58 -0500
parents ee87ea74d46b
children 5977b43499f7
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@540 13 'contestant_count', 'winner']
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@540 18 raw_id_fields = ['winner', 'contestants']
bgneal@540 19 actions = ['pick_winner']
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@540 29 def pick_winner(self, request, qs):
bgneal@540 30 """
bgneal@540 31 Picks a winner on the contests selected by the admin. Note that for
bgneal@540 32 safety reasons, we only update those contests that don't have winners
bgneal@540 33 already.
bgneal@540 34
bgneal@540 35 """
bgneal@540 36 count = 0
bgneal@540 37 for contest in qs:
bgneal@540 38 if not contest.winner:
bgneal@540 39 contest.pick_winner()
bgneal@540 40 contest.save()
bgneal@540 41 count += 1
bgneal@540 42
bgneal@540 43 self.message_user(request, "%d of %d winners picked" % (count,
bgneal@540 44 qs.count()))
bgneal@540 45
bgneal@540 46 pick_winner.short_description = "Pick winners for selected contests"
bgneal@540 47
bgneal@540 48
bgneal@540 49
bgneal@540 50 admin.site.register(Contest, ContestAdmin)