igor_nadein a590acb762 feat(frontend): добавлен API клиент и восстановлен backend из master
- Создан frontend/src/lib/api.ts с 43 методами для всех API endpoint'ов
- Создан frontend/src/lib/permissions.ts для проверки прав доступа
- Исправлен URL в getChatMessagesAround: /messages/around/ → /messages-around/
- Исправлен параметр: message_id → around_id
- Восстановлен backend из ветки master для стабильности
- Удалены конфликтующие файлы Pages Router (src/pages, App.tsx, providers)
- Оставлена только чистая структура Next.js App Router
- Добавлена документация по миграции API v2
2026-02-24 15:40:50 +03:00

51 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import base64
from PIL import Image
from io import BytesIO
import random
import string
def gen_person_code(instance):
import re
base = str(instance.phone_number or instance.email or instance.pk)
code = re.sub(r'\W+', '', base)[:16]
# Проверяем уникальность, если не уникален — добавляем символы в конец
from .models import HikPerson
existing = (
HikPerson.objects.using('hikcentral')
.filter(person_code=code)
.exists()
)
orig_code = code
while existing:
# Укорачиваем до 12 символов и дописываем случайные 4 символа
code = (
orig_code[:12] + ''.join(random.choices(string.ascii_letters + string.digits, k=4)))[:16]
existing = (
HikPerson.objects.using('hikcentral')
.filter(person_code=code)
.exists()
)
return code
def compress_image_to_base64(image_field, target_size_kb=200):
"""Сжимает изображение до размера (примерно) target_size_kb и возвращает base64-строку."""
if not image_field:
return ''
image = Image.open(image_field)
output = BytesIO()
quality = 90
# Сжимать до ~200КБ
while True:
output.seek(0)
image.save(output, format='JPEG', quality=quality)
size_kb = output.tell() / 1024
if size_kb <= target_size_kb or quality <= 20:
break
quality -= 10
output.seek(0)
img_b64 = base64.b64encode(output.read()).decode('utf-8')
return img_b64