-
Notifications
You must be signed in to change notification settings - Fork 0
/
booking.py
58 lines (44 loc) · 1.95 KB
/
booking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from flask import Blueprint, render_template, redirect, flash, url_for, request
from flask_login import current_user, login_user, logout_user
from forms import BookingForm
from app import app
from models import db, User, Booking
from user import *
booking = Blueprint('booking_blueprint',__name__)
def user_has_booking():
user_bookings = Booking.query.filter_by(user_id=current_user.id).count()
if user_bookings > 0:
return True
else:
return False
def get_user_bookings():
user_bookings = Booking.query.filter_by(user_id=current_user.id)
return user_bookings
@booking.route('/bookings', methods=('GET', 'POST'))
def bookings():
if not current_user.is_authenticated:
return redirect(url_for('core.home'))
context = {"user_has_booking":user_has_booking(),"user_bookings":get_user_bookings()}
return render_template('booking/dashboard.html', title='Booking Dashboard',data=context)
@booking.route('/bookings/new', methods=('GET', 'POST'))
def new_booking():
if not current_user.is_authenticated:
return redirect(url_for('core.home'))
form = BookingForm()
if form.validate_on_submit():
print (form.car_id.data)
booking = Booking(user_id=current_user.id,
car_id=form.car_id.data,
start_time=form.start_date_time.data,
end_time=form.end_date_time.data)
booking_validation = booking.is_car_avaiable()
if booking_validation['result'] == False:
flash(booking_validation['reason'])
return render_template('booking/new.html', title='New Booking', form=form)
db.session.add(booking)
db.session.commit()
flash('Thanks for making a booking')
return redirect(url_for('booking_blueprint.bookings'))
return render_template('booking/new.html', title='New Booking',form=form)
if __name__ == '__main__':
app.run()