Build [v1.9.19]

This commit is contained in:
Daniel Bedeleanu
2026-04-14 20:44:01 +03:00
parent fcb187974e
commit 00ee4cf9c5
38 changed files with 2059 additions and 157 deletions

View File

@@ -5,6 +5,8 @@ from .. import models, schemas, auth
from ..database import get_db
from ..db_manager import DbManager
from ..scheduler import sync_scheduler_config
from fastapi.responses import FileResponse
from fastapi import UploadFile, File
router = APIRouter(
prefix="/admin/db",
@@ -103,3 +105,63 @@ def update_db_settings(
# Re-trigger scheduler sync
sync_scheduler_config()
return settings
@router.get("/export")
def export_database(
current_admin: auth.TokenData = Depends(auth.get_current_admin)
):
"""Download the current database file."""
try:
path = DbManager.export_db()
return FileResponse(
path,
media_type="application/x-sqlite3",
filename="inventory_export.db"
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/import")
async def import_database(
file: UploadFile = File(...),
db: Session = Depends(get_db),
current_admin: auth.TokenData = Depends(auth.get_current_admin)
):
"""Upload and replace the current database. DANGEROUS."""
contents = await file.read()
try:
DbManager.import_db(contents, db, user_id=current_admin.sub)
return {"status": "success", "message": "Database successfully imported and replaced."}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/settings/prompt")
def get_ai_prompt(
db: Session = Depends(get_db),
current_admin: auth.TokenData = Depends(auth.get_current_admin)
):
"""Get the current AI extraction prompt."""
setting = db.query(models.SystemSetting).filter(models.SystemSetting.key == "ai_extraction_prompt").first()
if not setting:
return {"value": ""}
return {"value": setting.value}
@router.post("/settings/prompt")
def update_ai_prompt(
payload: dict,
db: Session = Depends(get_db),
current_admin: auth.TokenData = Depends(auth.get_current_admin)
):
"""Update the AI extraction prompt."""
value = payload.get("value")
if value is None:
raise HTTPException(status_code=400, detail="Value required")
existing = db.query(models.SystemSetting).filter(models.SystemSetting.key == "ai_extraction_prompt").first()
if existing:
existing.value = value
else:
db.add(models.SystemSetting(key="ai_extraction_prompt", value=value))
db.commit()
return {"status": "success"}

View File

@@ -97,10 +97,18 @@ def create_item(
current_user: auth.TokenData = Depends(auth.get_current_user)
):
"""[C-01] Create item — only for authenticated users. [M-02] user_id from token."""
# Check if barcode exists
db_item = db.query(models.Item).filter(models.Item.barcode == item.barcode).first()
if db_item:
raise HTTPException(status_code=400, detail="Barcode already registered")
# [AUTO-PERSIST] Create Category/Color if not exists
if item.category:
cat = db.query(models.Category).filter(models.Category.name == item.category).first()
if not cat:
db.add(models.Category(name=item.category))
db.commit()
if item.color:
col = db.query(models.Color).filter(models.Color.name == item.color).first()
if not col:
db.add(models.Color(name=item.color))
db.commit()
db_item = models.Item(**item.model_dump())
db.add(db_item)
@@ -148,6 +156,19 @@ def update_item(
if not db_item:
raise HTTPException(status_code=404, detail="Item not found")
# [AUTO-PERSIST] Create Category/Color if not exists
if item.category:
cat = db.query(models.Category).filter(models.Category.name == item.category).first()
if not cat:
db.add(models.Category(name=item.category))
db.commit()
if item.color:
col = db.query(models.Color).filter(models.Color.name == item.color).first()
if not col:
db.add(models.Color(name=item.color))
db.commit()
update_data = item.model_dump(exclude_unset=True)
for key, value in update_data.items():
setattr(db_item, key, value)