<commit_before>import datetime

from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.template.defaultfilters import slugify
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.views.decorators.cache import never_cache

from components.makahiki_base import get_round_info, get_current_round
from components.prizes import POINTS_PER_TICKET
from components.prizes.models import Prize, RafflePrize, RaffleDeadline

@login_required
@never_cache
def index(request):
  floor = request.user.get_profile().floor
  prizes = _get_prizes(floor)
  raffle_dict = _get_raffle_prizes(request.user)
    
  return render_to_response("view_prizes/index.html", {
      "prizes": prizes,
      "raffle": raffle_dict,
  }, context_instance=RequestContext(request))
  
@login_required
def add_ticket(request, prize_id):
  """
  Adds a user's raffle ticket to the prize.
  """
  prize = get_object_or_404(RafflePrize, id=prize_id)
  deadline = prize.deadline
  profile = request.user.get_profile()
  in_deadline = (deadline.pub_date <= datetime.datetime.today()) and (deadline.end_date >= datetime.datetime.today())
  if profile.available_tickets > 0 and in_deadline:
    prize.add_ticket(request.user)
    return HttpResponseRedirect(reverse("prizes_index"))
  elif not in_deadline:
    messages.error(request, "The raffle for this round is over.")
    return HttpResponseRedirect(reverse("prizes_index"))
    
  raise Http404
  
@login_required
def remove_ticket(request, prize_id):
  """
  Removes a user's raffle ticket from the prize.
  """
  prize = get_object_or_404(RafflePrize, id=prize_id)
  if prize.allocated_tickets(request.user) > 0:
    prize.remove_ticket(request.user)
    return HttpResponseRedirect(reverse("prizes_index"))
    
  raise Http404
  
def _get_prizes(floor):
  """
  Private method to process the prizes half of the page.<commit_msg>Add and remove ticket methods should require POST.  Added some additional error handling.<commit_after>import datetime

from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.template.defaultfilters import slugify
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.views.decorators.cache import never_cache

from components.makahiki_base import get_round_info, get_current_round
from components.prizes import POINTS_PER_TICKET
from components.prizes.models import Prize, RafflePrize, RaffleDeadline

@login_required
@never_cache
def index(request):
  floor = request.user.get_profile().floor
  prizes = _get_prizes(floor)
  raffle_dict = _get_raffle_prizes(request.user)
    
  return render_to_response("view_prizes/index.html", {
      "prizes": prizes,
      "raffle": raffle_dict,
  }, context_instance=RequestContext(request))
  
@login_required
def add_ticket(request, prize_id):
  """
  Adds a user's raffle ticket to the prize.
  """
  if request.method == "POST":
    prize = get_object_or_404(RafflePrize, id=prize_id)
    deadline = prize.deadline
    profile = request.user.get_profile()
    in_deadline = (deadline.pub_date <= datetime.datetime.today()) and (deadline.end_date >= datetime.datetime.today())
    if profile.available_tickets > 0 and in_deadline:
      prize.add_ticket(request.user)
      return HttpResponseRedirect(reverse("prizes_index"))
    elif not in_deadline:
      messages.error(request, "The raffle for this round is over.")
      return HttpResponseRedirect(reverse("prizes_index"))
    elif profile.available_tickets <= 0:
      messages.error(request, "Sorry, but you do not have any more tickets.")
      return HttpResponseRedirect(reverse("prizes_index"))
    
  raise Http404
  
@login_required
def remove_ticket(request, prize_id):
  """
  Removes a user's raffle ticket from the prize.
  """
  if request.method == "POST":
    prize = get_object_or_404(RafflePrize, id=prize_id)
    if prize.allocated_tickets(request.user) > 0:
      prize.remove_ticket(request.user)
      return HttpResponseRedirect(reverse("prizes_index"))

    elif profile.available_tickets <= 0:
      messages.error(request, "Sorry, but you do not have any tickets in this prize.")
      return HttpResponseRedirect(reverse("prizes_index"))
    
  raise Http404
  
def _get_prizes(floor):
  """
  Private method to process the prizes half of the page.
{'fraction_non_alphanumeric': 0.08673250322026621, 'fraction_numerical': 0.009016745384285102, 'mean_word_length': 4.996138996138996, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3493791', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1433, 'n_words': 351}