Spaces:
Runtime error
Runtime error
Greg
Claude Opus 4.7 (1M context)
Preparar despliegue en fly.io: monolito dockerizado + seeder demo
6aa6081 Download backend/app/api/v1/endpoints/settings.py from Talosware/talos-access: direct link, hf CLI and curl.
- Browser
- Download file 6.34 kB
-
https://huggingface.co/spaces/Talosware/talos-access/resolve/main/backend/app/api/v1/endpoints/settings.py
- Command line
-
hf download hf://spaces/Talosware/talos-access/backend/app/api/v1/endpoints/settings.py
-
curl -L -o settings.py https://huggingface.co/spaces/Talosware/talos-access/resolve/main/backend/app/api/v1/endpoints/settings.py
6.34 kB
| from datetime import date | |
| from fastapi import APIRouter, Depends, HTTPException, status, Query | |
| from sqlalchemy.orm import Session | |
| from pydantic import BaseModel | |
| from typing import Optional | |
| from app.core.database import get_db | |
| from app.dependencies.auth import get_current_user | |
| from app.models.user import User | |
| from app.models.account import Account | |
| from app.models.holiday import Holiday, HolidayType | |
| from app.middleware.rbac import _has_admin_perm | |
| router = APIRouter(prefix="/settings", tags=["settings"]) | |
| class ScheduleDay(BaseModel): | |
| entry: str | |
| exit: str | |
| class SettingsResponse(BaseModel): | |
| organization_name: str | |
| address: Optional[str] = None | |
| director_name: Optional[str] = None | |
| schedule: Optional[dict] = None | |
| tolerance: int = 15 | |
| checkin_mode: str = "manual" | |
| self_checkin_enabled: bool = False | |
| class SettingsUpdate(BaseModel): | |
| organization_name: Optional[str] = None | |
| address: Optional[str] = None | |
| director_name: Optional[str] = None | |
| schedule: Optional[dict] = None | |
| tolerance: Optional[int] = None | |
| checkin_mode: Optional[str] = None | |
| self_checkin_enabled: Optional[bool] = None | |
| def get_settings( | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| if not _has_admin_perm(current_user, db): | |
| raise HTTPException(status_code=403, detail="Admin only") | |
| account = db.query(Account).filter(Account.id == current_user.account_id).first() | |
| return SettingsResponse( | |
| organization_name=account.organization_name, | |
| address=account.address, | |
| director_name=account.director_name, | |
| schedule=account.schedule, | |
| tolerance=account.tolerance or 15, | |
| checkin_mode=account.checkin_mode or "manual", | |
| self_checkin_enabled=bool(account.self_checkin_enabled), | |
| ) | |
| def update_settings( | |
| payload: SettingsUpdate, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| if not _has_admin_perm(current_user, db): | |
| raise HTTPException(status_code=403, detail="Admin only") | |
| account = db.query(Account).filter(Account.id == current_user.account_id).first() | |
| if payload.organization_name is not None: | |
| account.organization_name = payload.organization_name | |
| if payload.address is not None: | |
| account.address = payload.address | |
| if payload.director_name is not None: | |
| account.director_name = payload.director_name | |
| if payload.schedule is not None: | |
| account.schedule = payload.schedule | |
| if payload.tolerance is not None: | |
| account.tolerance = payload.tolerance | |
| if payload.checkin_mode is not None: | |
| account.checkin_mode = payload.checkin_mode | |
| if payload.self_checkin_enabled is not None: | |
| account.self_checkin_enabled = 1 if payload.self_checkin_enabled else 0 | |
| db.commit() | |
| db.refresh(account) | |
| return SettingsResponse( | |
| organization_name=account.organization_name, | |
| address=account.address, | |
| director_name=account.director_name, | |
| schedule=account.schedule, | |
| tolerance=account.tolerance or 15, | |
| checkin_mode=account.checkin_mode or "manual", | |
| self_checkin_enabled=bool(account.self_checkin_enabled), | |
| ) | |
| # --- Holiday CRUD --- | |
| class HolidayCreate(BaseModel): | |
| name: str | |
| start_date: date | |
| end_date: Optional[date] = None | |
| type: HolidayType = HolidayType.national | |
| class HolidayResponse(BaseModel): | |
| id: int | |
| account_id: int | |
| name: str | |
| start_date: date | |
| end_date: Optional[date] = None | |
| type: HolidayType | |
| model_config = {"from_attributes": True} | |
| class HolidayUpdate(BaseModel): | |
| name: Optional[str] = None | |
| start_date: Optional[date] = None | |
| end_date: Optional[date] = None | |
| type: Optional[HolidayType] = None | |
| def list_holidays( | |
| year: Optional[int] = Query(None), | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| if not _has_admin_perm(current_user, db): | |
| raise HTTPException(status_code=403, detail="Admin only") | |
| query = db.query(Holiday).filter(Holiday.account_id == current_user.account_id) | |
| if year: | |
| query = query.filter( | |
| Holiday.start_date <= date(year, 12, 31), | |
| Holiday.end_date >= date(year, 1, 1), | |
| ) | |
| return query.order_by(Holiday.start_date).all() | |
| def create_holiday( | |
| payload: HolidayCreate, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| if not _has_admin_perm(current_user, db): | |
| raise HTTPException(status_code=403, detail="Admin only") | |
| h = Holiday(account_id=current_user.account_id, **payload.model_dump(exclude_none=True)) | |
| db.add(h) | |
| db.commit() | |
| db.refresh(h) | |
| return h | |
| def update_holiday( | |
| holiday_id: int, | |
| payload: HolidayUpdate, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| if not _has_admin_perm(current_user, db): | |
| raise HTTPException(status_code=403, detail="Admin only") | |
| h = db.query(Holiday).filter(Holiday.id == holiday_id, Holiday.account_id == current_user.account_id).first() | |
| if not h: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Holiday not found") | |
| update_data = payload.model_dump(exclude_none=True) | |
| for key, value in update_data.items(): | |
| setattr(h, key, value) | |
| db.commit() | |
| db.refresh(h) | |
| return h | |
| def delete_holiday( | |
| holiday_id: int, | |
| db: Session = Depends(get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| if not _has_admin_perm(current_user, db): | |
| raise HTTPException(status_code=403, detail="Admin only") | |
| h = db.query(Holiday).filter(Holiday.id == holiday_id, Holiday.account_id == current_user.account_id).first() | |
| if not h: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Holiday not found") | |
| db.delete(h) | |
| db.commit() | |