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

265 lines
5.9 KiB
Python
Raw Normal View History

2021-10-18 15:09:57 -04:00
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
2021-10-18 11:45:05 -04:00
from django.contrib.contenttypes.models import ContentType
2016-07-26 14:58:37 -04:00
from django.db import models
from django.urls import reverse
from mptt.models import MPTTModel, TreeForeignKey
2016-07-26 14:58:37 -04:00
from extras.utils import extras_features
2021-10-18 11:45:05 -04:00
from netbox.models import ChangeLoggedModel, NestedGroupModel, OrganizationalModel, PrimaryModel
from utilities.querysets import RestrictedQuerySet
2021-10-18 11:45:05 -04:00
from .choices import *
2016-07-26 14:58:37 -04:00
__all__ = (
2021-10-18 11:45:05 -04:00
'ContactAssignment',
'Contact',
'ContactGroup',
'ContactRole',
'Tenant',
'TenantGroup',
)
2021-10-18 11:45:05 -04:00
#
# Tenants
#
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class TenantGroup(NestedGroupModel):
2016-07-26 14:58:37 -04:00
"""
An arbitrary collection of Tenants.
"""
2018-03-30 13:57:26 -04:00
name = models.CharField(
max_length=100,
2018-03-30 13:57:26 -04:00
unique=True
)
slug = models.SlugField(
max_length=100,
2018-03-30 13:57:26 -04:00
unique=True
)
parent = TreeForeignKey(
to='self',
on_delete=models.CASCADE,
related_name='children',
blank=True,
null=True,
db_index=True
)
description = models.CharField(
max_length=200,
blank=True
)
2016-07-26 14:58:37 -04:00
class Meta:
ordering = ['name']
def get_absolute_url(self):
return reverse('tenancy:tenantgroup', args=[self.pk])
2016-07-26 14:58:37 -04:00
2021-05-21 16:54:33 -04:00
@extras_features('custom_fields', 'custom_links', 'export_templates', 'tags', 'webhooks')
class Tenant(PrimaryModel):
2016-07-26 14:58:37 -04:00
"""
A Tenant represents an organization served by the NetBox owner. This is typically a customer or an internal
department.
"""
2018-03-30 13:57:26 -04:00
name = models.CharField(
max_length=100,
2018-03-30 13:57:26 -04:00
unique=True
)
slug = models.SlugField(
max_length=100,
2018-03-30 13:57:26 -04:00
unique=True
)
group = models.ForeignKey(
to='tenancy.TenantGroup',
on_delete=models.SET_NULL,
related_name='tenants',
blank=True,
null=True
)
description = models.CharField(
max_length=200,
blank=True
2018-03-30 13:57:26 -04:00
)
comments = models.TextField(
blank=True
)
2021-10-18 15:09:57 -04:00
# Generic relations
contacts = GenericRelation(
to='tenancy.ContactAssignment'
)
objects = RestrictedQuerySet.as_manager()
clone_fields = [
'group', 'description',
]
2016-07-26 14:58:37 -04:00
class Meta:
ordering = ['name']
2016-07-26 14:58:37 -04:00
def __str__(self):
2016-07-26 14:58:37 -04:00
return self.name
def get_absolute_url(self):
return reverse('tenancy:tenant', args=[self.pk])
2021-10-18 11:45:05 -04:00
#
# Contacts
#
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class ContactGroup(NestedGroupModel):
"""
An arbitrary collection of Contacts.
"""
name = models.CharField(
2021-10-18 15:41:29 -04:00
max_length=100
2021-10-18 11:45:05 -04:00
)
slug = models.SlugField(
2021-10-18 15:41:29 -04:00
max_length=100
2021-10-18 11:45:05 -04:00
)
parent = TreeForeignKey(
to='self',
on_delete=models.CASCADE,
related_name='children',
blank=True,
null=True,
db_index=True
)
description = models.CharField(
max_length=200,
blank=True
)
class Meta:
ordering = ['name']
2021-10-18 15:41:29 -04:00
unique_together = (
('parent', 'name')
)
2021-10-18 11:45:05 -04:00
def get_absolute_url(self):
return reverse('tenancy:contactgroup', args=[self.pk])
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
class ContactRole(OrganizationalModel):
"""
Functional role for a Contact assigned to an object.
"""
name = models.CharField(
max_length=100,
unique=True
)
slug = models.SlugField(
max_length=100,
unique=True
)
description = models.CharField(
max_length=200,
blank=True,
)
objects = RestrictedQuerySet.as_manager()
class Meta:
ordering = ['name']
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('tenancy:contactrole', args=[self.pk])
@extras_features('custom_fields', 'custom_links', 'export_templates', 'tags', 'webhooks')
class Contact(PrimaryModel):
"""
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(
max_length=100
)
title = models.CharField(
max_length=100,
blank=True
)
phone = models.CharField(
max_length=50,
blank=True
)
email = models.EmailField(
blank=True
)
address = models.CharField(
max_length=200,
blank=True
)
comments = models.TextField(
blank=True
)
objects = RestrictedQuerySet.as_manager()
clone_fields = [
'group',
]
class Meta:
ordering = ['name']
2021-10-18 15:41:29 -04:00
unique_together = (
('group', 'name')
)
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])
@extras_features('webhooks')
class ContactAssignment(ChangeLoggedModel):
content_type = models.ForeignKey(
to=ContentType,
on_delete=models.CASCADE
)
object_id = models.PositiveIntegerField()
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(
max_length=50,
choices=ContactPriorityChoices,
blank=True
)
objects = RestrictedQuerySet.as_manager()
class Meta:
ordering = ('priority', 'contact')
2021-10-18 16:20:31 -04:00
def __str__(self):
return f"{self.contact} ({self.get_priority_display()})" if self.priority else self.name