[System] New annotation format for future compatibility

This commit is contained in:
Ferdinand Thiessen 2021-03-18 11:52:51 +01:00
parent 948c700e46
commit 466efcf9e7
6 changed files with 25 additions and 11 deletions

View File

@ -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

View File

@ -1,3 +1,5 @@
from __future__ import annotations # TODO: Remove if python requirement is >= 3.10
from ..database import db

View File

@ -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(

View File

@ -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

View File

@ -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")

View File

@ -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]"
)