def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(TicketBookingListView, self).get_context_data(**kwargs) ticketbookinglist = [] for ticket_booking in self.object_list: if not ticket_booking.cancelled and not ticket_booking.paid \ and ticket_booking.ticketed_event.payment_open: # ONLY DO THIS IF PAYPAL BUTTON NEEDED invoice_id = create_ticket_booking_paypal_transaction( self.request.user, ticket_booking).invoice_id host = 'http://{}'.format(self.request.META.get('HTTP_HOST')) paypal_form = PayPalPaymentsListForm( initial=context_helpers.get_paypal_dict( host, ticket_booking.ticketed_event.ticket_cost, ticket_booking.ticketed_event, invoice_id, '{} {}'.format('ticket_booking', ticket_booking.id), paypal_email=ticket_booking.ticketed_event. paypal_email, quantity=ticket_booking.tickets.count())) else: paypal_form = None ticketbookingform = { 'ticket_booking': ticket_booking, 'paypalform': paypal_form } ticketbookinglist.append(ticketbookingform) context['ticketbookinglist'] = ticketbookinglist return context
def test_paypal_view(request): ctx = {'sidenav_selection': 'test_paypal'} if request.method == 'GET': email = request.GET.get('email', '') ctx.update({'email': email}) elif request.method == 'POST': email = request.POST.get('email') if not email: ctx.update( {'email_errors': 'Please enter an email address to test'} ) else: ramdomnum = shortuuid.ShortUUID().random(length=6) invoice_id = '{}_{}'.format(email, ramdomnum) host = 'http://{}'.format(request.META.get('HTTP_HOST')) paypal_form = PayPalPaymentsUpdateForm( initial=get_paypal_dict( host, 0.01, 'paypal_test', invoice_id, 'paypal_test 0 {} {} {}'.format( invoice_id, email, request.user.email ), paypal_email=email, ) ) ctx.update({'paypalform': paypal_form, 'email': email}) return TemplateResponse(request, 'studioadmin/test_paypal_email.html', ctx)
def test_paypal_view(request): ctx = {'sidenav_selection': 'test_paypal'} if request.method == 'GET': email = request.GET.get('email', '') ctx.update({'email': email}) elif request.method == 'POST': email = request.POST.get('email') if not email: ctx.update( {'email_errors': 'Please enter an email address to test'}) else: ramdomnum = shortuuid.ShortUUID().random(length=6) invoice_id = '{}_{}'.format(email, ramdomnum) host = 'http://{}'.format(request.META.get('HTTP_HOST')) paypal_form = PayPalPaymentsUpdateForm(initial=get_paypal_dict( host, 0.01, 'paypal_test', invoice_id, 'paypal_test 0 {} {} {}'.format(invoice_id, email, request.user.email), paypal_email=email, )) ctx.update({'paypalform': paypal_form, 'email': email}) return TemplateResponse(request, 'studioadmin/test_paypal_email.html', ctx)
def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(TicketBookingListView, self).get_context_data(**kwargs) ticketbookinglist = [] for ticket_booking in self.object_list: if not ticket_booking.cancelled and not ticket_booking.paid \ and ticket_booking.ticketed_event.payment_open: # ONLY DO THIS IF PAYPAL BUTTON NEEDED invoice_id = create_ticket_booking_paypal_transaction( self.request.user, ticket_booking).invoice_id host = 'http://{}'.format(self.request.META.get('HTTP_HOST')) paypal_form = PayPalPaymentsListForm( initial=context_helpers.get_paypal_dict( host, ticket_booking.ticketed_event.ticket_cost, ticket_booking.ticketed_event, invoice_id, '{} {}'.format('ticket_booking', ticket_booking.id), paypal_email=ticket_booking.ticketed_event.paypal_email, quantity=ticket_booking.tickets.count() ) ) else: paypal_form = None ticketbookingform = { 'ticket_booking': ticket_booking, 'paypalform': paypal_form } ticketbookinglist.append(ticketbookingform) context['ticketbookinglist'] = ticketbookinglist return context
def test_PayPalPaymentsListForm_renders_buy_it_now_button(self): booking = mommy.make_recipe('booking.booking') pptrans = helpers.create_booking_paypal_transaction( booking.user, booking) form = PayPalPaymentsListForm(initial=get_paypal_dict( 'http://example.com', booking.event.cost, booking.event, pptrans.invoice_id, '{} {}'.format('booking', booking.id))) self.assertIn('Buy it Now', form.render())
def test_PayPalPaymentsUpdateForm_renders_buy_it_now_button(self): booking = mommy.make_recipe('booking.booking') pptrans = helpers.create_booking_paypal_transaction( booking.user, booking ) form = PayPalPaymentsUpdateForm( initial=get_paypal_dict( 'http://example.com', booking.event.cost, booking.event, pptrans.invoice_id, '{} {}'.format('booking', booking.id) ) ) self.assertIn('Buy it Now', form.render())
def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(BookingUpdateView, self).get_context_data(**kwargs) invoice_id = create_booking_paypal_transaction( self.request.user, self.object ).invoice_id host = 'http://{}'.format(self.request.META.get('HTTP_HOST')) paypal_cost = self.object.event.cost voucher = kwargs.get('voucher', None) voucher_error = kwargs.get('voucher_error', None) code = kwargs.get('code', None) context['voucher_form'] = VoucherForm(initial={'code': code}) if voucher: valid = not bool(voucher_error) context['valid_voucher'] = valid if valid: paypal_cost = Decimal( float(paypal_cost) * ((100 - voucher.discount) / 100) ).quantize(Decimal('.05')) messages.info(self.request, 'Voucher has been applied') times_used = UsedEventVoucher.objects.filter( voucher=voucher, user=self.request.user ).count() context['times_voucher_used'] = times_used paypal_form = PayPalPaymentsUpdateForm( initial=context_helpers.get_paypal_dict( host, paypal_cost, '{}'.format(self.object.event), invoice_id, '{} {}{}'.format( 'booking', self.object.id, ' {}'.format(voucher.code) if voucher else '' ), paypal_email=self.object.event.paypal_email, ) ) context["paypalform"] = paypal_form context["paypal_cost"] = paypal_cost return context_helpers.get_booking_create_context( self.object.event, self.request, context )
def get_extra_context(self, **kwargs): context = {} try: self.request.user.online_disclaimer context['disclaimer'] = True except ObjectDoesNotExist: pass try: self.request.user.print_disclaimer context['disclaimer'] = True except ObjectDoesNotExist: pass types_available_to_book = context_helpers.\ get_blocktypes_available_to_book(self.request.user) if types_available_to_book: context['can_book_block'] = True voucher = kwargs.get('voucher', None) voucher_error = kwargs.get('voucher_error', None) code = kwargs.get('code', None) context['voucher_form'] = VoucherForm(initial={'code': code}) if voucher: context['voucher'] = voucher if voucher_error: context['voucher_error'] = voucher_error valid_voucher = False if voucher: valid_voucher = not bool(voucher_error) context['valid_voucher'] = valid_voucher blockformlist = [] for block in self.get_queryset(): expired = block.expiry_date < timezone.now() paypal_cost = None voucher_applied = False if not block.paid and not expired: context['has_unpaid_block'] = True paypal_cost = block.block_type.cost if valid_voucher and voucher.check_block_type(block.block_type): paypal_cost = Decimal( float(paypal_cost) * ((100 - voucher.discount) / 100) ).quantize(Decimal('.05')) voucher_applied = True invoice_id = create_block_paypal_transaction( self.request.user, block).invoice_id host = 'http://{}'.format(self.request.META.get('HTTP_HOST')) paypal_form = PayPalPaymentsListForm( initial=context_helpers.get_paypal_dict( host, paypal_cost, block.block_type, invoice_id, '{} {}{}'.format( 'block', block.id, ' {}'.format(code) if valid_voucher else '' ), paypal_email=block.block_type.paypal_email ) ) else: paypal_form = None full = Booking.objects.filter( block__id=block.id).count() >= block.block_type.size blockform = { 'block': block, 'voucher_applied': voucher_applied, 'block_cost': paypal_cost, 'paypalform': paypal_form, 'expired': expired or full} blockformlist.append(blockform) context['blockformlist'] = blockformlist return context
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)
def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(BookingListView, self).get_context_data(**kwargs) user_blocks = self.request.user.blocks.all() active_block_event_types = [ block.block_type.event_type for block in user_blocks if block.active_block() ] bookingformlist = [] for booking in self.object_list: if booking.event.event_type not in active_block_event_types \ and booking.status == 'OPEN' and not booking.paid: # ONLY DO THIS IF PAYPAL BUTTON NEEDED invoice_id = create_booking_paypal_transaction( self.request.user, booking).invoice_id host = 'http://{}'.format(self.request.META.get('HTTP_HOST')) paypal_form = PayPalPaymentsListForm( initial=context_helpers.get_paypal_dict( host, booking.event.cost, booking.event, invoice_id, '{} {}'.format('booking', booking.id), paypal_email=booking.event.paypal_email, ) ) else: paypal_form = None try: WaitingListUser.objects.get(user=self.request.user, event=booking.event) on_waiting_list = True except WaitingListUser.DoesNotExist: on_waiting_list = False can_cancel = booking.event.allow_booking_cancellation and \ booking.event.can_cancel() and \ (booking.status == 'OPEN' and not booking.no_show) due_date_time = None if booking.event.advance_payment_required: uk_tz = pytz.timezone('Europe/London') if booking.event.payment_due_date: due_date_time = booking.event.payment_due_date elif booking.event.payment_time_allowed: last_booked = booking.date_rebooked if booking.date_rebooked else booking.date_booked due_date_time = last_booked + timedelta(hours=booking.event.payment_time_allowed) elif booking.event.cancellation_period: due_date_time = booking.event.date - timedelta( hours=booking.event.cancellation_period ) due_date_time = due_date_time.astimezone(uk_tz) bookingform = { 'booking_status': 'CANCELLED' if (booking.status == 'CANCELLED' or booking.no_show) else 'OPEN', 'ev_type': booking.event.event_type.event_type, 'booking': booking, 'paypalform': paypal_form, 'has_available_block': booking.event.event_type in active_block_event_types, 'can_cancel': can_cancel, 'on_waiting_list': on_waiting_list, 'due_date_time': due_date_time, } bookingformlist.append(bookingform) context['bookingformlist'] = bookingformlist return context
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)