class Category(db.Model): __tablename__ = 'categories' id = db.Column(db.Integer, primary_key=True) type = db.Column(db.String, nullable=False) questions = db.relationship('Question', backref='category') def __init__(self, type): self.type = type def format(self): return {'id': self.id, 'type': self.type}
class Session(db.Model): id = db.Column(db.Integer, primary_key=True) key = db.Column(db.String(32), nullable=False, unique=True) created_at = db.Column(db.DateTime, default=db.func.now()) games = db.relationship('Game', back_populates='session') @staticmethod def create_session(): new_session = Session(key=uuid4().hex) db.session.add(new_session) db.session.commit() return new_session
class Brand(db.Model): __tablename__ = "brands" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), nullable=False) description = db.Column(db.String(256), nullable=False) cnpj = db.Column(db.String(20), nullable=False) products = db.relationship("Product", backref="brands", lazy=True) def __init__(self, name, description, cnpj): self.name = name self.description = description self.cnpj = cnpj def __repr__(self): return "Brand({})".format(self.id)
class Order(db.Model): __tablename__ = "orders" id = db.Column(db.Integer, primary_key=True, autoincrement=True) total_price = db.Column(db.Float, nullable=False) sale_date = db.Column(db.Date, nullable=False) number_of_items = db.Column(db.Integer, nullable=False) status = db.Column(db.String(64), nullable=False) delivery_date = db.Column(db.Date, nullable=False) client_id = db.Column(db.Integer, db.ForeignKey("clients.id"), nullable=False) order_items = db.relationship("Order_Item", backref="orders", lazy=True) def __init__(self, total_price, sale_date, number_of_items, status, delivery_date, client_id): self.total_price = total_price self.sale_date = sale_date self.number_of_items = number_of_items self.status = status self.delivery_date = delivery_date self.client_id = client_id def __repr__(self): return "Order(id: {})".format(self.id)
class Game(db.Model): id = db.Column(db.Integer, primary_key=True) session_id = db.Column(db.Integer, db.ForeignKey('session.id')) board1 = db.Column(db.Text) board2 = db.Column(db.Text) winner = db.Column(db.Enum(Player), nullable=True) move = db.Column(db.Enum(Player), nullable=False) created_at = db.Column(db.DateTime, default=db.func.now()) finished_at = db.Column(db.DateTime) session = db.relationship('Session', back_populates='games') moves = db.relationship('Move', back_populates='game')
class Lot(db.Model): __tablename__ = "lots" id = db.Column(db.Integer, primary_key=True) product_id = db.Column(db.Integer, db.ForeignKey("products.id"), nullable=False) purchase_price = db.Column(db.Float, nullable=False) validity_date = db.Column(db.Date, nullable=False) lot_quantity = db.Column(db.Integer, nullable=False) def __init__(self, product_id, purchase_price, validity_date, lot_quantity): self.product_id = product_id self.purchase_price = purchase_price self.validity_date = validity_date self.lot_quantity = lot_quantity def __repr__(self): return "Lot({})".format(self.id)
class Move(db.Model): id = db.Column(db.Integer, primary_key=True) game_id = db.Column(db.Integer, db.ForeignKey('game.id')) created_at = db.Column(db.DateTime, default=db.func.now()) player = db.Column(db.Enum(Player), nullable=False) move_x = db.Column(db.Integer) move_y = db.Column(db.Integer) score = db.Column(db.Integer) game = db.relationship('Game', back_populates='moves')
class Order_Item(db.Model): __tablename__ = "order_items" id = db.Column(db.Integer, primary_key=True, autoincrement=True) quantity = db.Column(db.Integer, nullable=False) order_price = db.Column(db.Float, nullable=False) product_id = db.Column(db.Integer, db.ForeignKey("products.id"), nullable=False) order_id = db.Column(db.Integer, db.ForeignKey("orders.id"), nullable=False) def __init__(self, order_id, product_id, quantity, order_price): self.order_id = order_id self.product_id = product_id self.quantity = quantity self.order_price = order_price def __repr__(self): return "Order_Item(id: {})".format(self.id)
class Question(db.Model): __tablename__ = 'questions' id = db.Column(db.Integer, primary_key=True) question = db.Column(db.String, nullable=False) answer = db.Column(db.String, nullable=False) category_id = db.Column(db.Integer, db.ForeignKey('categories.id'), nullable=False) difficulty = db.Column(db.Integer, nullable=False) def __init__(self, question, answer, category_id, difficulty): self.question = question self.answer = answer self.category_id = category_id self.difficulty = difficulty def insert(self): db.session.add(self) db.session.commit() def update(self): db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def format(self): return { 'id': self.id, 'question': self.question, 'answer': self.answer, 'category_id': self.category_id, 'difficulty': self.difficulty }
class Product(db.Model): __tablename__ = "products" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), nullable=False) description = db.Column(db.String(256), nullable=False) sale_price = db.Column(db.Float, nullable=False) total_quantity = db.Column(db.Integer, nullable=False) actived = db.Column(db.SmallInteger, nullable=False) min_quantity = db.Column(db.Integer) brand_id = db.Column(db.Integer, db.ForeignKey("brands.id"), nullable=False) orders = db.relationship("Order_Item", backref="products", lazy=True) lots = db.relationship("Lot", backref="products", lazy=True) def __init__( self, name, description, sale_price, total_quantity, actived, brand_id, min_quantity, ): self.name = name self.description = description self.sale_price = sale_price self.total_quantity = total_quantity self.actived = actived self.brand_id = brand_id self.min_quantity = min_quantity def __repr__(self): return "Product({})".format(self.name)
class Client(db.Model): __tablename__ = "clients" id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(128), nullable=False) full_name = db.Column(db.String(256), nullable=False) cnpj = db.Column(db.String(32), nullable=False) address = db.Column(db.String(256), nullable=False) email = db.Column(db.String(128), nullable=False) phone = db.Column(db.String(32), nullable=False) created_at = db.Column(db.Date, nullable=False) orders = db.relationship("Order", backref="clients", lazy=True) def __init__(self, first_name, full_name, cnpj, address, email, phone): self.first_name = first_name self.full_name = full_name self.cnpj = cnpj self.address = address self.email = email self.phone = phone self.created_at = date.today() def __repr__(self): return "Client({})".format(self.first_name)
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, nullable=False, primary_key=True) name = db.Column(db.Text, nullable=False) email = db.Column(db.Text, nullable=False)
class ToDo(db.Model): __tablename__ = 'todo' id = db.Column(db.Integer, nullable=False, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) title = db.Column(db.Text, nullable=False) description = db.Column(db.Text, nullable=False)