class Restaurant(db.Model): __tablename__ = 'restaurants' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) url = db.Column(db.String(100)) neighborhood_id = db.Column(db.Integer, db.ForeignKey("neighborhoods.id")) neighborhood = db.relationship("Neighborhood", back_populates="restaurants") rating = db.Column(db.Integer) category_id = db.Column(db.Integer, db.ForeignKey('categories.id')) category = db.relationship("Category", back_populates="restaurants")
class Table(db.Model): __tablename__ = 'Table' MIN_TABLE_CAPACITY = 1 MAX_TABLE_CAPACITY = 15 id = db.Column(db.Integer, primary_key=True, autoincrement=True) restaurant_id = db.Column( db.Integer, db.ForeignKey('Restaurant.id', ondelete="CASCADE")) restaurant = relationship('Restaurant', back_populates="tables") capacity = db.Column(db.Integer) def serialize(self): return dict([(k, v) for k, v in self.__dict__.items() if k[0] != '_']) def __init__(self, capacity, restaurant): self.set_capacity(capacity) self.restaurant = restaurant def set_capacity(self, capacity): if capacity < self.MIN_TABLE_CAPACITY or capacity > self.MAX_TABLE_CAPACITY: raise ValueError( 'You can\'t set a negative value, zero or greater than ' + str(self.MAX_TABLE_CAPACITY)) self.capacity = capacity def set_restaurant(self, restaurant): self.restaurant = restaurant
class Like(db.Model): __tablename__ = 'Like' liker_id = db.Column(db.Integer, primary_key=True) restaurant_id = db.Column(db.Integer, db.ForeignKey('Restaurant.id', ondelete="CASCADE"), primary_key=True) restaurant = relationship('Restaurant', back_populates='likes') timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, *args, **kw): super(Like, self).__init__(*args, **kw) def serialize(self): return dict([(k, v) for k, v in self.__dict__.items() if k[0] != '_'])
class RestaurantAvailability(db.Model): __tablename__ = 'RestaurantAvailability' MAX_DAY_LENGTH = 10 week_days = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ] id = db.Column(db.Integer, primary_key=True, autoincrement=True) start_time = db.Column(db.Time) end_time = db.Column(db.Time) day = db.Column(db.String(length=MAX_DAY_LENGTH)) restaurant_id = db.Column( db.Integer, db.ForeignKey("Restaurant.id", ondelete="CASCADE")) restaurant = relationship("Restaurant", back_populates="availabilities") def serialize(self): att_dict = dict([(k, v) for k, v in self.__dict__.items() if k[0] != '_']) att_dict['start_time'] = str(self.start_time) att_dict['end_time'] = str(self.end_time) return att_dict def __init__(self, restaurant_id, day, start_time, end_time): self.restaurant_id = restaurant_id self.set_day(day) self.start_time = start_time self.end_time = end_time def set_times(self, start_time, end_time): self.start_time = start_time self.end_time = end_time def set_day(self, day): day = day.title() if day in self.week_days: self.day = day else: raise ('The string must be one of the week days')
class RestaurantRating(db.Model): MIN_VALUE = 0 MAX_VALUE = 10 __tablename__ = 'RestaurantRating' REVIEW_MAX_LENGTH = 200 customer_id = db.Column( db.Integer, primary_key=True ) customer_name = db.Column( db.String( length=REVIEW_MAX_LENGTH, ) ) restaurant_id = db.Column( db.Integer, db.ForeignKey('Restaurant.id'), primary_key=True ) restaurant = relationship('Restaurant', back_populates='ratings') value = db.Column( db.Integer, nullable=False ) review = db.Column( db.String( length=REVIEW_MAX_LENGTH, ), nullable=True ) timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow) def __init__(self, customer_id, restaurant_id, customer_name, value, review=None): self.customer_id = customer_id self.restaurant_id = restaurant_id self.customer_name = customer_name self.value = value[0] if type(value) == tuple else value # for some unknown reason value is a tuple (sometimes) self.review = review @staticmethod def check_value(value: int): if value < RestaurantRating.MIN_VALUE or value > RestaurantRating.MAX_VALUE: raise ValueError('Invalid value for rating!') @staticmethod def check_review(review: str): if len(review) > RestaurantRating.REVIEW_MAX_LENGTH: raise ValueError('Review\'s length must not be greater than MAX_SIZE') def set_value(self, value): RestaurantRating.check_value(value) self.value = value def set_review(self, review): RestaurantRating.check_review(review) self.review = review def get_how_long_ago(self): return timeago.format(datetime.datetime.now(), self.timestamp) def serialize(self): att_dict = dict([(k,v) for k,v in self.__dict__.items() if k[0] != '_']) att_dict['timestamp'] = self.get_how_long_ago() return att_dict