fix assigment

This commit is contained in:
IluaAir
2025-09-30 18:34:41 +03:00
parent edfa955edf
commit cc72c4619d

View File

@@ -1,6 +1,6 @@
import re import re
from tenancy.models import Tenant, TenantGroup, Contact, ContactRole from tenancy.models import Tenant, TenantGroup, Contact, ContactRole, ContactAssignment
from ipam.models import Prefix, VRF, Role from ipam.models import Prefix, VRF, Role
from extras.scripts import Script, StringVar, IPNetworkVar, ChoiceVar, ObjectVar from extras.scripts import Script, StringVar, IPNetworkVar, ChoiceVar, ObjectVar
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@@ -193,16 +193,10 @@ class CreateTenant(Script):
f"Создан префикс {prefix.prefix} для оператора: {tenant.name}" f"Создан префикс {prefix.prefix} для оператора: {tenant.name}"
) )
if data.get("contacts_fio"): if data.get("contacts_fio"):
try:
role = data.get("contacts_role", ContactRole.objects.first())
except Exception as e:
self.log_failure(f"Ошибка при получении роли контакта: {e}")
contact = Contact( contact = Contact(
name=data["contacts_fio"], name=data["contacts_fio"],
phone=data["contacts_phone"], phone=data["contacts_phone"],
email=data["contacts_email"], email=data["contacts_email"],
role=role,
tenant=tenant,
) )
try: try:
contact.full_clean() contact.full_clean()
@@ -210,6 +204,22 @@ class CreateTenant(Script):
self.log_success(f"Создан контакт: {contact.name}") self.log_success(f"Создан контакт: {contact.name}")
except Exception as e: except Exception as e:
self.log_failure(f"Ошибка при создании контакта: {e}") self.log_failure(f"Ошибка при создании контакта: {e}")
try:
role = data.get("contacts_role", ContactRole.objects.first())
assignment = ContactAssignment(
contact=contact,
role=role,
tenant=tenant,
)
assignment.full_clean()
assignment.save()
self.log_success(
f"Связан контакт {contact.name} с оператором: {tenant.name}"
)
except Exception as e:
self.log_failure(
f"Ошибка при связывании контакта с оператором: {e}"
)
else: else:
self.log_info( self.log_info(
@@ -230,10 +240,14 @@ class CreateTenant(Script):
name=data["contacts_fio"], name=data["contacts_fio"],
phone=data["contacts_phone"], phone=data["contacts_phone"],
email=data["contacts_email"], email=data["contacts_email"],
)
contact.full_clean()
assignment = ContactAssignment(
contact=contact,
role=data.get("contacts_role", ContactRole.objects.first()), role=data.get("contacts_role", ContactRole.objects.first()),
tenant=tenant, tenant=tenant,
) )
contact.full_clean() assignment.full_clean()
self.log_success("Все данные валидны") self.log_success("Все данные валидны")
except ValidationError as e: except ValidationError as e:
self.log_failure(f"Ошибка валидации: {e}") self.log_failure(f"Ошибка валидации: {e}")