feat: Buchungs-Pagination, Inbox-Scan-Button, Grafana-Direktansteuerung, UI-Feinschliff
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -233,6 +233,7 @@ podman run -d --name "$GRAFANA_CTR_NAME" --pod "$POD_NAME" \
|
||||
-e GF_SECURITY_ADMIN_PASSWORD="$FB_PASSWORD" \
|
||||
-e GF_SECURITY_ALLOW_EMBEDDING=true \
|
||||
-e GF_SECURITY_COOKIE_SAMESITE=lax \
|
||||
-e GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH=/var/lib/grafana/dashboards/finanzen.json \
|
||||
-e FINANCE_READ_PASSWORD \
|
||||
-v "$GRAFANA_PROVISIONING_DIR:/etc/grafana/provisioning:Z,ro" \
|
||||
-v "$GRAFANA_DASHBOARDS_DIR:/var/lib/grafana/dashboards:Z,ro" \
|
||||
|
||||
@@ -19,10 +19,12 @@ from app.routers.planning import list_planned as _list_planned
|
||||
from app.routers.planning import list_recurring as _list_recurring
|
||||
from app.routers.planning import recurring_suggestions as _recurring_suggestions
|
||||
from app.routers.scenarios import list_scenarios as _list_scenarios
|
||||
from app.routers.transactions import list_transactions
|
||||
from app.routers.transactions import count_transactions, list_transactions
|
||||
from app.services.balances import account_balance, total_balance
|
||||
from app.version import get_version
|
||||
|
||||
PAGE_SIZE = 50
|
||||
|
||||
templates = Jinja2Templates(directory="app/templates")
|
||||
templates.env.globals["app_version"] = get_version()
|
||||
router = APIRouter()
|
||||
@@ -81,8 +83,21 @@ def buchungen(
|
||||
category_id: int | None = None,
|
||||
q: str | None = None,
|
||||
status: str = "confirmed",
|
||||
page: int = 1,
|
||||
session: Session = Depends(get_session),
|
||||
):
|
||||
page = max(1, page)
|
||||
total = count_transactions(
|
||||
account_id=account_id,
|
||||
date_from=date_from,
|
||||
date_to=date_to,
|
||||
category_id=category_id,
|
||||
q=q,
|
||||
status=status,
|
||||
session=session,
|
||||
)
|
||||
total_pages = max(1, -(-total // PAGE_SIZE)) # ceil division
|
||||
page = min(page, total_pages)
|
||||
txs = list_transactions(
|
||||
account_id=account_id,
|
||||
date_from=date_from,
|
||||
@@ -90,8 +105,8 @@ def buchungen(
|
||||
category_id=category_id,
|
||||
q=q,
|
||||
status=status,
|
||||
limit=200,
|
||||
offset=0,
|
||||
limit=PAGE_SIZE,
|
||||
offset=(page - 1) * PAGE_SIZE,
|
||||
session=session,
|
||||
)
|
||||
accounts = session.execute(select(Account)).scalars().all()
|
||||
@@ -108,6 +123,8 @@ def buchungen(
|
||||
"q": q or "",
|
||||
"status": status,
|
||||
},
|
||||
"page": page,
|
||||
"total_pages": total_pages,
|
||||
})
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ from decimal import Decimal
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy import func, or_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.auth import require_auth
|
||||
@@ -46,19 +46,18 @@ class TransactionPatch(BaseModel):
|
||||
category_id: int | None = None
|
||||
|
||||
|
||||
@router.get("", response_model=list[TransactionOut])
|
||||
def list_transactions(
|
||||
def _apply_transaction_filters(
|
||||
stmt,
|
||||
account_id: int | None = None,
|
||||
date_from: date | None = None,
|
||||
date_to: date | None = None,
|
||||
category_id: int | None = None,
|
||||
q: str | None = None,
|
||||
status: str = "confirmed",
|
||||
limit: int = 200,
|
||||
offset: int = 0,
|
||||
session: Session = Depends(get_session),
|
||||
):
|
||||
stmt = select(Transaction)
|
||||
"""Wendet die Buchungs-Filter auf ein select()-Statement an. Wird sowohl fuer
|
||||
die Liste als auch fuer die dazugehoerige Gesamtanzahl (Pagination) genutzt,
|
||||
damit beide garantiert dieselben Ergebnisse zaehlen/zeigen."""
|
||||
if status:
|
||||
stmt = stmt.where(Transaction.status == status)
|
||||
if account_id is not None:
|
||||
@@ -73,10 +72,47 @@ def list_transactions(
|
||||
like = f"%{q}%"
|
||||
stmt = stmt.where(or_(Transaction.purpose.ilike(like),
|
||||
Transaction.counterparty.ilike(like)))
|
||||
return stmt
|
||||
|
||||
|
||||
@router.get("", response_model=list[TransactionOut])
|
||||
def list_transactions(
|
||||
account_id: int | None = None,
|
||||
date_from: date | None = None,
|
||||
date_to: date | None = None,
|
||||
category_id: int | None = None,
|
||||
q: str | None = None,
|
||||
status: str = "confirmed",
|
||||
limit: int = 200,
|
||||
offset: int = 0,
|
||||
session: Session = Depends(get_session),
|
||||
):
|
||||
stmt = _apply_transaction_filters(
|
||||
select(Transaction), account_id=account_id, date_from=date_from,
|
||||
date_to=date_to, category_id=category_id, q=q, status=status,
|
||||
)
|
||||
stmt = stmt.order_by(Transaction.booking_date, Transaction.id).offset(offset).limit(limit)
|
||||
return [TransactionOut.model_validate(t) for t in session.execute(stmt).scalars()]
|
||||
|
||||
|
||||
def count_transactions(
|
||||
account_id: int | None = None,
|
||||
date_from: date | None = None,
|
||||
date_to: date | None = None,
|
||||
category_id: int | None = None,
|
||||
q: str | None = None,
|
||||
status: str = "confirmed",
|
||||
session: Session = Depends(get_session),
|
||||
) -> int:
|
||||
"""Gesamtanzahl der Buchungen fuer die gleichen Filter wie list_transactions
|
||||
(ohne limit/offset) - Grundlage fuer die Seitenzahl in der GUI-Pagination."""
|
||||
stmt = _apply_transaction_filters(
|
||||
select(func.count(Transaction.id)), account_id=account_id, date_from=date_from,
|
||||
date_to=date_to, category_id=category_id, q=q, status=status,
|
||||
)
|
||||
return session.execute(stmt).scalar_one()
|
||||
|
||||
|
||||
@router.post("", response_model=TransactionOut, status_code=201)
|
||||
def create_transaction(data: TransactionIn, session: Session = Depends(get_session)):
|
||||
if session.get(Account, data.account_id) is None:
|
||||
|
||||
@@ -144,6 +144,10 @@ button {
|
||||
margin-right: 0.5rem;
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: #888;
|
||||
}
|
||||
|
||||
section.planning-section {
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
@@ -155,5 +159,6 @@ section.planning-section fieldset {
|
||||
.version {
|
||||
margin-top: 2rem;
|
||||
color: #888;
|
||||
font-size: 0.85rem;
|
||||
text-align: center;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
@@ -34,12 +34,20 @@
|
||||
hx-on::after-request="if(event.detail.successful){htmx.trigger(document.getElementById('imports'), 'refresh')}">
|
||||
<button type="submit">Übernehmen</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
<form class="inline-form" hx-delete="/api/imports/{{ s.id }}" hx-swap="none"
|
||||
hx-confirm="Import „{{ s.filename }}“ wirklich verwerfen?"
|
||||
hx-on::after-request="if(event.detail.successful){htmx.trigger(document.getElementById('imports'), 'refresh')}">
|
||||
<button type="submit">Verwerfen</button>
|
||||
</form>
|
||||
{% elif s.status == "confirmed" %}
|
||||
<span class="muted">Bestätigt — Buchungen übernommen</span>
|
||||
{% else %}
|
||||
<form class="inline-form" hx-delete="/api/imports/{{ s.id }}" hx-swap="none"
|
||||
hx-confirm="Import „{{ s.filename }}“ wirklich verwerfen?"
|
||||
hx-on::after-request="if(event.detail.successful){htmx.trigger(document.getElementById('imports'), 'refresh')}">
|
||||
<button type="submit">Verwerfen</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
|
||||
@@ -7,6 +7,11 @@
|
||||
Kontoauszug-PDF hier ablegen oder klicken zum Hochladen
|
||||
<input type="file" id="file-input" accept="application/pdf" style="display:none">
|
||||
</div>
|
||||
<button type="button" hx-post="/api/imports/scan-inbox" hx-swap="none"
|
||||
hx-on::after-request="if(event.detail.successful){htmx.trigger(document.getElementById('imports'), 'refresh')}">
|
||||
Inbox scannen
|
||||
</button>
|
||||
<p class="muted">Durchsucht <code>~/.local/share/finance_pod/data/inbox/</code> nach neuen PDF-Kontoauszügen.</p>
|
||||
<p id="upload-message"></p>
|
||||
|
||||
<h2>Importe</h2>
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
</table>
|
||||
|
||||
<h2>Grafana-Dashboard</h2>
|
||||
<iframe class="grafana" src="http://{{ request.url.hostname or '127.0.0.1' }}:8097"></iframe>
|
||||
<iframe class="grafana" src="http://{{ request.url.hostname or '127.0.0.1' }}:8097/d/finanzen/finanzen?orgId=1&kiosk"></iframe>
|
||||
<p class="hint">Kein Diagramm sichtbar? Einmal in
|
||||
<a href="http://{{ request.url.hostname or '127.0.0.1' }}:8097" target="_blank">Grafana anmelden</a>
|
||||
(gleiches Passwort wie hier).</p>
|
||||
|
||||
@@ -87,6 +87,24 @@
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p class="pager">
|
||||
{% set pager_params = {
|
||||
'account_id': filters.account_id,
|
||||
'date_from': filters.date_from.isoformat() if filters.date_from else None,
|
||||
'date_to': filters.date_to.isoformat() if filters.date_to else None,
|
||||
'category_id': filters.category_id,
|
||||
'q': filters.q,
|
||||
'status': filters.status,
|
||||
} %}
|
||||
{% if page > 1 %}
|
||||
<a href="/buchungen?{% for k, v in pager_params.items() %}{% if v %}{{ k }}={{ v|urlencode }}&{% endif %}{% endfor %}page={{ page - 1 }}">Zurück</a>
|
||||
{% endif %}
|
||||
Seite {{ page }} von {{ total_pages }}
|
||||
{% if page < total_pages %}
|
||||
<a href="/buchungen?{% for k, v in pager_params.items() %}{% if v %}{{ k }}={{ v|urlencode }}&{% endif %}{% endfor %}page={{ page + 1 }}">Weiter</a>
|
||||
{% endif %}
|
||||
</p>
|
||||
|
||||
<fieldset>
|
||||
<legend>Buchung manuell erfassen</legend>
|
||||
<form hx-ext="json-form" hx-post="/api/transactions" hx-swap="none"
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
from datetime import date, timedelta
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
def test_pages_require_login(client):
|
||||
for path in ("/", "/buchungen", "/import", "/planung", "/hilfe"):
|
||||
r = client.get(path, follow_redirects=False)
|
||||
@@ -41,6 +45,37 @@ def test_version_visible(client):
|
||||
assert "v0.2.0" in client.get("/").text
|
||||
|
||||
|
||||
def test_buchungen_pagination(client, db):
|
||||
from app.models.tables import Account, Transaction
|
||||
|
||||
acc = Account(bank="dkb", iban="DE01", name="G", type="giro")
|
||||
db.add(acc)
|
||||
db.flush()
|
||||
base = date(2026, 1, 1)
|
||||
for i in range(1, 61):
|
||||
purpose = "MARKER51" if i == 51 else f"TX{i}"
|
||||
db.add(Transaction(
|
||||
account_id=acc.id, booking_date=base + timedelta(days=i),
|
||||
amount=Decimal("10.00"), purpose=purpose, counterparty="X",
|
||||
status="confirmed", dedup_hash=f"hash-{i}",
|
||||
))
|
||||
db.commit()
|
||||
|
||||
client.post("/login", data={"username": "admin", "password": "geheim"})
|
||||
|
||||
r1 = client.get("/buchungen")
|
||||
assert r1.status_code == 200
|
||||
assert "Seite 1 von 2" in r1.text
|
||||
assert "Weiter" in r1.text
|
||||
assert "MARKER51" not in r1.text
|
||||
|
||||
r2 = client.get("/buchungen?page=2")
|
||||
assert r2.status_code == 200
|
||||
assert "Seite 2 von 2" in r2.text
|
||||
assert "Zurück" in r2.text
|
||||
assert "MARKER51" in r2.text
|
||||
|
||||
|
||||
def test_planning_page_has_all_sections(client):
|
||||
client.post("/login", data={"username": "admin", "password": "geheim"})
|
||||
r = client.get("/planung")
|
||||
|
||||
@@ -131,6 +131,17 @@ def test_upload_rejects_non_pdf(client, tmp_path, monkeypatch):
|
||||
assert r.status_code == 400
|
||||
|
||||
|
||||
def test_delete_error_import_returns_204(client, db):
|
||||
# Ein fehlgeschlagener Import (status="error") muss verworfen werden
|
||||
# koennen - nur bestaetigte Importe sind vor dem Loeschen geschuetzt.
|
||||
from app.models.tables import Statement
|
||||
db.add(Statement(filename="kaputt.pdf", bank="dkb", status="error",
|
||||
error_message="PDF nicht lesbar"))
|
||||
db.commit()
|
||||
sid = db.query(Statement).one().id
|
||||
assert client.delete(f"/api/imports/{sid}", headers=H).status_code == 204
|
||||
|
||||
|
||||
def test_upload_corrupt_pdf_produces_error_statement_not_500(client, tmp_path, monkeypatch):
|
||||
# Datei besteht die Endungspruefung (.pdf), ist aber strukturell kein
|
||||
# gueltiges PDF -> pdfplumber wirft eine eigene Exception (keine
|
||||
|
||||
Reference in New Issue
Block a user