class UserModel(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80)) password = db.Column(db.String(80)) # parser = reqparse.RequestParser() # parser.add_argument( # "username", type=str, required=True, help="This field cannot be left blank!" # ) def __init__(self, username, password): self.username = username self.password = password @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id) def save_to_db(self): db.session.add(self) db.session.commit()
class User(db.Model): __tablename__ = "user" id = db.Column(db.String, primary_key=True, default=uuid.uuid4()) username = db.Column(db.String(64), index=True, unique=True, nullable=True) email = db.Column(db.String(120), index=True, unique=True) password = db.Column(db.String(128)) def __repr__(self): return f'<User {self.username}>' def __init__(self, email, password, username): self.username = username self.email = email self.password = bcrypt.generate_password_hash(password).decode() def password_is_valid(self, password): return bcrypt.check_password_hash(self.password, password) def save(self): db.session.add(self) db.session.commit() @staticmethod def find_all(): return User.query.order_by(User.email).all() @staticmethod def find_by_email(email: str): return User.query.filter_by(email=email).first()
class ToDo(db.Model): id = db.Column(db.Integer, nullable=False, primary_key=True) task = db.Column(db.String(16), nullable=False, unique=False) description = db.Column(db.String(20), nullable=True, unique=False) activate = db.Column(db.Boolean, nullable=False, unique=False, default=True, server_default="true") def __init__(self, task, description): super().__init__() self.task = task self.description = description
class ItemModel(db.Model): __tablename__ = "items" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) price = db.Column(db.Float(precision=2)) store_id = db.Column(db.Integer, db.ForeignKey('stores.id')) store = db.relationship('StoreModel') def __init__(self, name, price, store_id): self.name = name self.price = price self.store_id = store_id def json(self): return {"name": self.name, "price": self.price} @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class StoreModel(db.Model): __tablename__ = "stores" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) items = db.relationship('ItemModel', lazy='dynamic') def __init__(self, name): self.name = name def json(self): return { 'name': self.name, 'items': [item.json() for item in self.items.all()] } @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class CommunicationGateway(db.Model): id = db.Column(db.Integer, nullable=False, primary_key=True) target_user_id = db.Column(db.Integer, nullable=False) params_hash = db.Column(JSON, nullable=False) type = db.Column(db.String(10), nullable=False) endpoint_type = db.Column(db.String(30), nullable=True) request_status = db.Column(db.String(20), nullable=False) def __init__(self, target_user_id, params_hash, type, request_status, endpoint_type=None): self.target_user_id = target_user_id self.params_hash = params_hash self.type = type self.request_status = request_status self.endpoint_type = endpoint_type
class Post(db.Model): __tablename__ = 'post' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50), nullable=False) content = db.Column(db.Text(), nullable=False) def __repr__(self): return f'Post(id={self.id}, title={self.title!r}, content={self.content!r})' def __str__(self): return f'Post {self.title}'
class Post(db.Model): __tablename__ = 'post' title = db.Column(db.Integer, primary_key=True) tile = db.Column(db.String(50), nullable=False) content = db.Column(db.Text, nullable=False)