diff --git a/web/app.py b/web/app.py index 7474f66..20ad8bc 100644 --- a/web/app.py +++ b/web/app.py @@ -27,6 +27,7 @@ logger = logging.getLogger(__name__) app = FastAPI(title="Домовой Бот - MEGA Admin Panel", version="3.0.0") templates = Jinja2Templates(directory="web/templates") app.mount("/static", StaticFiles(directory="web/static"), name="static") +app.mount("/static/service_images", StaticFiles(directory="data/service_images"), name="service_images") security = HTTPBasic() @@ -218,13 +219,62 @@ async def api_add_service(request: Request, username: str = Depends(get_current_ phone=data['phone'], category=data.get('category', 'other'), description=data.get('description', ''), - verified_by_admin=True + verified_by_admin=True, + image_path=data.get('image_path', None) ) session.add(service) await session.commit() return JSONResponse({"success": True, "message": "Служба добавлена"}) +@app.post("/api/service/upload_image") +async def api_upload_service_image( + file: UploadFile = File(...), + category: str = Form(...), + username: str = Depends(get_current_admin) +): + """Загрузить картинку для службы""" + import os + import uuid + from pathlib import Path + + # Создаём папку для картинок + images_dir = Path("data/service_images") + images_dir.mkdir(parents=True, exist_ok=True) + + # Генерируем уникальное имя файла + file_extension = file.filename.split(".")[-1] if "." in file.filename else "jpg" + unique_filename = f"{category}_{uuid.uuid4().hex}.{file_extension}" + file_path = images_dir / unique_filename + + # Сохраняем файл + try: + with open(file_path, "wb") as f: + content = await file.read() + f.write(content) + + # Обновляем все службы этой категории + async with AsyncSessionLocal() as session: + await session.execute( + update(Service) + .where(Service.category == category) + .values(image_path=str(file_path)) + ) + await session.commit() + + return JSONResponse({ + "success": True, + "message": f"Картинка загружена", + "file_path": str(file_path) + }) + except Exception as e: + logger.error(f"Ошибка загрузки картинки: {e}") + return JSONResponse({ + "success": False, + "error": str(e) + }, status_code=500) + + @app.post("/api/service/{service_id}/update") async def api_update_service(service_id: int, request: Request, username: str = Depends(get_current_admin)): diff --git a/web/templates/phones.html b/web/templates/phones.html index 5bc7aea..e43f459 100644 --- a/web/templates/phones.html +++ b/web/templates/phones.html @@ -10,6 +10,7 @@ .sidebar .nav-link { color: rgba(255,255,255,0.8); padding: 1rem; } .sidebar .nav-link:hover, .sidebar .nav-link.active { color: white; background-color: rgba(255,255,255,0.1); } .content { padding: 2rem; } + .service-image-preview { max-width: 200px; max-height: 200px; object-fit: contain; }
@@ -49,11 +50,11 @@| Название | Телефон | Описание | +Картинка | Действия | {{ service.name }} | {{ service.phone }} | {{ service.description or '—' }} | +
+ {% if service.image_path %}
+ ✅ + {% else %} + ❌ Нет + {% endif %} + |
|---|