I've created a small booking application where the client can book for a service, i've also made a form search where the client can search in a specific date to book.
The results should be all the available users except if one of them is busy in the given date by the client.
here is my models.py:
class User(db.Model, UserMixin):
__tablename__ = 'user'
id = db.Column(db.Integer(), primary_key=True)
public_id = db.Column(db.String(50), default=uuid.uuid4)
name = db.Column(db.String())
appointments = db.relationship('Appointment', backref='user', passive_deletes=True, lazy='dynamic')
class Appointment(db.Model):
id = db.Column(db.Integer(), primary_key=True)
public_id = db.Column(db.String(50), default=uuid.uuid4)
start_time = db.Column(db.DateTime(), default=datetime.utcnow)
end_time = db.Column(db.DateTime(), default=datetime.utcnow)
user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
Here is my code:
from datetime import timedelta
from dateutil.parser import parse
date_time = parse(request.form.get('datetime'))
end_time = date_time + timedelta(minutes=20)
overlapping = db.session.query(Appointment).filter(
not_(
or_(Appointment.start_time > end_time,
Appointment.end_time < date_time)))
overlapping.count() # if overlapping returns > 0 so the client can't book
# Here i am joining the two tables so i can get the available users on that date
results = User.query.join(Appointment, Appointment.user_id == User.id).filter(
Appointment.start_time > end_time).filter(
Appointment.end_time < date_time).all()
In the results i get nothing .
Please any suggestions guys !!!