bornhack-website/tickets/views.py

60 lines
1.7 KiB
Python
Raw Normal View History

2016-05-06 20:33:59 +00:00
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render
2016-05-06 20:33:59 +00:00
from django.views.generic import CreateView, TemplateView, DetailView
from django.core.urlresolvers import reverse_lazy
from camps.models import Camp
from .models import Ticket, TicketType
from .forms import TicketForm
class CampTicketSaleCheck(object):
def dispatch(self, *args, **kwargs):
current_camp = Camp.objects.current()
if current_camp and current_camp.ticket_sale_open:
2016-05-07 09:28:57 +00:00
return super(CampTicketSaleCheck, self).dispatch(*args, **kwargs)
2016-05-06 20:33:59 +00:00
raise Http404()
class TicketIndexView(CampTicketSaleCheck, TemplateView):
template_name = "tickets/index.html"
def get_context_data(self, **kwargs):
2016-05-07 09:28:57 +00:00
context = super(TicketIndexView, self).get_context_data(**kwargs)
2016-05-06 20:33:59 +00:00
context['ticket_types'] = TicketType.objects.all()
return context
class TicketDetailView(CampTicketSaleCheck, DetailView):
model = Ticket
template_name = 'tickets/detail.html'
context_object_name = 'ticket'
class BuyTicketView(CampTicketSaleCheck, CreateView):
model = Ticket
template_name = "tickets/buy.html"
form_class = TicketForm
def get_form_kwargs(self):
2016-05-07 09:28:57 +00:00
kwargs = super(BuyTicketView, self).get_form_kwargs()
2016-05-06 20:33:59 +00:00
ticket_type = self.request.GET.get('ticket_type', None)
if ticket_type:
kwargs['initial'] = {
'ticket_type': ticket_type
}
return kwargs
def form_valid(self, form):
instance = form.save(commit=False)
instance.user = self.request.user
instance.save()
return HttpResponseRedirect(
reverse_lazy('tickets:detail', kwargs={
'pk': str(instance.pk)
})
)