From 8fd8b710fbfe9d9b2ea616f371794b4eee3957a3 Mon Sep 17 00:00:00 2001 From: Niklas Meinzer Date: Tue, 28 Oct 2025 21:06:49 +0100 Subject: [PATCH] New models and first data on the landing page --- src/allmende_payment_system/app.py | 7 ++- src/allmende_payment_system/models.py | 57 ++++++++++++++++++- .../templates/index.html.jinja | 15 ++--- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/allmende_payment_system/app.py b/src/allmende_payment_system/app.py index a854e34..1231e14 100644 --- a/src/allmende_payment_system/app.py +++ b/src/allmende_payment_system/app.py @@ -49,6 +49,11 @@ SessionDep = Annotated[Session, Depends(get_session)] async def landing_page(request: Request, user_info: UserDep, session: SessionDep): user = ensure_user(user_info, session) print(f"User {user.username} ({user.display_name}) accessed landing page") + transactions = [] + for account in user.accounts: + transactions += account.transactions + transactions = sorted(transactions, key=lambda t: t.timestamp) return templates.TemplateResponse( - "index.html.jinja", context={"request": request, "user": user} + "index.html.jinja", + context={"request": request, "user": user, "transactions": transactions}, ) diff --git a/src/allmende_payment_system/models.py b/src/allmende_payment_system/models.py index b276981..c8005b8 100644 --- a/src/allmende_payment_system/models.py +++ b/src/allmende_payment_system/models.py @@ -1,4 +1,8 @@ -from sqlalchemy import Column, ForeignKey, Table +import datetime +import decimal +import typing + +from sqlalchemy import Column, ForeignKey, Numeric, Table from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship TABLE_PREFIX = "aps_" @@ -18,10 +22,11 @@ class Account(Base): secondary=TABLE_PREFIX + "user_account_association", back_populates="accounts", ) + transactions: Mapped[list["Transaction"]] = relationship("Transaction") @property def balance(self): - return 141.23 + return sum(t.total_amount for t in self.transactions) user_account_association = Table( @@ -41,3 +46,51 @@ class User(Base): accounts: Mapped[list["Account"]] = relationship( "Account", secondary=user_account_association, back_populates="users" ) + + +class Area(Base): + __tablename__ = TABLE_PREFIX + "area" + id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) + name: Mapped[str] = mapped_column(nullable=False, unique=True) + + +class Product(Base): + __tablename__ = TABLE_PREFIX + "product" + id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) + name: Mapped[str] = mapped_column(nullable=False, unique=True) + + price: Mapped[decimal.Decimal] = mapped_column(Numeric(10, 2)) + # TODO: limit this to actually used vat rates? + vat_rate: Mapped[decimal.Decimal] = mapped_column(Numeric(10, 2)) + + area_id: Mapped[int] = mapped_column(ForeignKey(TABLE_PREFIX + "area.id")) + area: Mapped["Area"] = relationship("Area") + + +TransactionTypes = typing.Literal[ + "product", + "deposit", + "withdrawal", + "expense", +] + + +class Transaction(Base): + __tablename__ = TABLE_PREFIX + "transaction" + id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) + type: Mapped[TransactionTypes] = mapped_column(nullable=False) + quantity: Mapped[decimal.Decimal] = mapped_column(Numeric(10, 2), nullable=True) + timestamp: Mapped[datetime.datetime] = mapped_column( + nullable=False, default=datetime.datetime.now() + ) + total_amount: Mapped[decimal.Decimal] = mapped_column( + Numeric(10, 2), nullable=False + ) + + product_id: Mapped[int] = mapped_column( + ForeignKey(TABLE_PREFIX + "product.id"), nullable=True + ) + product: Mapped["Product"] = relationship("Product") + + account_id: Mapped[int] = mapped_column(ForeignKey(TABLE_PREFIX + "account.id")) + account: Mapped["Account"] = relationship("Account", back_populates="transactions") diff --git a/src/allmende_payment_system/templates/index.html.jinja b/src/allmende_payment_system/templates/index.html.jinja index 1bb5bb4..b24287e 100644 --- a/src/allmende_payment_system/templates/index.html.jinja +++ b/src/allmende_payment_system/templates/index.html.jinja @@ -42,23 +42,20 @@
{% if transactions and transactions|length > 0 %} - {% for transaction in transactions[:5] %} + {% for transaction in transactions[:10] %}
-
{{ transaction.product_name | default('Transaction') }}
+
{{ transaction.product.name }}
- {{ transaction.date | default('') }} - {% if transaction.description %} - · {{ transaction.description }} - {% endif %} + {{ transaction.timestamp }}
- - {{ '%+.2f' | format(transaction.amount | default(0)) }} € + + {{ '%+.2f' | format(transaction.total_amount | default(0)) }} € {% if transaction.quantity %} -
{{ transaction.quantity }} {{ transaction.unit | default('pcs') }}
+
{{ transaction.quantity }} €
{% endif %}