-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
97 lines (76 loc) · 4.07 KB
/
models.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy_utils import PasswordType, EmailType
from sqlalchemy import ForeignKey
from flask_login import UserMixin
from app import LoginManager
from flask_migrate import Migrate
import datetime
from datetime import timedelta
from app import app
from app import login_manager
db = SQLAlchemy(app)
migrate = Migrate(app, db)
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
first_name = db.Column(db.String(80), nullable=True)
last_name = db.Column(db.String(80), nullable=True)
email_notifications = db.Column(db.Boolean, default=True)
text_notifications = db.Column(db.Boolean, default=True)
date_enrolled = db.Column(db.DateTime, default=datetime.datetime.now)
password = db.Column(PasswordType(
schemes=[
'pbkdf2_sha512',
'md5_crypt'
],
deprecated=['md5_crypt']
))
email = db.Column(EmailType, unique=True, nullable=False)
mobile = db.Column(db.String(80), nullable=True)
admin = db.Column(db.Boolean, default=False)
profile_complete = db.Column(db.Boolean, default=False)
bookings = db.relationship('Booking', backref='user')
def __repr__(self):
return '<User %r>' % self.username
class Car(db.Model):
id = db.Column(db.Integer, primary_key=True)
vin = db.Column(db.String(80), unique=True, nullable=False)
registration = db.Column(db.String(80), unique=True, nullable=False)
make = db.Column(db.String(80), unique=True, nullable=False)
model = db.Column(db.String(80), unique=True, nullable=False)
bookings = db.relationship('Booking', backref='car')
def __repr__(self):
return '<Subject %r>' % self.vin
class Booking(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
car_id = db.Column(db.Integer, db.ForeignKey('car.id'))
start_time = db.Column(db.DateTime)
end_time = db.Column(db.DateTime)
def __repr__(self):
return '<Lesson %r>' % self.id
def is_car_avaiable(self):
within_another_bookings_timeslot = db.session.query(Booking).filter(Booking.car_id == self.car_id,
Booking.start_time <= self.start_time,Booking.end_time >= self.end_time).count()
user_same_time_slot = db.session.query(Booking).filter(Booking.user_id == self.user_id,
Booking.start_time <= self.start_time,
Booking.end_time >= self.end_time).count()
booking_within_this_range = db.session.query(Booking).filter(Booking.car_id == self.car_id,
Booking.start_time >= self.start_time,Booking.end_time <= self.end_time).count()
starts_two_hours_before_end_start = db.session.query(Booking).filter(Booking.car_id == self.car_id,
Booking.end_time >= self.start_time - timedelta(hours=2),Booking.end_time <= self.start_time).count()
user_booking_count = db.session.query(Booking).filter(Booking.user_id == self.user_id).count()
if user_booking_count > 5:
return {"result": False,"reason":"You can not have more than 5 bookings"}
if user_same_time_slot > 0:
return {"result": False,"reason":" You already have a car already booked for these times."}
if booking_within_this_range > 0:
return {"result": False, "reason": " This car has already been booked between these times"}
if within_another_bookings_timeslot > 0:
return {"result": False, "reason": "This car has already been booked between these times"}
if starts_two_hours_before_end_start > 0:
return {"result": False, "reason": "Atleast 2 hours needed between bookings"}
return {"result": True, "reason": ""}
@login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id)