fix: handle missing avatar files gracefully in signals, improve registration phone error, PATCH avatar empty handling
This commit is contained in:
parent
46a412d2f5
commit
a7aa6cc15b
@ -344,8 +344,25 @@ class RegisterAPIView(APIView):
|
||||
if not phone_norm:
|
||||
return Response({"ok": False, "error": "invalid_phone"}, status=400)
|
||||
|
||||
if Employee.objects.filter(phone_number=phone_norm).exists():
|
||||
return Response({"ok": False, "error": "phone_taken"}, status=400)
|
||||
existing_phone = Employee.objects.filter(phone_number=phone_norm).first()
|
||||
if existing_phone:
|
||||
logger.warning(
|
||||
"[REGISTER] Phone %s already used by user id=%s",
|
||||
phone_norm,
|
||||
existing_phone.id,
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
"ok": False,
|
||||
"error": "phone_taken",
|
||||
"detail": "Номер телефона уже зарегистрирован."
|
||||
" Войдите в существующий аккаунт или используйте другой номер.",
|
||||
"phone_number": [
|
||||
"Этот номер телефона уже привязан к другому аккаунту."
|
||||
],
|
||||
},
|
||||
status=400,
|
||||
)
|
||||
|
||||
user = Employee.objects.filter(email__iexact=email).first()
|
||||
if user:
|
||||
|
||||
@ -16,33 +16,33 @@ def compress_and_cleanup_avatar(sender, instance: Employee, **kwargs):
|
||||
# Импортируем здесь, чтобы избежать циклических импортов
|
||||
from common.image_utils import compress_avatar
|
||||
|
||||
# Проверяем, загружен ли новый файл аватара
|
||||
if instance.avatar and hasattr(instance.avatar, 'file'):
|
||||
try:
|
||||
# Читаем данные нового аватара
|
||||
instance.avatar.seek(0)
|
||||
original_data = instance.avatar.read()
|
||||
|
||||
# Сжимаем изображение
|
||||
compressed_data = compress_avatar(original_data)
|
||||
|
||||
# Если сжатие дало результат меньшего размера, заменяем
|
||||
if len(compressed_data) < len(original_data):
|
||||
# Получаем имя файла
|
||||
filename = instance.avatar.name.split('/')[-1]
|
||||
if not filename.lower().endswith('.jpg'):
|
||||
# Меняем расширение на .jpg
|
||||
filename = filename.rsplit('.', 1)[0] + '.jpg'
|
||||
|
||||
# Заменяем файл на сжатый
|
||||
instance.avatar.save(
|
||||
filename,
|
||||
ContentFile(compressed_data),
|
||||
save=False
|
||||
)
|
||||
except Exception as e:
|
||||
# Логируем ошибку, но не прерываем сохранение
|
||||
print(f"Error compressing avatar: {e}")
|
||||
avatar_field = instance.avatar
|
||||
avatar_name = getattr(avatar_field, "name", "")
|
||||
if avatar_field and avatar_name:
|
||||
storage = avatar_field.storage
|
||||
if storage.exists(avatar_name):
|
||||
try:
|
||||
avatar_field.open("rb")
|
||||
avatar_field.seek(0)
|
||||
original_data = avatar_field.read()
|
||||
|
||||
compressed_data = compress_avatar(original_data)
|
||||
|
||||
if len(compressed_data) < len(original_data):
|
||||
filename = avatar_name.split("/")[-1]
|
||||
if not filename.lower().endswith(".jpg"):
|
||||
filename = filename.rsplit(".", 1)[0] + ".jpg"
|
||||
|
||||
avatar_field.save(
|
||||
filename,
|
||||
ContentFile(compressed_data),
|
||||
save=False,
|
||||
)
|
||||
except FileNotFoundError:
|
||||
print(f"Avatar file missing during compression: {avatar_name}")
|
||||
except Exception as e:
|
||||
# Логируем ошибку, но не прерываем сохранение
|
||||
print(f"Error compressing avatar: {e}")
|
||||
|
||||
# Удаляем старый файл если он был заменен
|
||||
if instance.pk is None:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user