Corp/backend/users/signals.py
Игорь Надеин 6fb2cdcea0 feat: enhance Employee model and integrate Telegram/WhatsApp bot
- Add birth_date, created_at/updated_at to Employee; remove username field
- Introduce EmployeeAction for tracking HR events and is_actually_active/status properties
- Create BotSubscriber model (OneToOne relation) for TG/WA binding
- Implement Aiogram-based Telegram bot:
  • request contact to bind by phone or @username
  • department selection keyboard from DEPARTMENTS dict
  • async ORM via sync_to_async, custom filters for bound & active employees
- Update profile view & template:
  • show “Bind Telegram/WhatsApp” button only when not already bound
  • preview avatar on upload
- Remove registration tokens in favor of contact-based auth
- Clean up templates, forms and add avatar validation mixin
2025-07-30 08:15:53 +03:00

45 lines
1.6 KiB
Python

# users/signals.py
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
from django.utils import timezone
from .models import Employee, EmployeeAction
from bots.models import BotSubscriber
@receiver(pre_save, sender=Employee)
def unbind_bot_subscriber_on_contact_change(sender, instance: Employee, **kwargs):
# Игнорируем новых пользователей
if instance.pk is None:
return
old = sender.objects.get(pk=instance.pk)
try:
sub = old.bot_subscription
except BotSubscriber.DoesNotExist:
return
# Если очистили или изменили telegram в профиле — сбросим telegram_id
if old.telegram and old.telegram != (instance.telegram or "").strip():
sub.telegram_id = None
sub.save(update_fields=['telegram_id'])
# Аналогично для whatsapp
if old.whatsapp and old.whatsapp != instance.whatsapp:
sub.whatsapp_id = None
sub.save(update_fields=['whatsapp_id'])
@receiver(post_save, sender=Employee)
def create_hired_action(sender, instance: Employee, created, **kwargs):
"""
При первом сохранении нового сотрудника создаём событие «Принят».
"""
if created:
EmployeeAction.objects.create(
employee=instance,
action=EmployeeAction.ACTION_HIRED,
date=instance.created_at or timezone.now(),
comment='Автоматически: принят при регистрации'
)