-
Notifications
You must be signed in to change notification settings - Fork 10
/
model.py
64 lines (51 loc) · 2.26 KB
/
model.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
from flask import Flask
from marshmallow import Schema, fields, pre_load, validate
from flask_marshmallow import Marshmallow
from flask_sqlalchemy import SQLAlchemy
from flask_redis import FlaskRedis
ma = Marshmallow()
db = SQLAlchemy()
redis_cache = FlaskRedis()
class FoodModel(db.Model):
__tablename__ = 'foods'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(250), nullable=False)
description = db.Column(db.String(250))
creation_date = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp(), nullable=False)
restaurant_id = db.Column(db.Integer, db.ForeignKey('restaurants.id', ondelete='CASCADE'), nullable=False)
restaurant = db.relationship('RestaurantModel', backref=db.backref('foods', lazy='dynamic' ))
menu_id = db.Column(db.Integer, db.ForeignKey('menus.id', ondelete='CASCADE'), nullable=False)
menu = db.relationship('MenuModel')
def __init__(self, name, description, restaurant_id, menu_id):
self.name = name
self.description = description
self.restaurant_id = restaurant_id
self.menu_id = menu_id
class MenuModel(db.Model):
__tablename__ = 'menus'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(250), nullable=False)
restaurant_id = db.Column(db.Integer, db.ForeignKey('restaurants.id', ondelete='CASCADE'), nullable=False)
restaurant = db.relationship('RestaurantModel')
def __init__(self, name, restaurant_id):
self.name = name
self.restaurant_id = restaurant_id
class RestaurantModel(db.Model):
__tablename__ = 'restaurants'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150), unique=True, nullable=False)
def __init__(self, name):
self.name = name
class RestaurantSchema(ma.Schema):
id = fields.Integer()
name = fields.String(required=True)
class MenuSchema(ma.Schema):
id = fields.Integer()
restaurant_id = fields.Integer(required=True)
name = fields.String(required=True)
class FoodSchema(ma.Schema):
id = fields.Integer(dump_only=True)
restaurant_id = fields.Integer(required=True)
name = fields.String(required=True, validate=validate.Length(1))
description = fields.String()
creation_date = fields.DateTime()