Mercurial > public > sg101
annotate contests/views.py @ 752:95f4e7f352fd
For Django 1.6: contrib auth password reset confirm view signature changed.
The uidb64 parameter was previously base 36 encoded and named uidb36.
Had to update urls.py. While I was in there I decided to make the
password reset email use the {% url %} tag to be more resilient if the
url changes.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 01 Jan 2014 19:52:07 -0600 |
parents | 89b240fe9297 |
children | 16e190fa6ef8 |
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@540 | 11 |
bgneal@540 | 12 from contests.models import Contest |
bgneal@540 | 13 |
bgneal@540 | 14 |
bgneal@540 | 15 @require_POST |
bgneal@540 | 16 def enter(request): |
bgneal@540 | 17 """ |
bgneal@540 | 18 This view is an AJAX view that is used to enter or withdraw a user from a |
bgneal@540 | 19 given contest. This function toggles the user's entered state in the |
bgneal@540 | 20 contest. |
bgneal@540 | 21 |
bgneal@540 | 22 """ |
bgneal@540 | 23 if not request.user.is_authenticated(): |
bgneal@540 | 24 return HttpResponseForbidden("Please login first") |
bgneal@540 | 25 |
bgneal@540 | 26 contest_id = request.POST.get('contest_id') |
bgneal@540 | 27 if not contest_id: |
bgneal@540 | 28 return HttpResponseBadRequest("Missing contest_id") |
bgneal@540 | 29 |
bgneal@540 | 30 contest = get_object_or_404(Contest, pk=contest_id) |
bgneal@540 | 31 if not contest.can_enter(): |
bgneal@540 | 32 return HttpResponseForbidden("Contest is over") |
bgneal@540 | 33 |
bgneal@540 | 34 # Toggle the user's state in the contest |
bgneal@540 | 35 |
bgneal@540 | 36 result = {} |
bgneal@540 | 37 if request.user in contest.contestants.all(): |
bgneal@540 | 38 contest.contestants.remove(request.user) |
bgneal@540 | 39 result['entered'] = False |
bgneal@540 | 40 result['msg'] = 'You have been withdrawn from this contest.' |
bgneal@540 | 41 else: |
bgneal@540 | 42 contest.contestants.add(request.user) |
bgneal@540 | 43 result['entered'] = True |
bgneal@540 | 44 result['msg'] = 'You have been entered into this contest!' |
bgneal@540 | 45 |
bgneal@679 | 46 json_result = json.dumps(result) |
bgneal@679 | 47 return HttpResponse(json_result, content_type='application/json') |