class User(db.Model): __tablename__ = 'users' chat_id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), nullable=False) email = db.Column(db.String(32), unique=True, nullable=False) dashboards = db.relationship('DashBoard', secondary=dashboard_users, backref=db.backref('users', lazy=True)) tasks = db.relationship('Task', secondary=task_users, backref=db.backref('users', lazy=True)) comments = db.relationship("Comment", backref='author') subscriptions = db.relationship('Event', secondary=user_subscriptions, backref=db.backref("subscribers", lazy=True)) def __repr__(self): return '<User %r>' % self.username def serialize(self) -> dict: return { "chat_id": self.chat_id, "username": self.username, "email": self.email }
class Case(db.Model): __tablename__ = 'cases' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=False) description = db.Column(db.Text) image_id = db.Column(db.Integer, db.ForeignKey('image.id')) cooldown_period = db.Column(db.Integer) image = db.relationship('Image', backref=db.backref('case_image')) items = db.relationship('Item', backref=db.backref('case')) def __str__(self): return self.name def json_with_cooldown(self, user_id): case_cooldown = CaseCooldown.query.filter_by(user_id=user_id).first() if case_cooldown is None: cooldown_left = 0 else: cooldown_left = case_cooldown.cooldown_left return { 'id': self.id, 'name': self.name, 'description': self.description, 'image': self.image.url, 'cooldown_left': cooldown_left }
class CartItem(db.Model): id = db.Column(db.Integer, primary_key=True) order_id = db.Column(db.Integer, db.ForeignKey('order.id'), nullable=False) order = db.relationship('Order', backref=db.backref('order_items', lazy=True)) menu_id = db.Column(db.Integer, db.ForeignKey('menu_item.id'), nullable=False) menu = db.relationship('MenuItem', backref=db.backref('menu_carts', lazy=True)) count = db.Column(db.Integer, nullable=True)
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) login = db.Column(db.String(255), unique=True, nullable=False) # user - vk_id; admin - login _password = db.Column(db.Text, nullable=False) # user - vk_access_token; admin - password email = db.Column(db.String(255), unique=True) active = db.Column(db.Boolean(), nullable=False) roles = db.relationship( 'Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic') ) def __init__(self, login='', password='', active=True, roles=[]): self.login = login self.password = password self.active = active self.roles = roles @hybrid_property def password(self): return self._password @password.setter def password(self, password): self._password = sha256_crypt.hash(password) @hybrid_method def verify_hash(self, password): return sha256_crypt.verify(password, self._password) @staticmethod def find_by_login(login): return User.query.filter_by(login=login).first()
class CaseCooldown(db.Model): __tablename__ = 'case_cooldown' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id', on_delete='CASCADE'), nullable=False) case_id = db.Column(db.Integer, db.ForeignKey('cases.id', on_delete='CASCADE'), nullable=False) cooldown = db.Column(db.DateTime) user = db.relationship('User') case = db.relationship('Case', backref=db.backref('case_cooldowns')) def __init__(self, user_id, case_id): cooldown_period = Case.query.get(case_id).cooldown_period if cooldown_period is None: cooldown_period = 0 self.user_id = user_id self.case_id = case_id self.cooldown = datetime.now() + timedelta(seconds=cooldown_period) @hybrid_property def is_cooled_down(self): return self.cooldown < datetime.now() @hybrid_property def cooldown_left(self): return max(0, (self.cooldown - datetime.now()).total_seconds())
class PayOrder(db.Model): __bind_key__ = 'flask_demo' __tablename__ = 'pay_order' id = db.Column(db.Integer, primary_key=True) project_id = db.Column(db.Integer, db.ForeignKey('pay_project.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) real_fee = db.Column(db.Integer) # 实际支付金额(分) name = db.Column(db.String(64)) address = db.Column(db.String(255)) phone = db.Column(db.String(20)) pay_type = db.Column(db.String(20)) # 微信or支付宝(wechat_pay or ali_pay) create_time = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp()) update_time = db.Column(db.TIMESTAMP, onupdate=datetime.datetime.now) project_obj = db.relationship('PayProject', backref=db.backref('payproject_set')) user_obj = db.relationship('User', backref=db.backref('user_set')) deleted = db.Column(db.Boolean, default=False, nullable=False) # 是否显示
class Table(db.Model): id = db.Column(db.Integer, primary_key=True) restaurant_id = db.Column(db.Integer, db.ForeignKey('restaurant.id'), nullable=False) tables = db.relationship('Restaurant', backref=db.backref('restaurant_tables', lazy=True)) category_id = db.Column(db.Integer, db.ForeignKey('table_category.id'), nullable=False) category = db.relationship('TableCategory', backref=db.backref('table_catgory_tables', lazy=True)) seat_count = db.Column(db.Integer, nullable=True) count = db.Column(db.Integer, nullable=True) def __repr__(self): return '<Table %r>' % self.id
class Order(db.Model): id = db.Column(db.Integer, primary_key=True) user_email = db.Column(db.String(120), nullable=False) mobile = db.Column(db.Integer, nullable=True) restaurant_id = db.Column(db.Integer, db.ForeignKey('restaurant.id'), nullable=False) restaurant = db.relationship('Restaurant', backref=db.backref('restaurant_orders', lazy=True)) order_tables = db.relationship('Table', secondary=order_tables, lazy='subquery', backref=db.backref('Orders', lazy=True)) order_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) spend_hour = db.Column(db.Integer, default=2) paid = db.Column(db.Boolean, default=False) active = db.Column(db.Boolean, default=False) status = db.Column(db.Enum(OrderStatus), default=OrderStatus.open, nullable=False) @property def get_table_price(self): table_price = sum( [table.category.price for table in self.order_tables]) return table_price @property def get_food_price(self): total_food_price = sum([item.menu.price for item in self.order_items]) return total_food_price @property def get_total_amount(self): result = 0 table_price = self.get_table_price get_food_price = self.get_food_price return table_price + get_food_price
class TestUser(db.Model): __bind_key__ = 'flask_demo' __tablename__ = 'test_user' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) is_active = db.Column(db.Boolean, default=True) role_id = db.Column(db.Integer, db.ForeignKey('test_role.id')) role = db.relationship('TestRole', backref=db.backref('testuser_set')) create_time = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp()) up_time = db.Column(db.TIMESTAMP, onupdate=datetime.datetime.now)
class MenuItem(db.Model): id = db.Column(db.Integer, primary_key=True) restaurant_id = db.Column(db.Integer, db.ForeignKey('restaurant.id'), nullable=False) items = db.relationship('Restaurant', backref=db.backref('restaurant_items', lazy=True)) name = db.Column(db.String(50), nullable=False) description = db.Column(db.Text, nullable=True) price = db.Column(db.Float) def __repr__(self): return '<MenuItem %r>' % self.name
class Item(db.Model): __tablename__ = 'items' id = db.Column(db.Integer, primary_key=True, nullable=False) name = db.Column(db.String(255), nullable=False) description = db.Column(db.Text) probability = db.Column(db.Float, nullable=False) expiration_period = db.Column(db.Integer, default=3600) image_id = db.Column(db.Integer, db.ForeignKey('image.id')) image = db.relationship('Image', backref=db.backref('item_image')) case_id = db.Column(db.Integer, db.ForeignKey('cases.id', ondelete='CASCADE')) def __str__(self): return str(self.name) + ', ' + str(self.id)
class Restaurant(db.Model): id = db.Column(db.Integer, primary_key=True) user_email = db.Column(db.String, db.ForeignKey('user.email'), nullable=False) user = db.relationship('User', backref=db.backref('user_restaurants', lazy=True)) category_id = db.Column(db.Integer, db.ForeignKey('restaurant_category.id'), nullable=False) category = db.relationship('RestaurantCategory', backref=db.backref('category_restaurants', lazy=True)) place_id = db.Column(db.Integer, db.ForeignKey('place.id'), nullable=False) place = db.relationship('Place', backref=db.backref('place_restaurants', lazy=True)) title = db.Column(db.String(128)) description = db.Column(db.Text, nullable=True) mobile = db.Column(db.Integer, nullable=True) email = db.Column(db.String(120), nullable=False) opening_time = db.Column(TIME(), nullable=False) closing_time = db.Column(TIME(), nullable=False) active = db.Column(db.Boolean(), default=True) def __repr__(self): return '<Restaurant %r>' % self.title def get_available_tables(self, date_time): restaurant_tables = self.restaurant_tables restaurant_booked_table_id_list = [] end_time = date_time + timedelta(hours=2) query = (db.session.query(Order).filter(Order.order_time >= end_time)) for order in query: restaurant_booked_table_id_list.extend( [table.id for table in order.order_tables]) return [restaurant_tables - restaurant_booked_table_id_list]
class Region(db.Model): __tablename__ = "region" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200), nullable=False) state_id = db.Column(db.Integer, db.ForeignKey("region.id")) # backref creates a new property state which you can call constituencies = relationship("Region", backref=db.backref('state', remote_side=[id])) voteResults = relationship("VoteResult", back_populates="region") def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class User(UserMixin, db.Model): __bind_key__ = 'flask_demo' __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) real_name = db.Column(db.String(64)) nickname = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128), nullable=False) wechat = db.Column(db.String(64)) level = db.Column(db.String(32), server_default='normal') # 默认用户为普通代理(总代理all) expire_time = db.Column(db.DATETIME) # 过期时间,默认可以使用一周 login_count = db.Column(db.Integer, default=0) login_last_time = db.Column(db.DATETIME) login_last_ip = db.Column(db.String(64), server_default='') role_id = db.Column(db.ForeignKey('role.id')) role_obj = db.relationship('Role', backref=db.backref('user_set')) c_time = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp()) up_time = db.Column(db.TIMESTAMP, onupdate=datetime.datetime.now) # @property # def password(self): # raise AttributeError('password is not a readable attribute') # # @password.setter def set_password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): try: return unicode(self.id) # python 2 except NameError: return str(self.id) # python 3
class Event(db.Model): __tablename__ = 'event' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) description = db.Column(db.String(1000)) country = db.Column(db.String(50)) city = db.Column(db.String(50)) locality = db.Column(db.String(50)) url = db.Column(db.String(500)) youtube = db.Column(db.String(50)) dates = db.relationship('Date', secondary=events_dates, order_by=Date.id, lazy='subquery', backref=db.backref('events')) def __repr__(self): return '<Event %r>' % self.name
class Site(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(200), nullable=False) website = db.Column(db.String(260), nullable=False) description = db.Column(db.Text) language = db.Column(db.String(50), default='English') tags = db.relationship('Tag', secondary=tags, backref=db.backref('sites', lazy='dynamic')) source_url = db.Column(db.String(500)) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) submitted_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) # slug = db.Column(db.String(260), unique=True, nullable=False) def __repr__(self): return '<site %r>' % self.website
class Dates(db.Model): id = db.Column(db.Integer,primary_key=True) date = db.Column(db.DateTime) showtime = db.relationship('Movies', secondary=showtime, backref=db.backref('dates', lazy='dynamic')) def __init__(self,date): self.date = date def __repr__(self): return '<Date: %r>' %self.date @property def serialize(self): #d = Dates.query.filter_by(id=i.id).first().showtime return { str(self.date) : [i.title for i in self.showtime] }
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(100), unique=True, nullable=False) email = db.Column(db.String(100), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) time = db.Column(db.Integer, nullable=True, default=0) last_start = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) tasks = db.relationship("Task", secondary=task_user, backref=db.backref("users"), lazy="dynamic") def serialize(self): return { "id": self.id, "username": self.username, "email": self.email, "time": self.time, "tasks": self.tasks }
class Paper(db.Model): __tablename__ = 'paper' id = db.Column(db.Integer, primary_key=True) status = db.Column(db.String(120), nullable=False) title = db.Column(db.String(120), nullable=False) abstract = db.Column(db.Text(500), nullable=False) # relationships authors = db.relationship('User', secondary=authors, lazy='subquery', backref=db.backref('papers', lazy=True)) reviewers = db.relationship("Score", back_populates="paper") scores = db.relationship("Score", back_populates="paper") def has_reviewer(self, user): for reviewer in self.reviewers: if reviewer.reviewer.id == user.id: return True return False def __repr__(self): return '<Paper %r>' % self.title
class Booking(db.Model): id = db.Column(db.Integer, primary_key=True) # user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # date_id = db.Column(db.Integer, db.ForeignKey('dates.id')) user_id = db.Column(db.Integer) date_id = db.Column(db.Integer) movie_booking = db.relationship('Movies', secondary=movie_booking, backref=db.backref('booking', lazy='dynamic')) @property def serialize(self): #d = Dates.query.filter_by(id=i.id).first().showtime return { 'id': self.user_id, 'date': self.date_id, 'Movies': [i.id for i in self.movie_booking] } @property def serialize_user(self): #d = Dates.query.filter_by(id=i.id).first().showtime return {self.date_id: [i.id for i in self.movie_booking]}