Initial commit
This commit is contained in:
@@ -0,0 +1,363 @@
|
||||
"""
|
||||
UI Routes for Specs Accounting System.
|
||||
FastAPI + Jinja2 templates.
|
||||
"""
|
||||
from fastapi import APIRouter, Request, Query, HTTPException, Depends
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional
|
||||
|
||||
from app.database import get_db
|
||||
from app.services.resource_allocation_service import ResourceAllocationService
|
||||
from app.services.procurement_flow_service import ProcurementFlowService
|
||||
from app.models import Project, System, Guardian, Module, Resource, ResourceAllocation, Specification, Purchase
|
||||
|
||||
router = APIRouter(prefix="/ui", tags=["UI"])
|
||||
templates = Jinja2Templates(directory="app/templates")
|
||||
|
||||
|
||||
def get_project_stats(db: Session, project_id: int) -> dict:
|
||||
"""Get statistics for a project."""
|
||||
systems_count = db.query(System).filter(
|
||||
System.project_id == project_id
|
||||
).count()
|
||||
|
||||
resources = db.query(Resource).filter(
|
||||
Resource.project_id == project_id
|
||||
).all()
|
||||
|
||||
total_received = sum(r.quantity for r in resources)
|
||||
total_allocated = sum(
|
||||
db.query(ResourceAllocation.quantity).filter(
|
||||
ResourceAllocation.resource_id == r.id
|
||||
).scalar() or 0
|
||||
for r in resources
|
||||
)
|
||||
|
||||
return {
|
||||
'systems_count': systems_count,
|
||||
'total_received': total_received,
|
||||
'total_allocated': total_allocated,
|
||||
'total_remaining': total_received - total_allocated
|
||||
}
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def home(request: Request, db: Session = Depends(get_db)):
|
||||
"""Home page with project list."""
|
||||
projects = db.query(Project).all()
|
||||
project_stats = {p.id: get_project_stats(db, p.id) for p in projects}
|
||||
return templates.TemplateResponse(
|
||||
"home.html",
|
||||
{
|
||||
"request": request,
|
||||
"projects": projects,
|
||||
"stats": project_stats
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/projects")
|
||||
async def projects_list(request: Request, db: Session = Depends(get_db)):
|
||||
"""List all projects."""
|
||||
projects = db.query(Project).all()
|
||||
return templates.TemplateResponse(
|
||||
"projects/list.html",
|
||||
{"request": request, "projects": projects}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/projects/{project_id}")
|
||||
async def project_detail(request: Request, project_id: int, db: Session = Depends(get_db)):
|
||||
"""Project detail page."""
|
||||
project = db.query(Project).filter(Project.id == project_id).first()
|
||||
if not project:
|
||||
raise HTTPException(status_code=404, detail="Project not found")
|
||||
|
||||
customers = db.query(Project).filter(Project.id == project_id).first().customers
|
||||
systems = db.query(System).filter(System.project_id == project_id).all()
|
||||
contracts = db.query(Project).filter(Project.id == project_id).first().contracts
|
||||
resources = db.query(Resource).filter(Resource.project_id == project_id).all()
|
||||
purchases = db.query(Purchase).filter(Purchase.project_id == project_id).all()
|
||||
|
||||
stats = get_project_stats(db, project_id)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"projects/detail.html",
|
||||
{
|
||||
"request": request,
|
||||
"project": project,
|
||||
"customers": customers,
|
||||
"systems": systems,
|
||||
"contracts": contracts,
|
||||
"resources": resources,
|
||||
"purchases": purchases,
|
||||
"stats": stats
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/systems")
|
||||
async def systems_list(request: Request, project_id: Optional[int] = None, db: Session = Depends(get_db)):
|
||||
"""List all systems."""
|
||||
if project_id:
|
||||
systems = db.query(System).filter(System.project_id == project_id).all()
|
||||
else:
|
||||
systems = db.query(System).all()
|
||||
return templates.TemplateResponse(
|
||||
"systems/list.html",
|
||||
{"request": request, "systems": systems}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/systems/{system_id}")
|
||||
async def system_detail(request: Request, system_id: int, db: Session = Depends(get_db)):
|
||||
"""System detail page - main UI page."""
|
||||
system = db.query(System).filter(System.id == system_id).first()
|
||||
if not system:
|
||||
raise HTTPException(status_code=404, detail="System not found")
|
||||
|
||||
# Get Common part (guardian_id = NULL)
|
||||
common_modules = db.query(SystemModule).filter(
|
||||
SystemModule.system_id == system_id,
|
||||
SystemModule.guardian_id == None
|
||||
).all()
|
||||
|
||||
# Get Guardians
|
||||
guardians = db.query(Guardian).filter(Guardian.system_id == system_id).all()
|
||||
|
||||
guardian_data = []
|
||||
for guardian in guardians:
|
||||
core = db.query(SystemModule).filter(
|
||||
SystemModule.system_id == system_id,
|
||||
SystemModule.guardian_id == guardian.id,
|
||||
Module.name == "Core"
|
||||
).first()
|
||||
|
||||
other_modules = db.query(SystemModule).filter(
|
||||
SystemModule.system_id == system_id,
|
||||
SystemModule.guardian_id == guardian.id
|
||||
).all()
|
||||
|
||||
guardian_data.append({
|
||||
'guardian': guardian,
|
||||
'core': core,
|
||||
'modules': [m for m in other_modules if m.module.name != 'Core']
|
||||
})
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"systems/detail.html",
|
||||
{
|
||||
"request": request,
|
||||
"system": system,
|
||||
"common_modules": common_modules,
|
||||
"guardian_data": guardian_data
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/guardians/{guardian_id}")
|
||||
async def guardian_detail(request: Request, guardian_id: int, db: Session = Depends(get_db)):
|
||||
"""Guardian detail page."""
|
||||
guardian = db.query(Guardian).filter(Guardian.id == guardian_id).first()
|
||||
if not guardian:
|
||||
raise HTTPException(status_code=404, detail="Guardian not found")
|
||||
|
||||
core = db.query(SystemModule).filter(
|
||||
SystemModule.guardian_id == guardian_id,
|
||||
Module.name == "Core"
|
||||
).first()
|
||||
|
||||
modules = db.query(SystemModule).filter(
|
||||
SystemModule.guardian_id == guardian_id
|
||||
).all()
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"guardians/detail.html",
|
||||
{
|
||||
"request": request,
|
||||
"guardian": guardian,
|
||||
"core": core,
|
||||
"modules": modules
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/resources")
|
||||
async def resources_list(request: Request, project_id: Optional[int] = None, db: Session = Depends(get_db)):
|
||||
"""Resource pool page."""
|
||||
query = db.query(Resource)
|
||||
if project_id:
|
||||
query = query.filter(Resource.project_id == project_id)
|
||||
|
||||
resources = query.all()
|
||||
return templates.TemplateResponse(
|
||||
"resources/list.html",
|
||||
{"request": request, "resources": resources, "project_id": project_id}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/resources/{resource_id}")
|
||||
async def resource_detail(request: Request, resource_id: int, db: Session = Depends(get_db)):
|
||||
"""Resource detail page."""
|
||||
resource = db.query(Resource).filter(Resource.id == resource_id).first()
|
||||
if not resource:
|
||||
raise HTTPException(status_code=404, detail="Resource not found")
|
||||
|
||||
# Get allocations for this resource
|
||||
allocations = db.query(ResourceAllocation).filter(
|
||||
ResourceAllocation.resource_id == resource_id
|
||||
).all()
|
||||
|
||||
# Calculate remaining
|
||||
allocated = sum(a.quantity for a in allocations)
|
||||
remaining = resource.quantity - allocated
|
||||
|
||||
# Get system and guardian info for allocations
|
||||
allocation_details = []
|
||||
for alloc in allocations:
|
||||
system = db.query(System).filter(System.id == alloc.system_id).first()
|
||||
guardian = db.query(Guardian).filter(Guardian.id == alloc.guardian_id).first() if alloc.guardian_id else None
|
||||
|
||||
allocation_details.append({
|
||||
'allocation': alloc,
|
||||
'system': system,
|
||||
'guardian': guardian,
|
||||
'is_common': guardian is None
|
||||
})
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"resources/detail.html",
|
||||
{
|
||||
"request": request,
|
||||
"resource": resource,
|
||||
"allocations": allocation_details,
|
||||
"remaining": remaining
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/resources/{resource_id}/allocate")
|
||||
async def allocate_resource(
|
||||
request: Request,
|
||||
resource_id: int,
|
||||
system_id: int = Query(..., description="System ID"),
|
||||
guardian_id: Optional[int] = Query(None, description="Guardian ID (optional)"),
|
||||
quantity: int = Query(..., description="Quantity to allocate"),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Allocate resource to system/guardian."""
|
||||
try:
|
||||
service = ResourceAllocationService(db)
|
||||
allocation = service.allocate_resource(
|
||||
resource_id=resource_id,
|
||||
system_id=system_id,
|
||||
quantity=quantity,
|
||||
guardian_id=guardian_id,
|
||||
description="UI allocation"
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
"resources/allocated.html",
|
||||
{
|
||||
"request": request,
|
||||
"allocation": allocation,
|
||||
"resource_id": resource_id
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
return templates.TemplateResponse(
|
||||
"resources/allocate_error.html",
|
||||
{
|
||||
"request": request,
|
||||
"resource_id": resource_id,
|
||||
"error": str(e)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/specifications")
|
||||
async def specifications_list(request: Request, project_id: Optional[int] = None, db: Session = Depends(get_db)):
|
||||
"""Specifications list page."""
|
||||
query = db.query(Specification)
|
||||
if project_id:
|
||||
query = query.filter(Specification.project_id == project_id)
|
||||
|
||||
specifications = query.all()
|
||||
return templates.TemplateResponse(
|
||||
"specifications/list.html",
|
||||
{"request": request, "specifications": specifications, "project_id": project_id}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/specifications/{specification_id}")
|
||||
async def specification_detail(request: Request, specification_id: int, db: Session = Depends(get_db)):
|
||||
"""Specification detail page."""
|
||||
specification = db.query(Specification).filter(Specification.id == specification_id).first()
|
||||
if not specification:
|
||||
raise HTTPException(status_code=404, detail="Specification not found")
|
||||
|
||||
items = db.query(SpecificationItem).filter(
|
||||
SpecificationItem.specification_id == specification_id
|
||||
).all()
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"specifications/detail.html",
|
||||
{
|
||||
"request": request,
|
||||
"specification": specification,
|
||||
"items": items
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/purchases")
|
||||
async def purchases_list(request: Request, project_id: Optional[int] = None, db: Session = Depends(get_db)):
|
||||
"""Purchases list page."""
|
||||
query = db.query(Purchase)
|
||||
if project_id:
|
||||
query = query.filter(Purchase.project_id == project_id)
|
||||
|
||||
purchases = query.all()
|
||||
return templates.TemplateResponse(
|
||||
"purchases/list.html",
|
||||
{"request": request, "purchases": purchases, "project_id": project_id}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/purchases/{purchase_id}")
|
||||
async def purchase_detail(request: Request, purchase_id: int, db: Session = Depends(get_db)):
|
||||
"""Purchase detail page."""
|
||||
purchase = db.query(Purchase).filter(Purchase.id == purchase_id).first()
|
||||
if not purchase:
|
||||
raise HTTPException(status_code=404, detail="Purchase not found")
|
||||
|
||||
items = db.query(PurchaseItem).filter(
|
||||
PurchaseItem.purchase_id == purchase_id
|
||||
).all()
|
||||
|
||||
specifications = db.query(Specification).filter(
|
||||
Specification.id.in_(
|
||||
db.query(PurchaseSpecification.specification_id).filter(
|
||||
PurchaseSpecification.purchase_id == purchase_id
|
||||
)
|
||||
)
|
||||
).all()
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"purchases/detail.html",
|
||||
{
|
||||
"request": request,
|
||||
"purchase": purchase,
|
||||
"items": items,
|
||||
"specifications": specifications
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.get("/seed")
|
||||
async def seed_demo_data(db: Session = Depends(get_db)):
|
||||
"""Seed demo data endpoint."""
|
||||
from app.utils.seed_data import seed_demo_data
|
||||
seed_demo_data(db)
|
||||
return {"message": "Demo data seeded successfully"}
|
||||
Reference in New Issue
Block a user