1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
Files
netbox-community-netbox/netbox/tenancy/models/contacts.py

177 lines
4.9 KiB
Python
Raw Normal View History

from django.contrib.contenttypes.fields import GenericForeignKey
from django.core.exceptions import ValidationError
2016-07-26 14:58:37 -04:00
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
2016-07-26 14:58:37 -04:00
from core.models import ContentType
from netbox.models import ChangeLoggedModel, NestedGroupModel, OrganizationalModel, PrimaryModel
2023-11-29 20:25:44 -05:00
from netbox.models.features import CustomFieldsMixin, ExportTemplatesMixin, TagsMixin
from tenancy.choices import *
2016-07-26 14:58:37 -04:00
__all__ = (
2021-10-18 11:45:05 -04:00
'ContactAssignment',
'Contact',
'ContactGroup',
'ContactRole',
)
2021-10-18 11:45:05 -04:00
class ContactGroup(NestedGroupModel):
"""
An arbitrary collection of Contacts.
"""
class Meta:
ordering = ['name']
constraints = (
models.UniqueConstraint(
fields=('parent', 'name'),
name='%(app_label)s_%(class)s_unique_parent_name'
),
2021-10-18 15:41:29 -04:00
)
verbose_name = _('contact group')
verbose_name_plural = _('contact groups')
2021-10-18 11:45:05 -04:00
def get_absolute_url(self):
return reverse('tenancy:contactgroup', args=[self.pk])
class ContactRole(OrganizationalModel):
"""
Functional role for a Contact assigned to an object.
"""
def get_absolute_url(self):
return reverse('tenancy:contactrole', args=[self.pk])
class Meta:
ordering = ('name',)
verbose_name = _('contact role')
verbose_name_plural = _('contact roles')
2021-10-18 11:45:05 -04:00
class Contact(PrimaryModel):
2021-10-18 11:45:05 -04:00
"""
Contact information for a particular object(s) in NetBox.
"""
group = models.ForeignKey(
to='tenancy.ContactGroup',
on_delete=models.SET_NULL,
related_name='contacts',
blank=True,
null=True
)
name = models.CharField(
verbose_name=_('name'),
2021-10-18 11:45:05 -04:00
max_length=100
)
title = models.CharField(
verbose_name=_('title'),
2021-10-18 11:45:05 -04:00
max_length=100,
blank=True
)
phone = models.CharField(
verbose_name=_('phone'),
2021-10-18 11:45:05 -04:00
max_length=50,
blank=True
)
email = models.EmailField(
verbose_name=_('email'),
2021-10-18 11:45:05 -04:00
blank=True
)
address = models.CharField(
verbose_name=_('address'),
2021-10-18 11:45:05 -04:00
max_length=200,
blank=True
)
link = models.URLField(
verbose_name=_('link'),
blank=True
)
2021-10-18 11:45:05 -04:00
clone_fields = (
'group', 'name', 'title', 'phone', 'email', 'address', 'link',
)
2021-10-18 11:45:05 -04:00
class Meta:
ordering = ['name']
constraints = (
models.UniqueConstraint(
fields=('group', 'name'),
name='%(app_label)s_%(class)s_unique_group_name'
),
2021-10-18 15:41:29 -04:00
)
verbose_name = _('contact')
verbose_name_plural = _('contacts')
2021-10-18 11:45:05 -04:00
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('tenancy:contact', args=[self.pk])
2023-11-29 20:25:44 -05:00
class ContactAssignment(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLoggedModel):
2021-10-18 11:45:05 -04:00
content_type = models.ForeignKey(
to='contenttypes.ContentType',
2021-10-18 11:45:05 -04:00
on_delete=models.CASCADE
)
object_id = models.PositiveBigIntegerField()
2021-10-18 11:45:05 -04:00
object = GenericForeignKey(
ct_field='content_type',
fk_field='object_id'
)
contact = models.ForeignKey(
to='tenancy.Contact',
on_delete=models.PROTECT,
related_name='assignments'
)
role = models.ForeignKey(
to='tenancy.ContactRole',
on_delete=models.PROTECT,
related_name='assignments'
)
priority = models.CharField(
verbose_name=_('priority'),
2021-10-18 11:45:05 -04:00
max_length=50,
choices=ContactPriorityChoices,
blank=True
)
clone_fields = ('content_type', 'object_id', 'role', 'priority')
2021-10-18 11:45:05 -04:00
class Meta:
ordering = ('priority', 'contact')
indexes = (
models.Index(fields=('content_type', 'object_id')),
)
constraints = (
models.UniqueConstraint(
fields=('content_type', 'object_id', 'contact', 'role'),
name='%(app_label)s_%(class)s_unique_object_contact_role'
),
)
verbose_name = _('contact assignment')
verbose_name_plural = _('contact assignments')
2021-10-18 16:20:31 -04:00
def __str__(self):
2021-10-18 16:47:49 -04:00
if self.priority:
return f"{self.contact} ({self.get_priority_display()}) -> {self.object}"
return str(f"{self.contact} -> {self.object}")
def get_absolute_url(self):
return reverse('tenancy:contact', args=[self.contact.pk])
def clean(self):
super().clean()
# Validate the assigned object type
if self.content_type not in ContentType.objects.with_feature('contacts'):
raise ValidationError(
_("Contacts cannot be assigned to this object type ({type}).").format(type=self.content_type)
)
def to_objectchange(self, action):
objectchange = super().to_objectchange(action)
objectchange.related_object = self.object
return objectchange