def BookMachineTmr(request): #this function will only be called when we book machines for tomorrow this is a POST tmr_date=datetime.datetime.today()+timedelta(days=1) tomorrow=tmr_date.strftime("%Y-%m-%d") sfu_id=request.user.username if request.POST: #'id_machine_name' is passed via the form new_booking_tmr = Bookings(machine_name='id_machine_name',booked_date=tomorrow, username=sfu_id) form = AddBookingFormTomorrow(request.POST, instance=new_booking_tmr)#try to add the bookings for today booked_time="" if form.is_valid(): data = form.cleaned_data if(data['machine_name_tmr']=='rowing'): booked_time=data['rowing_time_tmr'] elif(data['machine_name_tmr']=='treadmill'): booked_time=data['treadmill_time_tmr'] elif(data['machine_name_tmr']=='stairs'): booked_time=data['stairs_time_tmr'] new_booking_tmr.booked_time=booked_time new_booking_tmr.machine_name=data['machine_name_tmr'] #check that user hasnt previoulsy booked that machine booked_check=Bookings.objects.filter(machine_name=data['machine_name_tmr'],booked_date=tomorrow,username=sfu_id,booked_time=booked_time) if(booked_check.count()>0): return HttpResponseRedirect('/swt/bookings/') else: form.save() return HttpResponseRedirect('/swt/bookings/') else: return HttpResponseRedirect('/swt/bookings')
def checkout(request): new_booking = Bookings( bookingType=request.session['type'], seatsChosen=request.session['seatsChosen'], bookingPayment=request.session['amount'], user=request.user, ) trip = Trip(destination=request.session['destination'], duration=request.session['duration'], price=request.session['price'], hotelName=request.session['hotelName'], totalCost=request.session['amount']) if request.method == "POST": token = request.POST.get("stripeToken") try: charge = stripe.Charge.create(amount=request.session['amount'], currency="gbp", source=token, description="booking successfully made") new_booking.charge_id = charge.id except stripe.error.CardError as ce: return False, ce else: new_booking.save() and trip.save() send_mail( 'Walton Happy Travels Package Booking', 'Thank you ' + request.user.email + ' for Booking with Walton Happy Travels. Please find below the ' + 'details of your Booking' + "\n" + 'Details:' + "\n" + 'destination: ' + str(request.session['destination']) + "\n" + 'duration: ' + str(request.session['duration']) + ' Days' + "\n" + 'Departure Date: ' + str(request.session['departureDate']) + "\n" + 'Hotel Name: ' + str(request.session['hotelName']) + "\n" + 'Number of People: ' + str(request.session['seatsChosen']) + "\n" + 'Total Cost: £' + request.session['amount'][:-2], '*****@*****.**', ['*****@*****.**'], fail_silently=False) return redirect("home")
def BookMachine(request): #query everything that it is available to use today during the whole day until gym closes. assume it clos$ #everything is done ine simple 24 hours format so 1 is 1 am 13 is 1 pm ... #select current date #year month day curr_date=datetime.datetime.now() today=curr_date.strftime("%Y-%m-%d") tmr_date=datetime.datetime.today()+timedelta(days=1) tomorrow=tmr_date.strftime("%Y-%m-%d") time=strftime("%H") sfu_id=request.user.username multi_book="" if request.POST: #'id_machine_name' is passed via the form new_booking = Bookings(machine_name='id_machine_name',booked_date=today, username=sfu_id) form = AddBookingForm(request.POST, instance=new_booking)#try to add the bookings for today booked_time="" if form.is_valid(): data = form.cleaned_data if(data['machine_name']=='rowing'): booked_time=data['rowing_time'] elif(data['machine_name']=='treadmill'): booked_time=data['treadmill_time'] elif(data['machine_name']=='stairs'): booked_time=data['stairs_time'] new_booking.booked_time=booked_time #check that user hasnt previoulsy booked that machine booked_check=Bookings.objects.filter(machine_name=data['machine_name'],booked_date=today,username=sfu_id,booked_time=booked_time) if(booked_check.count()>0): return HttpResponseRedirect('/swt/bookings/') else: form.save() return HttpResponseRedirect('/swt/bookings/') else: form_today= AddBookingForm() form_tomorrow=AddBookingFormTomorrow() args={} args.update(csrf(request)) #query database see what the user has booked for today my_bookings= Bookings.objects.filter(booked_date=today,username=sfu_id).order_by('booked_time') #assume that there are 2 machines of each type at the gym and we have to calculate how many machines are booked for each time fully_booked_treadmill=Bookings.fully_booked_arr(today,'treadmill') fully_booked_rowing=Bookings.fully_booked_arr(today,'rowing') fully_booked_stairs=Bookings.fully_booked_arr(today,'stairs') #now do the same types of query's but for tomorrow's date my_bookings_tmr=Bookings.objects.filter(booked_date=tomorrow,username=sfu_id).order_by('booked_time') #assume there are 2 machines of each time, if wished change number in the model function fully_booked_treadmill_tmr=Bookings.fully_booked_arr(tomorrow,'treadmill') fully_booked_rowing_tmr=Bookings.fully_booked_arr(tomorrow,'rowing') fully_booked_stairs_tmr=Bookings.fully_booked_arr(tomorrow,'stairs') #we may not need THIS booked_times_treadmill = [] booked_times_stairs = [] booked_times_rowing = [] # The Default Index to start with in Booking Time Dropdown default_t_index = 6 default_s_index = 6 default_r_index = 6 #tomorrows indexes default_t_index_tmr=6 default_s_index_tmr=6 default_r_index_tmr=6 #calculatin the indexes for today default_t_index=default_t_index + Bookings.get_delta_index(fully_booked_treadmill) default_s_index=default_s_index + Bookings.get_delta_index(fully_booked_stairs) default_r_index=default_r_index + Bookings.get_delta_index(fully_booked_rowing) #calculatin the index for tomorrow default_t_index_tmr=default_t_index_tmr+ Bookings.get_delta_index(fully_booked_treadmill_tmr) default_s_index_tmr=default_s_index_tmr+ Bookings.get_delta_index(fully_booked_stairs_tmr) default_r_index_tmr=default_r_index_tmr+ Bookings.get_delta_index(fully_booked_rowing_tmr) args['form_today'] = form_today args['booked_times_treadmill'] = booked_times_treadmill args['booked_times_stairs'] = booked_times_stairs args['booked_times_rowing'] = booked_times_rowing args['default_t_index'] = default_t_index args['default_s_index'] = default_s_index args['default_r_index'] = default_r_index args['fully_booked_treadmill']=fully_booked_treadmill args['fully_booked_stairs']=fully_booked_stairs args['fully_booked_rowing']=fully_booked_rowing args['my_bookings']=my_bookings args['time']=int(time) #tomorrow variables and related args['tomorrow']=tomorrow args['form_tomorrow']=form_tomorrow args['my_bookings_tmr']=my_bookings_tmr return render_to_response('bookmachine.html',args)
def checkout( request, show_id, ): show = get_object_or_404(Shows, pk=show_id) bookings = Bookings.objects.all() seatId = request.session.get('seatId') sId = (seatId['seatId']) seatName = (seatId['seatName']) #converting strings into array sIdx = sId.split(',') # removing last empty item in the array sIdx = sIdx[:-1] #converting array into string # sIdx = ''.join(sId) f_seats = Seats.objects.filter(id__in=sIdx) price = Seats.objects.filter(id__in=sIdx).aggregate( Sum('price'))['price__sum'] tickets = len(sIdx) eventId = Shows.objects.values('event_id').filter(id=show_id) showId = Shows.objects.values('id').filter(id=show_id) context = { 'f_seats': f_seats, 'price': price, 'show': show, } if request.method == 'POST': seats = Seats.objects.filter(id__in=sIdx) seats.update(is_booked=True) c_name = request.POST['name'] c_email = request.POST['email'] c_phone = request.POST['phoneNumber'] eventId = eventId showId = showId seatsId = sId seatsName = seatName seatsPrice = price if bookings.filter(booked_seats=seatsId).exists(): messages.error(request, 'Selected seat already booked, Please try again !') return redirect('home') else: #submitting form to the database bookings = Bookings(c_name=c_name, c_email=c_email, c_phone=c_phone, event_id=eventId, show_id=showId, booked_seats=seatsId, booked_seats_name=seatsName, booked_price=seatsPrice, booked_seats_count=tickets) b_seats = Bookings.objects.filter(booked_seats=sId) print(b_seats) context = { 'f_seats': f_seats, 'price': price, 'tickets': tickets, 'b_seats': b_seats, 'show': show, 'c_name': c_name, 'c_email': c_email, 'c_phone': c_phone, 'booked_seats': seatsId, } bookings.save() return render(request, 'events/booking_confirmation.html', context) return render(request, 'events/checkout.html', context)