def test_form_valid(self): form_data = {'quantity': 2} form = TicketPurchaseForm( data=form_data, ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking ) self.assertTrue(form.is_valid())
def test_quantity_choices(self): form = TicketPurchaseForm(ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking) self.assertEqual(self.ticketed_event.tickets_left(), 10) self.assertEqual(self.ticketed_event.max_tickets, 10) self.assertIsNone(self.ticketed_event.max_ticket_purchase) quantity_widget = form.fields['quantity'].widget # choices 1 - 10: max tickets, none bought yet choices = [(i, i) for i in range(1, 11)] choices.insert(0, (0, '------')) self.assertEqual(quantity_widget.choices, choices)
def test_quantity_choices_max_ticket_purchase(self): self.ticketed_event.max_ticket_purchase = 5 self.ticketed_event.save() form = TicketPurchaseForm(ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking) self.assertEqual(self.ticketed_event.tickets_left(), 10) self.assertEqual(self.ticketed_event.max_tickets, 10) self.assertEqual(self.ticketed_event.max_ticket_purchase, 5) quantity_widget = form.fields['quantity'].widget # choices 1 - 5: max tickets 10 but max single purchase is 5 choices = [(i, i) for i in range(1, 6)] choices.insert(0, (0, '------')) self.assertEqual(quantity_widget.choices, choices)
def test_quantity_choices_no_max_ticket_purchase_or_max_tickets(self): self.ticketed_event.max_tickets = None self.ticketed_event.save() form = TicketPurchaseForm(ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking) self.assertEqual(self.ticketed_event.tickets_left(), 10000) self.assertIsNone(self.ticketed_event.max_tickets) self.assertIsNone(self.ticketed_event.max_ticket_purchase) quantity_widget = form.fields['quantity'].widget # choices 1 - 100 choices = [(i, i) for i in range(1, 101)] choices.insert(0, (0, '------')) self.assertEqual(quantity_widget.choices, choices)
def test_quantity_choices_with_booked_tickets_on_cancelled_booking(self): new_booking = mommy.make(TicketBooking, ticketed_event=self.ticketed_event, purchase_confirmed=True, cancelled=True) mommy.make(Ticket, ticket_booking=new_booking, _quantity=8) form = TicketPurchaseForm(ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking) self.assertEqual(self.ticketed_event.tickets_left(), 10) self.assertEqual(self.ticketed_event.max_tickets, 10) self.assertIsNone(self.ticketed_event.max_ticket_purchase) quantity_widget = form.fields['quantity'].widget # choices = 1-10; 8 tickets bought on another booking, but cancelled, so # still 10 left to book choices = [(i, i) for i in range(1, 11)] choices.insert(0, (0, '------')) self.assertEqual(quantity_widget.choices, choices)
def test_quantity_choices_with_booked_tickets_multiple_bookings(self): new_booking = mommy.make(TicketBooking, ticketed_event=self.ticketed_event, purchase_confirmed=True) mommy.make(Ticket, ticket_booking=new_booking, _quantity=4) mommy.make(Ticket, ticket_booking=self.ticket_booking, _quantity=4) form = TicketPurchaseForm(ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking) self.assertEqual(self.ticketed_event.tickets_left(), 2) self.assertEqual(self.ticketed_event.max_tickets, 10) self.assertIsNone(self.ticketed_event.max_ticket_purchase) quantity_widget = form.fields['quantity'].widget # choices = 1-6; 4 tickets booked on another booking, 6 left altogether, # including the 4 already allocated to this booking choices = [(i, i) for i in range(1, 7)] choices.insert(0, (0, '------')) self.assertEqual(quantity_widget.choices, choices)
def test_quantity_choices_with_booked_tickets_on_same_ticket_booking(self): """ tickets booked on the current ticket booking do not affect the choices (if user has booked 8 out of 10 max tickets, we want them to have the option to change it to e.g. 4, so don't want to limit it to 2) """ mommy.make(Ticket, ticket_booking=self.ticket_booking, _quantity=8) form = TicketPurchaseForm(ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking) self.assertEqual(self.ticketed_event.tickets_left(), 2) self.assertEqual(self.ticketed_event.max_tickets, 10) self.assertIsNone(self.ticketed_event.max_ticket_purchase) quantity_widget = form.fields['quantity'].widget # choices = 1-10; 8 tickets booked on this booking, user can change # quantity to up to 10 still choices = [(i, i) for i in range(1, 11)] choices.insert(0, (0, '------')) self.assertEqual(quantity_widget.choices, choices)
def test_quantity_choices_with_booked_unconfirmed_tickets_on_same_tb(self): """ same quantity choices shoould be returned if the purchase is not yet confirmed """ self.ticket_booking.purchase_confirmed = False self.ticket_booking.save() mommy.make(Ticket, ticket_booking=self.ticket_booking, _quantity=8) form = TicketPurchaseForm(ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking) # tickets left is 10 since this booking is unconfirmed self.assertEqual(self.ticketed_event.tickets_left(), 10) self.assertEqual(self.ticketed_event.max_tickets, 10) self.assertIsNone(self.ticketed_event.max_ticket_purchase) quantity_widget = form.fields['quantity'].widget # choices = 1-10; 8 tickets booked on this booking, user can change # quantity to up to 10 still choices = [(i, i) for i in range(1, 11)] choices.insert(0, (0, '------')) self.assertEqual(quantity_widget.choices, choices)
def test_quantity_choices_max_ticket_purchase_and_booked_tickets(self): self.ticketed_event.max_ticket_purchase = 5 self.ticketed_event.save() new_booking = mommy.make( TicketBooking, ticketed_event=self.ticketed_event, purchase_confirmed=True, ) mommy.make(Ticket, ticket_booking=new_booking, _quantity=7) self.ticketed_event.save() form = TicketPurchaseForm(ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking) self.assertEqual(self.ticketed_event.tickets_left(), 3) self.assertEqual(self.ticketed_event.max_tickets, 10) self.assertEqual(self.ticketed_event.max_ticket_purchase, 5) quantity_widget = form.fields['quantity'].widget # choices 1 - 3: max single purchase is 5, but there are only 3 left choices = [(i, i) for i in range(1, 4)] choices.insert(0, (0, '------')) self.assertEqual(quantity_widget.choices, choices)
def get_context_data(self, **kwargs): context = super(TicketCreateView, self).get_context_data(**kwargs) context['ticketed_event'] = self.ticketed_event context['ticket_booking'] = self.ticket_booking ticket_purchase_form = TicketPurchaseForm( prefix='ticket_purchase_form', ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking, data=self.request.POST if 'ticket_purchase_form-quantity' in self.request.POST else None, ) context['ticket_purchase_form'] = ticket_purchase_form context['ticket_formset'] = TicketFormSet( prefix='ticket_formset', data=self.request.POST if 'ticket_formset-submit' in self.request.POST else None, instance=self.ticket_booking, ticketed_event=self.ticketed_event, ) return context
def test_form_valid(self): form_data = {'quantity': 2} form = TicketPurchaseForm(data=form_data, ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking) self.assertTrue(form.is_valid())
def post(self, request, *args, **kwargs): if 'cancel' in request.POST: self.ticket_booking.delete() messages.info( request, 'Ticket booking for {} has been cancelled.'.format( self.ticketed_event)) return HttpResponseRedirect(reverse('booking:ticketed_events')) context = self.get_context_data() ticket_purchase_form = context['ticket_purchase_form'] ticket_formset = context['ticket_formset'] if ticket_purchase_form.has_changed(): # tickets on current booking are only included in the tickets_left # calculation if purchase has been confirmed old_tickets = self.ticket_booking.tickets.all() old_ticket_count = old_tickets.count() if self.ticket_booking.purchase_confirmed: tickets_left_excl_this = self.ticketed_event.tickets_left() \ + old_ticket_count else: tickets_left_excl_this = self.ticketed_event.tickets_left() new_quantity = int( request.POST.get('ticket_purchase_form-quantity')) if new_quantity > tickets_left_excl_this: messages.error( request, 'Cannot purchase the number of tickets requested. ' 'Only {} tickets left.'.format( self.ticketed_event.tickets_left())) else: # create the correct number of tickets on this booking if old_ticket_count < new_quantity: for i in range(new_quantity - old_ticket_count): Ticket.objects.create( ticket_booking=self.ticket_booking) if old_ticket_count > new_quantity: for ticket in old_tickets[new_quantity:]: ticket.delete() if old_ticket_count > 0: ActivityLog.objects.create( log="Ticket quantity updated on booking ref {}".format( self.ticket_booking.booking_reference)) tickets = self.ticket_booking.tickets.all() context['tickets'] = tickets return TemplateResponse(request, self.template_name, context) if 'ticket_formset-submit' in request.POST: if ticket_formset.is_valid(): ticket_formset.save() # we only create the paypal form if there is a ticket cost and # online payments are open if self.ticketed_event.ticket_cost and \ self.ticketed_event.payment_open: invoice_id = create_ticket_booking_paypal_transaction( self.request.user, self.ticket_booking).invoice_id host = 'http://{}'.format( self.request.META.get('HTTP_HOST')) paypal_form = PayPalPaymentsUpdateForm( initial=context_helpers.get_paypal_dict( host, self.ticketed_event.ticket_cost, self.ticketed_event, invoice_id, '{} {}'.format('ticket_booking', self.ticket_booking.id), paypal_email=self.ticketed_event.paypal_email, quantity=self.ticket_booking.tickets.count())) context["paypalform"] = paypal_form self.ticket_booking.purchase_confirmed = True # reset the ticket_booking booked date to the date user confirms context['purchase_confirmed'] = True self.ticket_booking.date_booked = timezone.now() self.ticket_booking.save() ActivityLog.objects.create( log="Ticket Purchase confirmed: event {}, user {}, " "booking ref {}".format( self.ticketed_event.name, request.user.username, self.ticket_booking.booking_reference)) host = 'http://{}'.format(request.META.get('HTTP_HOST')) ctx = { 'host': host, 'ticketed_event': self.ticketed_event, 'ticket_booking': self.ticket_booking, 'ticket_count': self.ticket_booking.tickets.count(), 'user': request.user, } try: # send notification email to user send_mail( '{} Ticket booking confirmed for {}: ref {}'.format( settings.ACCOUNT_EMAIL_SUBJECT_PREFIX, self.ticketed_event, self.ticket_booking.booking_reference, ), get_template('booking/email/ticket_booking_made.txt' ).render(ctx), settings.DEFAULT_FROM_EMAIL, [request.user.email], html_message=get_template( 'booking/email/ticket_booking_made.html').render( ctx), fail_silently=False) except Exception as e: # send mail to tech support with Exception send_support_email( e, __name__, "ticket booking created - " "send email to user") if self.ticketed_event.email_studio_when_purchased: try: send_mail( '{} Ticket booking confirmed for {}: ref {}'. format( settings.ACCOUNT_EMAIL_SUBJECT_PREFIX, self.ticketed_event, self.ticket_booking.booking_reference, ), get_template( 'booking/email/to_studio_ticket_booking_made.txt' ).render(ctx), settings.DEFAULT_FROM_EMAIL, [settings.DEFAULT_STUDIO_EMAIL], html_message=get_template( 'booking/email/to_studio_ticket_booking_made.html' ).render(ctx), fail_silently=False) except Exception as e: # send mail to tech support with Exception send_support_email( e, __name__, "ticket booking created - " "send email to studio") else: messages.error(request, "Please correct errors in the form below") tickets = self.ticket_booking.tickets.all() context['tickets'] = tickets ticket_purchase_form = TicketPurchaseForm( prefix='ticket_purchase_form', ticketed_event=self.ticketed_event, ticket_booking=self.ticket_booking, initial={'quantity': self.ticket_booking.tickets.count()}) context["ticket_purchase_form"] = ticket_purchase_form return TemplateResponse(request, self.template_name, context)