Move to FastAPI with accounts, SQLite books, export and tax returns
Storage and export - Keep books and transactions in SQLite (data/cashbook.db) instead of .ods files; existing .ods books are migrated once at startup - Export a book as the cashbook spreadsheet (.xlsx/.ods, built from the template via LibreOffice) or as CSV - GST rounded per transaction to match the spreadsheet Tax - GST return page with IRD GST101A boxes 5-15 per six-monthly, two-monthly or monthly period, due dates, and a Zero-rated GST type - Income tax page with IR10 (financial statements summary) and IR4 (company return) boxes, from the cashbook plus year-end amounts typed in and saved per book FastAPI and accounts (following led-editor) - Replace the stdlib server with a FastAPI app (src/cashbook_app) run by uvicorn; dependencies in Pipfile - Cookie sessions with bcrypt passwords, first-run admin setup or BOOTSTRAP_ADMIN_*, invites (link or email), sign-in throttling, password change that signs out other sessions - Users page for administrators: rename, reset password, promote or demote, delete, list and revoke invites; last admin is protected - Replaces the shared CASHBOOK_PASSWORD basic auth; AUTH_* settings in .env (see .env.example) - Docker image uses a venv with system site packages for python3-uno - pytest suite for auth, user management, imports, GST and tax figures Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
17b48f25e8
commit
4b3e5f0932
47 files changed
+4189
-862
No files matched your search
@@ -0,0 +1,75 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from cashbook_app.db.models import User
|
||||
from cashbook_app.db.session import get_db
|
||||
from cashbook_app.deps import require_superuser
|
||||
from cashbook_app.schemas.users import (InviteCreateRequest, InviteCreateResponse, InvitePublic,
|
||||
UserPublic, UserUpdateAdmin)
|
||||
from cashbook_app.services import accounts
|
||||
|
||||
router = APIRouter(prefix="/api/users", tags=["users"])
|
||||
|
||||
|
||||
@router.get("", response_model=list[UserPublic])
|
||||
def list_users(_admin: User = Depends(require_superuser), db: Session = Depends(get_db)):
|
||||
return [UserPublic.model_validate(u) for u in accounts.list_users(db)]
|
||||
|
||||
|
||||
@router.patch("/{user_id}", response_model=UserPublic)
|
||||
def update_user(user_id: int, body: UserUpdateAdmin, admin: User = Depends(require_superuser),
|
||||
db: Session = Depends(get_db)) -> UserPublic:
|
||||
if user_id == admin.id and body.is_superuser is False:
|
||||
raise HTTPException(status_code=400, detail="You can't remove your own admin rights")
|
||||
try:
|
||||
updated = accounts.update_user(db, user_id, username=body.username,
|
||||
password=body.password, is_superuser=body.is_superuser)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
if updated is None:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
return UserPublic.model_validate(updated)
|
||||
|
||||
|
||||
@router.delete("/{user_id}")
|
||||
def delete_user(user_id: int, admin: User = Depends(require_superuser),
|
||||
db: Session = Depends(get_db)) -> dict:
|
||||
if user_id == admin.id:
|
||||
raise HTTPException(status_code=400, detail="You can't delete your own account")
|
||||
try:
|
||||
if not accounts.delete_user(db, user_id):
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
return {"message": "User deleted"}
|
||||
|
||||
|
||||
@router.get("/invites", response_model=list[InvitePublic])
|
||||
def list_invites(_admin: User = Depends(require_superuser), db: Session = Depends(get_db)):
|
||||
return [InvitePublic.model_validate(i) for i in accounts.list_pending_invites(db)]
|
||||
|
||||
|
||||
@router.post("/invites", response_model=InviteCreateResponse)
|
||||
def create_invite(body: InviteCreateRequest, request: Request, admin: User = Depends(require_superuser),
|
||||
db: Session = Depends(get_db)) -> InviteCreateResponse:
|
||||
email = (body.email or "").strip().lower()
|
||||
invite = accounts.create_invite(db, email, invited_by_user_id=admin.id,
|
||||
expires_days=body.expires_days, grants_superuser=body.grants_superuser)
|
||||
invite_url = accounts.build_invite_url(invite.token, str(request.base_url))
|
||||
delivered = False
|
||||
if email:
|
||||
try:
|
||||
delivered = accounts.send_invite_email(email, invite_url)
|
||||
except Exception:
|
||||
delivered = False
|
||||
return InviteCreateResponse(id=invite.id, email=invite.email, invite_url=invite_url, delivered=delivered)
|
||||
|
||||
|
||||
@router.delete("/invites/{invite_id}")
|
||||
def revoke_invite(invite_id: int, _admin: User = Depends(require_superuser),
|
||||
db: Session = Depends(get_db)) -> dict:
|
||||
if not accounts.revoke_invite(db, invite_id):
|
||||
raise HTTPException(status_code=404, detail="Invite not found")
|
||||
return {"message": "Invite revoked"}
|
||||
Reference in new issue
Block a user