2026-08-25 13:28:43 +03:00

28 lines
1.1 KiB
Python
Raw Permalink 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.

"""Синхронизация обычной M2M-связи навыков с игровым прогрессом."""
from django.db.models.signals import m2m_changed
from django.dispatch import receiver
from employees.models import Employee, EmployeeSkillProgress
@receiver(m2m_changed, sender=Employee.skills.through)
def sync_employee_skill_progress(sender, instance, action, pk_set, **kwargs):
if action == "post_add":
for skill_id in pk_set or ():
progress, _ = EmployeeSkillProgress.objects.get_or_create(
employee=instance,
skill_id=skill_id,
defaults={"is_active": True},
)
if not progress.is_active:
progress.is_active = True
progress.save(update_fields=["is_active", "updated_at"])
elif action == "post_remove":
EmployeeSkillProgress.objects.filter(
employee=instance,
skill_id__in=pk_set or (),
).update(is_active=False)
elif action == "pre_clear":
EmployeeSkillProgress.objects.filter(employee=instance).update(is_active=False)