photo_processor.py: извлекаем логин из email

This commit is contained in:
2026-08-14 14:41:30 +07:00
parent b065a85114
commit a98fcd0feb
+33
View File
@@ -81,6 +81,30 @@ def get_crop_coordinates(image_bytes: bytes) -> tuple | None:
return None
def _check_moderation_status(user_id: str) -> dict:
"""
Проверяет текущий статус модерации пользователя.
Возвращает {'status': 'ok', 'moderation_status': 'pending'|'published'|'none', ...}
"""
# Извлекаем логин из email (user_id@tcs.sibcem.ru -> user_id)
login = user_id.split('@')[0]
status_url = API_URL.replace("/upload/", "/status/")
try:
response = requests.get(
status_url,
params={"user_id": login},
headers={"X-API-Key": API_KEY},
timeout=30,
verify=False
)
response.raise_for_status()
return response.json()
except Exception as e:
logger.error(f"Ошибка проверки статуса модерации: {e}")
return {"status": "error", "reason": f"Ошибка проверки статуса: {str(e)}"}
def _upload_to_api(image_bytes: bytes, user_id: str, coords: tuple) -> dict:
"""
Отправляет фото и координаты в Django API.
@@ -122,6 +146,15 @@ def prepare_ad_photo(input_path: str, user_id: str, output_path: str = None, tar
output_path и target_size больше не используются (оставлены для обратной совместимости).
"""
try:
# Проверяем статус модерации перед отправкой
status = _check_moderation_status(user_id)
if status.get("status") == "ok" and status.get("moderation_status") == "pending":
photo_id = status.get("photo_id", "???")
return {
"success": False,
"error": f"У вас уже есть фото на модерации (ID: {photo_id}). Дождитесь решения модератора."
}
# 1. Читаем байты
if isinstance(input_path, bytes):
image_bytes = input_path