def post(self, request, *args, **kwargs): booking = Booking.objects.get( pk=request.session['ps_booking'] ) if 'ps_booking' in request.session else None if request.user.is_anonymous(): form = AnonymousMakeBookingsForm(request.POST) else: form = MakeBookingsForm(request.POST) vehicles = VehicleInfoFormset(request.POST) # re-render the page if there's no booking in the session if not booking: return self.render_page(request, booking, form, vehicles) # re-render the page if the form doesn't validate if (not form.is_valid()) or (not vehicles.is_valid()): return self.render_page(request, booking, form, vehicles, show_errors=True) # update the booking object with information from the form if not booking.details: booking.details = {} booking.details['first_name'] = form.cleaned_data.get('first_name') booking.details['last_name'] = form.cleaned_data.get('last_name') booking.details['phone'] = form.cleaned_data.get('phone') booking.details['country'] = form.cleaned_data.get( 'country').iso_3166_1_a2 booking.details['postcode'] = form.cleaned_data.get('postcode') booking.details['num_adult'] = form.cleaned_data.get('num_adult') booking.details['num_concession'] = form.cleaned_data.get( 'num_concession') booking.details['num_child'] = form.cleaned_data.get('num_child') booking.details['num_infant'] = form.cleaned_data.get('num_infant') # update vehicle registrations from form VEHICLE_CHOICES = {'0': 'vehicle', '1': 'concession', '2': 'motorbike'} BookingVehicleRego.objects.filter(booking=booking).delete() for vehicle in vehicles: BookingVehicleRego.objects.create( booking=booking, rego=vehicle.cleaned_data.get('vehicle_rego'), type=VEHICLE_CHOICES[vehicle.cleaned_data.get('vehicle_type')], entry_fee=vehicle.cleaned_data.get('entry_fee')) # Check if number of people is exceeded in any of the campsites for c in booking.campsites.all(): if booking.num_guests > c.campsite.max_people: form.add_error( None, 'Number of people exceeded for the current camp site.') return self.render_page(request, booking, form, vehicles, show_errors=True) # Prevent booking if less than min people if booking.num_guests < c.campsite.min_people: form.add_error( 'Number of people is less than the minimum allowed for the current campsite.' ) return self.render_page(request, booking, form, vehicles, show_errors=True) # generate final pricing try: lines = utils.price_or_lineitems(request, booking, booking.campsite_id_list) except Exception as e: form.add_error( None, '{} Please contact Parks and Visitors services with this error message, the campground/campsite and the time of the request.' .format(str(e))) return self.render_page(request, booking, form, vehicles, show_errors=True) print(lines) total = sum( [Decimal(p['price_incl_tax']) * p['quantity'] for p in lines]) # get the customer object if request.user.is_anonymous(): # searching on EmailIdentity looks for both EmailUser and Profile objects with the email entered by user customer_qs = EmailIdentity.objects.filter( email__iexact=form.cleaned_data.get('email')) if customer_qs: customer = customer_qs.first().user else: customer = EmailUser.objects.create( email=form.cleaned_data.get('email'), first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), phone_number=form.cleaned_data.get('phone'), mobile_number=form.cleaned_data.get('phone')) Address.objects.create( line1='address', user=customer, postcode=form.cleaned_data.get('postcode'), country=form.cleaned_data.get('country').iso_3166_1_a2) else: customer = request.user # FIXME: get feedback on whether to overwrite personal info if the EmailUser # already exists # finalise the booking object booking.customer = customer booking.cost_total = total booking.save() # generate invoice reservation = u"Reservation for {} confirmation {}".format( u'{} {}'.format(booking.customer.first_name, booking.customer.last_name), booking.id) logger.info( u'{} built booking {} and handing over to payment gateway'.format( u'User {} with id {}'.format(booking.customer.get_full_name(), booking.customer.id) if booking.customer else u'An anonymous user', booking.id)) result = utils.checkout(request, booking, lines, invoice_text=reservation) return result
def post(self, request, *args, **kwargs): booking = Booking.objects.get(pk=request.session['ps_booking']) if 'ps_booking' in request.session else None if request.user.is_anonymous(): form = AnonymousMakeBookingsForm(request.POST) else: form = MakeBookingsForm(request.POST) vehicles = VehicleInfoFormset(request.POST) # re-render the page if there's no booking in the session if not booking: return self.render_page(request, booking, form, vehicles) # re-render the page if the form doesn't validate if (not form.is_valid()) or (not vehicles.is_valid()): return self.render_page(request, booking, form, vehicles, show_errors=True) # update the booking object with information from the form if not booking.details: booking.details = {} booking.details['first_name'] = form.cleaned_data.get('first_name') booking.details['last_name'] = form.cleaned_data.get('last_name') booking.details['phone'] = form.cleaned_data.get('phone') booking.details['country'] = form.cleaned_data.get('country').iso_3166_1_a2 booking.details['postcode'] = form.cleaned_data.get('postcode') booking.details['num_adult'] = form.cleaned_data.get('num_adult') booking.details['num_concession'] = form.cleaned_data.get('num_concession') booking.details['num_child'] = form.cleaned_data.get('num_child') booking.details['num_infant'] = form.cleaned_data.get('num_infant') # update vehicle registrations from form VEHICLE_CHOICES = {'0': 'vehicle', '1': 'concession', '2': 'motorbike'} BookingVehicleRego.objects.filter(booking=booking).delete() for vehicle in vehicles: obj_check = BookingVehicleRego.objects.filter(booking = booking, rego = vehicle.cleaned_data.get('vehicle_rego'), type=VEHICLE_CHOICES[vehicle.cleaned_data.get('vehicle_type')], entry_fee=vehicle.cleaned_data.get('entry_fee')).exists() if(not obj_check): BookingVehicleRego.objects.create( booking=booking, rego=vehicle.cleaned_data.get('vehicle_rego'), type=VEHICLE_CHOICES[vehicle.cleaned_data.get('vehicle_type')], entry_fee=vehicle.cleaned_data.get('entry_fee') ) else: form.add_error(None, 'Duplicate regos not permitted.If unknown add number, e.g. Hire1, Hire2.') return self.render_page(request, booking, form, vehicles,show_errors=True) # Check if number of people is exceeded in any of the campsites for c in booking.campsites.all(): if booking.num_guests > c.campsite.max_people: form.add_error(None, 'Number of people exceeded for the current camp site.') return self.render_page(request, booking, form, vehicles, show_errors=True) # Prevent booking if less than min people if booking.num_guests < c.campsite.min_people: form.add_error('Number of people is less than the minimum allowed for the current campsite.') return self.render_page(request, booking, form, vehicles, show_errors=True) # generate final pricing try: lines = utils.price_or_lineitems(request, booking, booking.campsite_id_list) except Exception as e: form.add_error(None, '{} Please contact Parks and Visitors services with this error message, the campground/campsite and the time of the request.'.format(str(e))) return self.render_page(request, booking, form, vehicles, show_errors=True) print(lines) total = sum([Decimal(p['price_incl_tax'])*p['quantity'] for p in lines]) # get the customer object if request.user.is_anonymous(): # searching on EmailIdentity looks for both EmailUser and Profile objects with the email entered by user customer_qs = EmailIdentity.objects.filter(email__iexact=form.cleaned_data.get('email')) if customer_qs: customer = customer_qs.first().user else: customer = EmailUser.objects.create( email=form.cleaned_data.get('email'), first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), phone_number=form.cleaned_data.get('phone'), mobile_number=form.cleaned_data.get('phone') ) Address.objects.create(line1='address', user=customer, postcode=form.cleaned_data.get('postcode'), country=form.cleaned_data.get('country').iso_3166_1_a2) else: customer = request.user # FIXME: get feedback on whether to overwrite personal info if the EmailUser # already exists # finalise the booking object booking.customer = customer booking.cost_total = total booking.save() # generate invoice reservation = u"Reservation for {} confirmation {}".format(u'{} {}'.format(booking.customer.first_name, booking.customer.last_name), booking.id) logger.info(u'{} built booking {} and handing over to payment gateway'.format(u'User {} with id {}'.format(booking.customer.get_full_name(),booking.customer.id) if booking.customer else u'An anonymous user',booking.id)) result = utils.checkout(request, booking, lines, invoice_text=reservation) return result