Added the timestamp, user_pk, and random_id type aliases to simplify the column definition of the data models.

This commit is contained in:
依瑪貓 2023-04-24 03:37:33 +08:00
parent 6d780e9296
commit ded85d88f7

View File

@ -34,6 +34,18 @@ from accounting import db
from accounting.locale import gettext from accounting.locale import gettext
from accounting.utils.user import user_cls, user_pk_column from accounting.utils.user import user_cls, user_pk_column
timestamp: t.Type[dt.datetime] \
= t.Annotated[dt.datetime, mapped_column(db.DateTime(timezone=True),
server_default=db.func.now())]
"""The timestamp."""
user_pk: t.Type[int] \
= t.Annotated[int, mapped_column(db.ForeignKey(user_pk_column,
onupdate="CASCADE"))]
"""The user primary key."""
random_id: t.Type[int] \
= t.Annotated[int, mapped_column(primary_key=True, autoincrement=False)]
"""The random ID."""
class BaseAccount(db.Model): class BaseAccount(db.Model):
"""A base account.""" """A base account."""
@ -100,7 +112,7 @@ class Account(db.Model):
"""An account.""" """An account."""
__tablename__ = "accounting_accounts" __tablename__ = "accounting_accounts"
"""The table name.""" """The table name."""
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=False) id: Mapped[random_id]
"""The account ID.""" """The account ID."""
base_code: Mapped[str] \ base_code: Mapped[str] \
= mapped_column(db.ForeignKey(BaseAccount.code, onupdate="CASCADE", = mapped_column(db.ForeignKey(BaseAccount.code, onupdate="CASCADE",
@ -114,21 +126,15 @@ class Account(db.Model):
"""The title.""" """The title."""
is_need_offset: Mapped[bool] = mapped_column(default=False) is_need_offset: Mapped[bool] = mapped_column(default=False)
"""Whether the journal entry line items of this account need offset.""" """Whether the journal entry line items of this account need offset."""
created_at: Mapped[dt.datetime] \ created_at: Mapped[timestamp]
= mapped_column(db.DateTime(timezone=True),
server_default=db.func.now())
"""The time of creation.""" """The time of creation."""
created_by_id: Mapped[int] \ created_by_id: Mapped[user_pk] = mapped_column()
= mapped_column(db.ForeignKey(user_pk_column, onupdate="CASCADE"))
"""The ID of the creator.""" """The ID of the creator."""
created_by: Mapped[user_cls] = db.relationship(foreign_keys=created_by_id) created_by: Mapped[user_cls] = db.relationship(foreign_keys=created_by_id)
"""The creator.""" """The creator."""
updated_at: Mapped[dt.datetime] \ updated_at: Mapped[timestamp]
= mapped_column(db.DateTime(timezone=True),
server_default=db.func.now())
"""The time of last update.""" """The time of last update."""
updated_by_id: Mapped[int] \ updated_by_id: Mapped[user_pk] = mapped_column()
= mapped_column(db.ForeignKey(user_pk_column, onupdate="CASCADE"))
"""The ID of the updator.""" """The ID of the updator."""
updated_by: Mapped[user_cls] = db.relationship(foreign_keys=updated_by_id) updated_by: Mapped[user_cls] = db.relationship(foreign_keys=updated_by_id)
"""The updator.""" """The updator."""
@ -370,21 +376,15 @@ class Currency(db.Model):
"""The code.""" """The code."""
name_l10n: Mapped[str] = mapped_column("name") name_l10n: Mapped[str] = mapped_column("name")
"""The name.""" """The name."""
created_at: Mapped[dt.datetime] \ created_at: Mapped[timestamp]
= mapped_column(db.DateTime(timezone=True),
server_default=db.func.now())
"""The time of creation.""" """The time of creation."""
created_by_id: Mapped[int] \ created_by_id: Mapped[user_pk] = mapped_column()
= mapped_column(db.ForeignKey(user_pk_column, onupdate="CASCADE"))
"""The ID of the creator.""" """The ID of the creator."""
created_by: Mapped[user_cls] = db.relationship(foreign_keys=created_by_id) created_by: Mapped[user_cls] = db.relationship(foreign_keys=created_by_id)
"""The creator.""" """The creator."""
updated_at: Mapped[dt.datetime] \ updated_at: Mapped[timestamp]
= mapped_column(db.DateTime(timezone=True),
server_default=db.func.now())
"""The time of last update.""" """The time of last update."""
updated_by_id: Mapped[int] \ updated_by_id: Mapped[user_pk] = mapped_column()
= mapped_column(db.ForeignKey(user_pk_column, onupdate="CASCADE"))
"""The ID of the updator.""" """The ID of the updator."""
updated_by: Mapped[user_cls] \ updated_by: Mapped[user_cls] \
= db.relationship(foreign_keys=updated_by_id) = db.relationship(foreign_keys=updated_by_id)
@ -535,7 +535,7 @@ class JournalEntry(db.Model):
"""A journal entry.""" """A journal entry."""
__tablename__ = "accounting_journal_entries" __tablename__ = "accounting_journal_entries"
"""The table name.""" """The table name."""
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=False) id: Mapped[random_id]
"""The journal entry ID.""" """The journal entry ID."""
date: Mapped[dt.date] date: Mapped[dt.date]
"""The date.""" """The date."""
@ -543,21 +543,15 @@ class JournalEntry(db.Model):
"""The account number under the date.""" """The account number under the date."""
note: Mapped[str | None] note: Mapped[str | None]
"""The note.""" """The note."""
created_at: Mapped[dt.datetime] \ created_at: Mapped[timestamp]
= mapped_column(db.DateTime(timezone=True),
server_default=db.func.now())
"""The time of creation.""" """The time of creation."""
created_by_id: Mapped[int] \ created_by_id: Mapped[user_pk] = mapped_column()
= mapped_column(db.ForeignKey(user_pk_column, onupdate="CASCADE"))
"""The ID of the creator.""" """The ID of the creator."""
created_by: Mapped[user_cls] = db.relationship(foreign_keys=created_by_id) created_by: Mapped[user_cls] = db.relationship(foreign_keys=created_by_id)
"""The creator.""" """The creator."""
updated_at: Mapped[dt.datetime] \ updated_at: Mapped[timestamp]
= mapped_column(db.DateTime(timezone=True),
server_default=db.func.now())
"""The time of last update.""" """The time of last update."""
updated_by_id: Mapped[int] \ updated_by_id: Mapped[user_pk] = mapped_column()
= mapped_column(db.ForeignKey(user_pk_column, onupdate="CASCADE"))
"""The ID of the updator.""" """The ID of the updator."""
updated_by: Mapped[user_cls] = db.relationship(foreign_keys=updated_by_id) updated_by: Mapped[user_cls] = db.relationship(foreign_keys=updated_by_id)
"""The updator.""" """The updator."""
@ -652,7 +646,7 @@ class JournalEntryLineItem(db.Model):
"""A line item in the journal entry.""" """A line item in the journal entry."""
__tablename__ = "accounting_journal_entry_line_items" __tablename__ = "accounting_journal_entry_line_items"
"""The table name.""" """The table name."""
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=False) id: Mapped[random_id] = mapped_column()
"""The line item ID.""" """The line item ID."""
journal_entry_id: Mapped[int] \ journal_entry_id: Mapped[int] \
= mapped_column(db.ForeignKey(JournalEntry.id, onupdate="CASCADE", = mapped_column(db.ForeignKey(JournalEntry.id, onupdate="CASCADE",
@ -883,21 +877,15 @@ class Option(db.Model):
"""The name.""" """The name."""
value: Mapped[str] = mapped_column(db.Text) value: Mapped[str] = mapped_column(db.Text)
"""The option value.""" """The option value."""
created_at: Mapped[dt.datetime] \ created_at: Mapped[timestamp]
= mapped_column(db.DateTime(timezone=True),
server_default=db.func.now())
"""The time of creation.""" """The time of creation."""
created_by_id: Mapped[int] \ created_by_id: Mapped[user_pk] = mapped_column()
= mapped_column(db.ForeignKey(user_pk_column, onupdate="CASCADE"))
"""The ID of the creator.""" """The ID of the creator."""
created_by: Mapped[user_cls] = db.relationship(foreign_keys=created_by_id) created_by: Mapped[user_cls] = db.relationship(foreign_keys=created_by_id)
"""The creator.""" """The creator."""
updated_at: Mapped[dt.datetime] \ updated_at: Mapped[timestamp]
= mapped_column(db.DateTime(timezone=True),
server_default=db.func.now())
"""The time of last update.""" """The time of last update."""
updated_by_id: Mapped[int] \ updated_by_id: Mapped[user_pk] = mapped_column()
= mapped_column(db.ForeignKey(user_pk_column, onupdate="CASCADE"))
"""The ID of the updator.""" """The ID of the updator."""
updated_by: Mapped[user_cls] = db.relationship(foreign_keys=updated_by_id) updated_by: Mapped[user_cls] = db.relationship(foreign_keys=updated_by_id)
"""The updator.""" """The updator."""