Exemple #1
0
class TaskModel(orm.Model):
    __tablename__ = "task"

    id = orm.Column(orm.Integer, primary_key=True)
    content = orm.Column(orm.String(120), nullable=False)
    note = orm.Column(orm.String(255))
    priority = orm.Column(orm.Enum(Priority), nullable=False)
    due_date = orm.Column(orm.TIMESTAMP)
    completed = orm.Column(orm.Boolean, nullable=False)

    category_id = orm.Column(orm.Integer, orm.ForeignKey("category.id"), nullable=False)

    @classmethod
    def find_by_id(cls, _id: int) -> "TaskModel":
        return cls.query.filter_by(id=_id).first()

    @classmethod
    def find_all(cls, priority: Priority = None) -> List["TaskModel"]:
        if not priority:
            return cls.query.all()
        else:
            return cls.query.filter_by(priority=priority.value)

    def save_to_db(self) -> None:
        orm.session.add(self)
        orm.session.commit()

    def delete_from_db(self) -> None:
        orm.session.delete(self)
        orm.session.commit()
Exemple #2
0
class UserModel(orm.Model):
    __tablename__ = "user"

    id = orm.Column(orm.Integer, primary_key=True)
    username = orm.Column(orm.String(64), unique=True, nullable=False)
    password = orm.Column(orm.String(94), nullable=False)

    categories = orm.relationship("CategoryModel",
                                  backref="user",
                                  lazy="dynamic")

    @classmethod
    def find_by_username(cls, username: str) -> "UserModel":
        return cls.query.filter_by(username=username).first()

    @classmethod
    def find_by_id(cls, _id: int) -> "UserModel":
        return cls.query.filter_by(id=_id).first()

    def save_to_db(self) -> None:
        orm.session.add(self)
        orm.session.commit()

    def delete_from_db(self) -> None:
        orm.session.delete(self)
        orm.session.commit()
Exemple #3
0
class UserModel(orm.Model):
    __tablename__ = 'users'
    id = orm.Column(orm.Integer, primary_key=True)
    username = orm.Column(orm.String(80))
    password = orm.Column(orm.String(80))

    def __init__(self, username: str, password: str):
        self.username = username
        self.password = password

    def json(self) -> Dict:
        return {'id': self.id, 'username': self.username}

    @classmethod
    def find_user_by_username(cls, username):
        return cls.query.filter_by(username=username).first()

    @classmethod
    def find_user_by_id(cls, _id):
        return cls.query.filter_by(id=_id).first()

    def save_user_to_db(self) -> None:
        orm.session.add(self)
        orm.session.commit()

    def delete_user_in_db(self) -> None:
        orm.session.delete(self)
        orm.session.commit()
Exemple #4
0
class CategoryModel(orm.Model):
    __tablename__ = "category"

    id = orm.Column(orm.Integer, primary_key=True)
    title = orm.Column(orm.String(40), nullable=False)
    accent = orm.Column(orm.Enum(Accent),
                        nullable=False,
                        server_default=("BLUE"))

    tasks = orm.relationship("TaskModel", backref="category", lazy="dynamic")

    user_id = orm.Column(orm.Integer,
                         orm.ForeignKey("user.id"),
                         nullable=False)

    @classmethod
    def find_by_id(cls, _id: int) -> "CategoryModel":
        return cls.query.filter_by(id=_id).first()

    @classmethod
    def find_by_user_id_and_title(cls, user_id: int,
                                  title: str) -> "CategoryModel":
        return cls.query.filter_by(user_id=user_id, title=title).first()

    @classmethod
    def find_all(cls) -> List["CategoryModel"]:
        return cls.query.all()

    def save_to_db(self) -> None:
        orm.session.add(self)
        orm.session.commit()

    def delete_from_db(self) -> None:
        orm.session.delete(self)
        orm.session.commit()
Exemple #5
0
class StoreModel(orm.Model):
    __tablename__ = 'stores'
    id = orm.Column(orm.Integer, primary_key=True)
    name = orm.Column(orm.String(80))
    items = orm.relationship('ItemModel', lazy="dynamic")

    def __init__(self, name: str):
        self.name = name

    def json(self) -> StoreJson:
        return {
            'name': self.name,
            'items': [item.json() for item in self.items.all()]
        }

    @classmethod  # notice that this cannot be changed to class function/method but should be @classmethod
    def find_store_by_name(cls, name: str) -> "StoreModel":
        return cls.query.filter_by(name=name).first()

    def save_store_to_db(self) -> None:
        orm.session.add(self)
        orm.session.commit()

    def delete_store_in_db(self) -> None:
        orm.session.delete(self)
        orm.session.commit()

    @classmethod
    def get_stores_from_db(cls) -> List["StoreModel"]:
        return cls.query.all()
Exemple #6
0
class ItemModel(orm.Model):
    __tablename__ = 'items'
    id = orm.Column(orm.Integer, primary_key=True)
    name = orm.Column(orm.String(80))
    price = orm.Column(orm.Float(precision=2))

    store = orm.relationship('StoreModel')
    store_id = orm.Column(orm.Integer, orm.ForeignKey('stores.id'))

    def __init__(self, name: str, price: float, store_id: int):
        self.name = name
        self.price = price
        self.store_id = store_id

    def json(self) -> ItemJson:
        return {
            'id': self.id,
            'name': self.name,
            'price': self.price,
            'store_id': self.store_id
        }

    @classmethod  # notice that this cannot be changed to class function/method but should be @classmethod
    def find_item_by_name(cls, name: str) -> "ItemModel":
        return cls.query.filter_by(name=name).first()

    def save_item_to_db(self) -> None:
        orm.session.add(self)
        orm.session.commit()

    def delete_item_in_db(self) -> None:
        orm.session.delete(self)
        orm.session.commit()

    @classmethod
    def get_items_from_db(cls) -> List["ItemModel"]:
        return cls.query.all()