annotate contests/views.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -0500
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')