Spaces:
Running
Running
axeronis commited on
Commit ·
cfa37ce
1
Parent(s): 5095eda
fix: site_settings import error - use get_admin_user with async session
Browse files- backend/app/api/site_settings.py +45 -27
backend/app/api/site_settings.py
CHANGED
|
@@ -3,18 +3,21 @@ Site Settings API - Manage global site configuration like trust logos.
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
from fastapi import APIRouter, Depends, HTTPException
|
| 6 |
-
from sqlmodel import
|
| 7 |
-
from
|
|
|
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from datetime import datetime
|
| 10 |
import json
|
| 11 |
|
| 12 |
-
from app.core.database import
|
| 13 |
-
from app.
|
| 14 |
-
from app.models import SiteSettings
|
| 15 |
|
| 16 |
router = APIRouter(prefix="/site-settings", tags=["site-settings"])
|
| 17 |
|
|
|
|
|
|
|
| 18 |
|
| 19 |
class SettingUpdate(BaseModel):
|
| 20 |
value: str # JSON string or simple string
|
|
@@ -30,11 +33,21 @@ class TrustLogosUpdate(BaseModel):
|
|
| 30 |
logos: List[str]
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Public endpoint - no auth required
|
| 34 |
@router.get("/public/{key}")
|
| 35 |
async def get_public_setting(
|
| 36 |
key: str,
|
| 37 |
-
session:
|
| 38 |
):
|
| 39 |
"""Get a public site setting by key (no auth required)."""
|
| 40 |
# Only allow certain keys to be public
|
|
@@ -43,9 +56,10 @@ async def get_public_setting(
|
|
| 43 |
if key not in allowed_public_keys:
|
| 44 |
raise HTTPException(status_code=404, detail="Setting not found")
|
| 45 |
|
| 46 |
-
|
| 47 |
select(SiteSettings).where(SiteSettings.key == key)
|
| 48 |
-
)
|
|
|
|
| 49 |
|
| 50 |
if not setting:
|
| 51 |
# Return default values
|
|
@@ -61,24 +75,26 @@ async def get_public_setting(
|
|
| 61 |
|
| 62 |
@router.get("/", response_model=List[SettingResponse])
|
| 63 |
async def list_settings(
|
| 64 |
-
session:
|
| 65 |
-
_admin = Depends(
|
| 66 |
):
|
| 67 |
"""List all site settings (admin only)."""
|
| 68 |
-
|
|
|
|
| 69 |
return [SettingResponse(key=s.key, value=s.value, updated_at=s.updated_at) for s in settings]
|
| 70 |
|
| 71 |
|
| 72 |
@router.get("/{key}", response_model=SettingResponse)
|
| 73 |
async def get_setting(
|
| 74 |
key: str,
|
| 75 |
-
session:
|
| 76 |
-
_admin = Depends(
|
| 77 |
):
|
| 78 |
"""Get a site setting by key (admin only)."""
|
| 79 |
-
|
| 80 |
select(SiteSettings).where(SiteSettings.key == key)
|
| 81 |
-
)
|
|
|
|
| 82 |
|
| 83 |
if not setting:
|
| 84 |
raise HTTPException(status_code=404, detail="Setting not found")
|
|
@@ -90,13 +106,14 @@ async def get_setting(
|
|
| 90 |
async def update_setting(
|
| 91 |
key: str,
|
| 92 |
data: SettingUpdate,
|
| 93 |
-
session:
|
| 94 |
-
admin = Depends(
|
| 95 |
):
|
| 96 |
"""Update or create a site setting (admin only)."""
|
| 97 |
-
|
| 98 |
select(SiteSettings).where(SiteSettings.key == key)
|
| 99 |
-
)
|
|
|
|
| 100 |
|
| 101 |
if setting:
|
| 102 |
setting.value = data.value
|
|
@@ -110,8 +127,8 @@ async def update_setting(
|
|
| 110 |
)
|
| 111 |
session.add(setting)
|
| 112 |
|
| 113 |
-
session.commit()
|
| 114 |
-
session.refresh(setting)
|
| 115 |
|
| 116 |
return SettingResponse(key=setting.key, value=setting.value, updated_at=setting.updated_at)
|
| 117 |
|
|
@@ -119,13 +136,14 @@ async def update_setting(
|
|
| 119 |
@router.put("/trust-logos/update")
|
| 120 |
async def update_trust_logos(
|
| 121 |
data: TrustLogosUpdate,
|
| 122 |
-
session:
|
| 123 |
-
admin = Depends(
|
| 124 |
):
|
| 125 |
"""Convenience endpoint to update trust logos list (admin only)."""
|
| 126 |
-
|
| 127 |
select(SiteSettings).where(SiteSettings.key == "trust_logos")
|
| 128 |
-
)
|
|
|
|
| 129 |
|
| 130 |
value = json.dumps(data.logos)
|
| 131 |
|
|
@@ -141,7 +159,7 @@ async def update_trust_logos(
|
|
| 141 |
)
|
| 142 |
session.add(setting)
|
| 143 |
|
| 144 |
-
session.commit()
|
| 145 |
-
session.refresh(setting)
|
| 146 |
|
| 147 |
return {"success": True, "logos": data.logos}
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
from fastapi import APIRouter, Depends, HTTPException
|
| 6 |
+
from sqlmodel import select
|
| 7 |
+
from sqlmodel.ext.asyncio.session import AsyncSession
|
| 8 |
+
from typing import List
|
| 9 |
from pydantic import BaseModel
|
| 10 |
from datetime import datetime
|
| 11 |
import json
|
| 12 |
|
| 13 |
+
from app.core.database import get_async_session
|
| 14 |
+
from app.api.deps import get_current_user
|
| 15 |
+
from app.models import User, SiteSettings
|
| 16 |
|
| 17 |
router = APIRouter(prefix="/site-settings", tags=["site-settings"])
|
| 18 |
|
| 19 |
+
ADMIN_EMAIL = "axeroni.s2@gmail.com"
|
| 20 |
+
|
| 21 |
|
| 22 |
class SettingUpdate(BaseModel):
|
| 23 |
value: str # JSON string or simple string
|
|
|
|
| 33 |
logos: List[str]
|
| 34 |
|
| 35 |
|
| 36 |
+
async def get_admin_user(current_user: User = Depends(get_current_user)):
|
| 37 |
+
"""Restrict access to admin email."""
|
| 38 |
+
if current_user.email.lower() != ADMIN_EMAIL.lower():
|
| 39 |
+
raise HTTPException(
|
| 40 |
+
status_code=403,
|
| 41 |
+
detail="Access denied. Admin rights required."
|
| 42 |
+
)
|
| 43 |
+
return current_user
|
| 44 |
+
|
| 45 |
+
|
| 46 |
# Public endpoint - no auth required
|
| 47 |
@router.get("/public/{key}")
|
| 48 |
async def get_public_setting(
|
| 49 |
key: str,
|
| 50 |
+
session: AsyncSession = Depends(get_async_session)
|
| 51 |
):
|
| 52 |
"""Get a public site setting by key (no auth required)."""
|
| 53 |
# Only allow certain keys to be public
|
|
|
|
| 56 |
if key not in allowed_public_keys:
|
| 57 |
raise HTTPException(status_code=404, detail="Setting not found")
|
| 58 |
|
| 59 |
+
result = await session.execute(
|
| 60 |
select(SiteSettings).where(SiteSettings.key == key)
|
| 61 |
+
)
|
| 62 |
+
setting = result.scalar_one_or_none()
|
| 63 |
|
| 64 |
if not setting:
|
| 65 |
# Return default values
|
|
|
|
| 75 |
|
| 76 |
@router.get("/", response_model=List[SettingResponse])
|
| 77 |
async def list_settings(
|
| 78 |
+
session: AsyncSession = Depends(get_async_session),
|
| 79 |
+
_admin: User = Depends(get_admin_user)
|
| 80 |
):
|
| 81 |
"""List all site settings (admin only)."""
|
| 82 |
+
result = await session.execute(select(SiteSettings))
|
| 83 |
+
settings = result.scalars().all()
|
| 84 |
return [SettingResponse(key=s.key, value=s.value, updated_at=s.updated_at) for s in settings]
|
| 85 |
|
| 86 |
|
| 87 |
@router.get("/{key}", response_model=SettingResponse)
|
| 88 |
async def get_setting(
|
| 89 |
key: str,
|
| 90 |
+
session: AsyncSession = Depends(get_async_session),
|
| 91 |
+
_admin: User = Depends(get_admin_user)
|
| 92 |
):
|
| 93 |
"""Get a site setting by key (admin only)."""
|
| 94 |
+
result = await session.execute(
|
| 95 |
select(SiteSettings).where(SiteSettings.key == key)
|
| 96 |
+
)
|
| 97 |
+
setting = result.scalar_one_or_none()
|
| 98 |
|
| 99 |
if not setting:
|
| 100 |
raise HTTPException(status_code=404, detail="Setting not found")
|
|
|
|
| 106 |
async def update_setting(
|
| 107 |
key: str,
|
| 108 |
data: SettingUpdate,
|
| 109 |
+
session: AsyncSession = Depends(get_async_session),
|
| 110 |
+
admin: User = Depends(get_admin_user)
|
| 111 |
):
|
| 112 |
"""Update or create a site setting (admin only)."""
|
| 113 |
+
result = await session.execute(
|
| 114 |
select(SiteSettings).where(SiteSettings.key == key)
|
| 115 |
+
)
|
| 116 |
+
setting = result.scalar_one_or_none()
|
| 117 |
|
| 118 |
if setting:
|
| 119 |
setting.value = data.value
|
|
|
|
| 127 |
)
|
| 128 |
session.add(setting)
|
| 129 |
|
| 130 |
+
await session.commit()
|
| 131 |
+
await session.refresh(setting)
|
| 132 |
|
| 133 |
return SettingResponse(key=setting.key, value=setting.value, updated_at=setting.updated_at)
|
| 134 |
|
|
|
|
| 136 |
@router.put("/trust-logos/update")
|
| 137 |
async def update_trust_logos(
|
| 138 |
data: TrustLogosUpdate,
|
| 139 |
+
session: AsyncSession = Depends(get_async_session),
|
| 140 |
+
admin: User = Depends(get_admin_user)
|
| 141 |
):
|
| 142 |
"""Convenience endpoint to update trust logos list (admin only)."""
|
| 143 |
+
result = await session.execute(
|
| 144 |
select(SiteSettings).where(SiteSettings.key == "trust_logos")
|
| 145 |
+
)
|
| 146 |
+
setting = result.scalar_one_or_none()
|
| 147 |
|
| 148 |
value = json.dumps(data.logos)
|
| 149 |
|
|
|
|
| 159 |
)
|
| 160 |
session.add(setting)
|
| 161 |
|
| 162 |
+
await session.commit()
|
| 163 |
+
await session.refresh(setting)
|
| 164 |
|
| 165 |
return {"success": True, "logos": data.logos}
|