New models and first data on the landing page
This commit is contained in:
@@ -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},
|
||||
)
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -42,23 +42,20 @@
|
||||
<div class="card">
|
||||
<div class="list-group list-group-flush">
|
||||
{% if transactions and transactions|length > 0 %}
|
||||
{% for transaction in transactions[:5] %}
|
||||
{% for transaction in transactions[:10] %}
|
||||
<div class="list-group-item d-flex justify-content-between align-items-start py-3">
|
||||
<div class="flex-grow-1">
|
||||
<div class="fw-semibold">{{ transaction.product_name | default('Transaction') }}</div>
|
||||
<div class="fw-semibold">{{ transaction.product.name }}</div>
|
||||
<small class="text-muted">
|
||||
{{ transaction.date | default('') }}
|
||||
{% if transaction.description %}
|
||||
· {{ transaction.description }}
|
||||
{% endif %}
|
||||
{{ transaction.timestamp }}
|
||||
</small>
|
||||
</div>
|
||||
<div class="text-end ms-3">
|
||||
<span class="fs-5 fw-bold {% if transaction.amount < 0 %}text-danger{% else %}text-success{% endif %}">
|
||||
{{ '%+.2f' | format(transaction.amount | default(0)) }} €
|
||||
<span class="fs-5 fw-bold {% if transaction.total_amount < 0 %}text-danger{% else %}text-success{% endif %}">
|
||||
{{ '%+.2f' | format(transaction.total_amount | default(0)) }} €
|
||||
</span>
|
||||
{% if transaction.quantity %}
|
||||
<div class="small text-muted">{{ transaction.quantity }} {{ transaction.unit | default('pcs') }}</div>
|
||||
<div class="small text-muted">{{ transaction.quantity }} €</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user