class Product(db.Model): __tablename__ = "product" __searchable__ = ["name"] id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String()) price = db.Column(db.Float()) returned = db.Column(db.Boolean(), default=False) receipt_id = db.Column(db.Integer(), db.ForeignKey("receipt.id"), nullable=False) receipt = db.relationship("Receipt", back_populates="products") color_id = db.Column(db.Integer(), db.ForeignKey("color.id")) color = db.relationship("Color") tags = db.relationship("Tag", secondary=product_tags, lazy="subquery") images = db.relationship("Image", secondary=product_images, lazy="subquery") def __init__(self, *args, **kwargs): self.color = Color.query.order_by(func.random()).first() super().__init__(*args, **kwargs) def to_json(self): return { "id": self.id, "name": self.name, "price": self.price, "returned": self.returned, "color": self.color.to_json(), "tags": [tag.to_json() for tag in self.tags], "images": [image.to_json() for image in self.images] }
class Role(db.Model): __tablename__ = "role" id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(20), unique=True) def __repr__(self): return f"Role('{self.id}', '{self.name}')"
class Image(db.Model): __tablename__ = "image" id = db.Column(db.Integer(), primary_key=True) filename = db.Column(db.String()) def __init__(self, image, *args, **kwargs): img = _Image.open(image) location = os.path.join(current_app.root_path, current_app.config["UPLOAD_IMG_PATH"]) # Creating the directories if needed if (not os.path.exists(location)): os.makedirs(location) # Generate unique filename filename = token_hex(8) + ".png" while (os.path.isfile(os.path.join(location, filename))): filename = secrets.token_hex(8) + ".png" # Save the image img.save(os.path.join(location, filename)) self.filename = filename super().__init__(*args, **kwargs) def to_json(self): return {"id": self.id, "filename": self.filename, "path": self.path} @property def path(self): return os.path.join(current_app.root_path, current_app.config["UPLOAD_IMG_PATH"], self.filename)
class Tag(db.Model): __tablename__ = "tag" __searchable__ = ["name"] id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String()) def to_json(self): return {"id": self.id, "name": self.name}
class Store(db.Model): __tablename__ = "store" __searchable__ = ["name"] id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String()) receipts = db.relationship("Receipt", back_populates="store") def to_json(self): return {"id": self.id, "name": self.name}
class Color(db.Model): __tablename__ = "color" id = db.Column(db.Integer(), primary_key=True) value = db.Column(db.String(6), unique=True) def to_json(self): return { "id": self.id, "value": self.value }
class Receipt(db.Model): __tablename__ = "receipt" __searchable__ = ["receipt_number"] id = db.Column(db.Integer(), primary_key=True) receipt_number = db.Column(db.String()) date = db.Column(db.Date()) payed = db.Column(db.Date()) store_id = db.Column(db.Integer(), db.ForeignKey("store.id")) store = db.relationship("Store", back_populates="receipts") color_id = db.Column(db.Integer(), db.ForeignKey("color.id")) color = db.relationship("Color") tags = db.relationship("Tag", secondary=receipt_tags, lazy="subquery") images = db.relationship("Image", secondary=receipt_images, lazy="subquery") products = db.relationship("Product", back_populates="receipt", cascade="all, delete, delete-orphan") def __init__(self, *args, **kwargs): self.color = Color.query.order_by(func.random()).first() super().__init__(*args, **kwargs) @hybrid_property def sum(self): return sum([product.price for product in self.products]) @sum.expression def sum(cls): return select([func.sum(Product.price)]).where(Product.receipt_id == cls.id).as_scalar() def to_json(self): return { "id": self.id, "receipt_number": self.receipt_number, "date": self.date, "payed": self.payed, "store": self.store.to_json(), "color": self.color.to_json(), "tags": [tag.to_json() for tag in self.tags], "images": [image.to_json() for image in self.images] }
from sqlalchemy import Column, Integer, String, Float, ForeignKey, func, select from sqlalchemy.orm import relationship from flask_login import UserMixin from receipts_storage.app import db, login from .role import Role user_roles = db.Table("user_roles", db.Column("user_id", db.Integer(), db.ForeignKey("person.id", ondelete="CASCADE")), db.Column("role_id", db.Integer(), db.ForeignKey("role.id", ondelete="CASCADE")) ) @login.user_loader def load_user(user_id): return User.query.get(int(user_id)) class User(UserMixin, db.Model): __tablename__ = "person" id = Column(Integer, primary_key=True) username = Column(String, unique=True, nullable=False) firstname = Column(String, nullable=False) lastname = Column(String, nullable=False) password = Column(String, nullable=False) roles = db.relationship("Role", secondary=user_roles, backref="users") def has_roles(self, *requirements): role_names = [role.name for role in self.roles] for requirement in requirements: if isinstance(requirement, (list, tuple)): # this is a tuple_of_role_names requirement tuple_of_role_names = requirement
from datetime import date from sqlalchemy import select from sqlalchemy.sql import func from sqlalchemy.ext.hybrid import hybrid_property from receipts_storage.app import db from ..models import Product, Color receipt_tags = db.Table("receipt_tags", db.Column("receipt_id", db.Integer(), db.ForeignKey("receipt.id", ondelete="CASCADE"), primary_key=True), db.Column("tag_id", db.Integer(), db.ForeignKey("tag.id", ondelete="CASCADE"), primary_key=True) ) receipt_images = db.Table("receipt_images", db.Column("receipt_id", db.Integer(), db.ForeignKey("receipt.id", ondelete="CASCADE"), primary_key=True), db.Column("image_id", db.Integer(), db.ForeignKey("image.id", ondelete="CASCADE"), primary_key=True) ) class Receipt(db.Model): __tablename__ = "receipt" __searchable__ = ["receipt_number"] id = db.Column(db.Integer(), primary_key=True) receipt_number = db.Column(db.String()) date = db.Column(db.Date()) payed = db.Column(db.Date()) store_id = db.Column(db.Integer(), db.ForeignKey("store.id")) store = db.relationship("Store", back_populates="receipts") color_id = db.Column(db.Integer(), db.ForeignKey("color.id")) color = db.relationship("Color") tags = db.relationship("Tag", secondary=receipt_tags, lazy="subquery") images = db.relationship("Image", secondary=receipt_images, lazy="subquery")