class Restaurant(SearchableMixin, db.Model): __tablename__ = "restaurant" __searchable__ = ["name", "phone", "average_rating"] id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.Text(100)) lat = db.Column(db.Float) # restaurant latitude lon = db.Column(db.Float) # restaurant longitude phone = db.Column(db.Unicode(40)) time_of_stay = db.Column(db.Integer) # minutes cuisine_type = db.Column(db.Enum(CuisineType)) opening_hours = db.Column(db.Integer) closing_hours = db.Column(db.Integer) operator_id = db.Column(db.Integer, db.ForeignKey("operator.id")) average_rating = db.Column(db.Integer, default=0) # precautions = db.relationship("Precaution", secondary=precautions, backref="restaurants") tables = db.relationship("Table", back_populates="restaurant") reviews = db.relationship("Review", back_populates="restaurant") menus = db.relationship("Menu", back_populates="restaurant") def get_bookings(self, starting_booking_datetime: datetime): """Get all the bookings that were confirmed starting from a specific time. Args: starting_booking_time (datetime): the starting time of the booking """ total_real_bookings = [] for table in self.tables: bookings = [ b for b in table.booking if b.checkin and b.start_booking == starting_booking_datetime ] total_real_bookings.extend(bookings) return total_real_bookings def get_free_table(self, seats, date_hour): filtered_tables = [] tables_list = Table.query.filter_by(restaurant_id=self.id).order_by( Table.seats.asc()) for table in tables_list: if table.seats >= seats: filtered_tables.append(table) id_booked_tables = [] for table in filtered_tables: for booking in table.booking: if booking.start_booking == date_hour: id_booked_tables.append(table.id) break for table in filtered_tables: if table.id not in id_booked_tables: return table.id return None
class Food(db.Model): __tablename__ = "food" id = db.Column(db.Integer, primary_key=True, autoincrement=True) category = db.Column(db.Enum(FoodCategory)) name = db.Column(db.Text(100)) price = db.Column(db.Float) menu = db.relationship("Menu", secondary=MenuItems, back_populates="foods")
class Menu(db.Model): __tablename__ = "menu" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.Text(100)) restaurant_id = db.Column(db.Integer, db.ForeignKey("restaurant.id")) restaurant = db.relationship("Restaurant", back_populates="menus") foods = db.relationship("Food", secondary=MenuItems, back_populates="menu")
class Table(db.Model): __tablename__ = "table" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.Text(100)) seats = db.Column(db.Integer) restaurant_id = db.Column(db.Integer, db.ForeignKey("restaurant.id")) restaurant = db.relationship("Restaurant", back_populates="tables") booking = db.relationship("Booking", back_populates="table")
class Precautions(db.Model): __tablename__ = "precautions" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.Text(100))