annotate contests/views.py @ 1112:654b55ad9134

Tweak countdown admin settings.
author Brian Neal <bgneal@gmail.com>
date Mon, 11 Jul 2016 21:44:09 -0500
parents 16e190fa6ef8
children
rev   line source
bgneal@540 1 """
bgneal@540 2 Views for the contests application.
bgneal@540 3
bgneal@540 4 """
bgneal@679 5 import json
bgneal@679 6
bgneal@540 7 from django.http import (HttpResponse, HttpResponseForbidden,
bgneal@540 8 HttpResponseBadRequest)
bgneal@540 9 from django.shortcuts import get_object_or_404
bgneal@540 10 from django.views.decorators.http import require_POST
bgneal@1085 11 from django.views.generic import DetailView, ListView
bgneal@540 12
bgneal@540 13 from contests.models import Contest
bgneal@540 14
bgneal@540 15
bgneal@1085 16 class ContestListView(ListView):
bgneal@1085 17 context_object_name = 'contests'
bgneal@1085 18 queryset=Contest.public_objects.all()
bgneal@1085 19
bgneal@1085 20 def get_context_data(self, **kwargs):
bgneal@1085 21 context = super(ContestListView, self).get_context_data(**kwargs)
bgneal@1085 22 context['V3_DESIGN'] = True
bgneal@1085 23 return context
bgneal@1085 24
bgneal@1085 25
bgneal@1085 26 class ContestDetailView(DetailView):
bgneal@1085 27 context_object_name = 'contest'
bgneal@1085 28 queryset=Contest.public_objects.all().prefetch_related('winners')
bgneal@1085 29
bgneal@1085 30 def get_context_data(self, **kwargs):
bgneal@1085 31 context = super(ContestDetailView, self).get_context_data(**kwargs)
bgneal@1085 32 context['V3_DESIGN'] = True
bgneal@1085 33 return context
bgneal@1085 34
bgneal@1085 35
bgneal@540 36 @require_POST
bgneal@540 37 def enter(request):
bgneal@540 38 """
bgneal@540 39 This view is an AJAX view that is used to enter or withdraw a user from a
bgneal@540 40 given contest. This function toggles the user's entered state in the
bgneal@540 41 contest.
bgneal@540 42
bgneal@540 43 """
bgneal@540 44 if not request.user.is_authenticated():
bgneal@540 45 return HttpResponseForbidden("Please login first")
bgneal@540 46
bgneal@540 47 contest_id = request.POST.get('contest_id')
bgneal@540 48 if not contest_id:
bgneal@540 49 return HttpResponseBadRequest("Missing contest_id")
bgneal@540 50
bgneal@540 51 contest = get_object_or_404(Contest, pk=contest_id)
bgneal@540 52 if not contest.can_enter():
bgneal@540 53 return HttpResponseForbidden("Contest is over")
bgneal@540 54
bgneal@540 55 # Toggle the user's state in the contest
bgneal@540 56
bgneal@540 57 result = {}
bgneal@540 58 if request.user in contest.contestants.all():
bgneal@540 59 contest.contestants.remove(request.user)
bgneal@540 60 result['entered'] = False
bgneal@540 61 result['msg'] = 'You have been withdrawn from this contest.'
bgneal@540 62 else:
bgneal@540 63 contest.contestants.add(request.user)
bgneal@540 64 result['entered'] = True
bgneal@540 65 result['msg'] = 'You have been entered into this contest!'
bgneal@540 66
bgneal@679 67 json_result = json.dumps(result)
bgneal@679 68 return HttpResponse(json_result, content_type='application/json')