File size: 2,621 Bytes
80feb1b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Form
from sqlalchemy.orm import Session
from typing import Optional
import os
import shutil
from datetime import datetime, timezone
from ..database import get_db, Settings
from ..models.settings import Settings as SettingsModel, SettingsUpdate
router = APIRouter(
prefix="/settings",
tags=["settings"],
responses={404: {"description": "Not found"}},
)
# Get hotel settings
@router.get("/", response_model=SettingsModel)
def get_settings(db: Session = Depends(get_db)):
# Get the first settings record or create one if it doesn't exist
settings = db.query(Settings).first()
if not settings:
# Create default settings
settings = Settings(
hotel_name="Tabble Hotel",
address="123 Main Street, City",
contact_number="+1 123-456-7890",
email="[email protected]",
)
db.add(settings)
db.commit()
db.refresh(settings)
return settings
# Update hotel settings
@router.put("/", response_model=SettingsModel)
async def update_settings(
hotel_name: str = Form(...),
address: Optional[str] = Form(None),
contact_number: Optional[str] = Form(None),
email: Optional[str] = Form(None),
tax_id: Optional[str] = Form(None),
logo: Optional[UploadFile] = File(None),
db: Session = Depends(get_db)
):
# Get existing settings or create new
settings = db.query(Settings).first()
if not settings:
settings = Settings(
hotel_name=hotel_name,
address=address,
contact_number=contact_number,
email=email,
tax_id=tax_id,
)
db.add(settings)
else:
# Update fields
settings.hotel_name = hotel_name
settings.address = address
settings.contact_number = contact_number
settings.email = email
settings.tax_id = tax_id
# Handle logo upload if provided
if logo:
# Create directory if it doesn't exist
os.makedirs("app/static/images/logo", exist_ok=True)
# Save logo
logo_path = f"app/static/images/logo/hotel_logo_{logo.filename}"
with open(logo_path, "wb") as buffer:
shutil.copyfileobj(logo.file, buffer)
# Update settings with logo path
settings.logo_path = f"/static/images/logo/hotel_logo_{logo.filename}"
# Update timestamp
settings.updated_at = datetime.now(timezone.utc)
db.commit()
db.refresh(settings)
return settings
|