From 466efcf9e708a7da74701825d83731452b5ae829 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 18 Mar 2021 11:52:51 +0100 Subject: [PATCH] [System] New annotation format for future compatibility --- flaschengeist/models/session.py | 2 ++ flaschengeist/models/setting.py | 2 ++ flaschengeist/models/user.py | 11 +++++++---- flaschengeist/plugins/balance/models.py | 3 ++- flaschengeist/plugins/pricelist/models.py | 12 ++++++++---- flaschengeist/plugins/schedule/models.py | 6 ++++-- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/flaschengeist/models/session.py b/flaschengeist/models/session.py index df95b85..c170ad6 100644 --- a/flaschengeist/models/session.py +++ b/flaschengeist/models/session.py @@ -1,3 +1,5 @@ +from __future__ import annotations # TODO: Remove if python requirement is >= 3.10 + from datetime import datetime, timedelta, timezone from . import ModelSerializeMixin, UtcDateTime diff --git a/flaschengeist/models/setting.py b/flaschengeist/models/setting.py index 8b3acc4..6e8f74d 100644 --- a/flaschengeist/models/setting.py +++ b/flaschengeist/models/setting.py @@ -1,3 +1,5 @@ +from __future__ import annotations # TODO: Remove if python requirement is >= 3.10 + from ..database import db diff --git a/flaschengeist/models/user.py b/flaschengeist/models/user.py index b8e8b83..6e23e4b 100644 --- a/flaschengeist/models/user.py +++ b/flaschengeist/models/user.py @@ -1,3 +1,5 @@ +from __future__ import annotations # TODO: Remove if python requirement is >= 3.10 + from flask import url_for from typing import Optional from datetime import date, datetime @@ -6,6 +8,7 @@ from sqlalchemy.orm.collections import attribute_mapped_collection from ..database import db from . import ModelSerializeMixin, UtcDateTime + association_table = db.Table( "user_x_role", db.Column("user_id", db.Integer, db.ForeignKey("user.id")), @@ -30,7 +33,7 @@ class Role(db.Model, ModelSerializeMixin): __tablename__ = "role" id: int = db.Column(db.Integer, primary_key=True) name: str = db.Column(db.String(30), unique=True) - permissions: [Permission] = db.relationship("Permission", secondary=role_permission_association_table) + permissions: list[Permission] = db.relationship("Permission", secondary=role_permission_association_table) class User(db.Model, ModelSerializeMixin): @@ -55,11 +58,11 @@ class User(db.Model, ModelSerializeMixin): lastname: str = db.Column(db.String(50), nullable=False) mail: str = db.Column(db.String(60), nullable=False) birthday: Optional[date] = db.Column(db.Date) - roles: [str] = [] - permissions: Optional[type([str])] = None + roles: list[str] = [] + permissions: Optional[list[str]] = None avatar_url: Optional[str] = "" - roles_: [Role] = db.relationship("Role", secondary=association_table, cascade="save-update, merge") + roles_: list[Role] = db.relationship("Role", secondary=association_table, cascade="save-update, merge") _id = db.Column("id", db.Integer, primary_key=True) _sessions = db.relationship("Session", back_populates="_user") _attributes = db.relationship( diff --git a/flaschengeist/plugins/balance/models.py b/flaschengeist/plugins/balance/models.py index 03ab890..41c3520 100644 --- a/flaschengeist/plugins/balance/models.py +++ b/flaschengeist/plugins/balance/models.py @@ -1,6 +1,7 @@ +from __future__ import annotations # TODO: Remove if python requirement is >= 3.10 + from datetime import datetime from typing import Optional - from sqlalchemy.ext.hybrid import hybrid_property from flaschengeist.database import db diff --git a/flaschengeist/plugins/pricelist/models.py b/flaschengeist/plugins/pricelist/models.py index 1308f29..5adab4d 100644 --- a/flaschengeist/plugins/pricelist/models.py +++ b/flaschengeist/plugins/pricelist/models.py @@ -1,7 +1,9 @@ +from __future__ import annotations # TODO: Remove if python requirement is >= 3.10 + from flaschengeist.database import db from flaschengeist.models import ModelSerializeMixin -from typing import Optional +from typing import Optional, Union drink_tag_association = db.Table( "drink_x_tag", @@ -70,7 +72,7 @@ class DrinkIngredient(db.Model, ModelSerializeMixin): id: int = db.Column("id", db.Integer, primary_key=True) volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False), nullable=False) drink_ingredient_id: int = db.Column("drink_ingredient_id", db.Integer, db.ForeignKey("drink.id")) - drink_ingredient: "Drink" = db.relationship("Drink") + drink_ingredient = db.relationship("Drink") price: float = 0 @property @@ -104,7 +106,9 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin): id: int = db.Column("id", db.Integer, primary_key=True) volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False)) prices: [DrinkPrice] = db.relationship(DrinkPrice, back_populates="volume", cascade="all,delete,delete-orphan") - ingredients: [Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id) + ingredients: Union[DrinkIngredient, ExtraIngredient] = [] + # TODO: Really protected or just not exported (e.g. name_)? + _ingredients: [Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id) drink_id = db.Column(db.Integer, db.ForeignKey("drink.id"), nullable=False) @@ -123,7 +127,7 @@ class Drink(db.Model, ModelSerializeMixin): cost_price_pro_volume: Optional[float] = db.Column(db.Numeric(precision=5, scale=3, asdecimal=False)) cost_price_package_netto: Optional[float] = db.Column(db.Numeric(precision=5, scale=3, asdecimal=False)) - tags: [Optional[Tag]] = db.relationship("Tag", secondary=drink_tag_association, cascade="save-update, merge") + tags: list[Tag] = db.relationship("Tag", secondary=drink_tag_association, cascade="save-update, merge") type_id_ = db.Column("type_id", db.Integer, db.ForeignKey("drink_type.id")) type: DrinkType = db.relationship("DrinkType") diff --git a/flaschengeist/plugins/schedule/models.py b/flaschengeist/plugins/schedule/models.py index 118c9b9..04c060f 100644 --- a/flaschengeist/plugins/schedule/models.py +++ b/flaschengeist/plugins/schedule/models.py @@ -1,3 +1,5 @@ +from __future__ import annotations # TODO: Remove if python requirement is >= 3.10 + from datetime import datetime from typing import Optional @@ -55,7 +57,7 @@ class Job(db.Model, ModelSerializeMixin): end: Optional[datetime] = db.Column(UtcDateTime) comment: str = db.Column(db.String(256)) type: JobType = db.relationship("JobType") - services: [Service] = db.relationship("Service", back_populates="job_") + services: list[Service] = db.relationship("Service", back_populates="job_") required_services: float = db.Column(db.Numeric(precision=4, scale=2, asdecimal=False), nullable=False) event_ = db.relationship("Event", back_populates="jobs") @@ -81,6 +83,6 @@ class Event(db.Model, ModelSerializeMixin): end: datetime = db.Column(UtcDateTime) description: Optional[str] = db.Column(db.String(255)) type: EventType = db.relationship("EventType") - jobs: [Job] = db.relationship( + jobs: list[Job] = db.relationship( "Job", back_populates="event_", cascade="all,delete,delete-orphan", order_by="[Job.start, Job.end]" )