2021-04-11 12:57:53 -04:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-07-26 14:58:37 -04:00
|
|
|
from django.db import models
|
2017-04-05 14:40:25 -04:00
|
|
|
from django.urls import reverse
|
2020-03-11 20:20:04 -04:00
|
|
|
from mptt.models import MPTTModel, TreeForeignKey
|
2016-07-26 14:58:37 -04:00
|
|
|
|
2020-03-14 03:03:22 -04:00
|
|
|
from extras.utils import extras_features
|
2021-02-24 21:01:16 -05:00
|
|
|
from netbox.models import NestedGroupModel, PrimaryModel
|
2020-05-29 16:27:36 -04:00
|
|
|
from utilities.querysets import RestrictedQuerySet
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2020-01-14 12:01:23 -05:00
|
|
|
__all__ = (
|
|
|
|
'Tenant',
|
|
|
|
'TenantGroup',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-06 13:01:20 -04:00
|
|
|
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
|
2021-02-24 21:01:16 -05:00
|
|
|
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(
|
2020-10-15 15:37:34 -04:00
|
|
|
max_length=100,
|
2018-03-30 13:57:26 -04:00
|
|
|
unique=True
|
|
|
|
)
|
|
|
|
slug = models.SlugField(
|
2020-10-15 15:37:34 -04:00
|
|
|
max_length=100,
|
2018-03-30 13:57:26 -04:00
|
|
|
unique=True
|
|
|
|
)
|
2020-03-11 20:20:04 -04:00
|
|
|
parent = TreeForeignKey(
|
|
|
|
to='self',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='children',
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
db_index=True
|
|
|
|
)
|
2020-03-13 16:24:37 -04:00
|
|
|
description = models.CharField(
|
|
|
|
max_length=200,
|
|
|
|
blank=True
|
|
|
|
)
|
2016-07-26 14:58:37 -04:00
|
|
|
|
2020-03-13 16:24:37 -04:00
|
|
|
csv_headers = ['name', 'slug', 'parent', 'description']
|
2018-05-30 11:19:10 -04:00
|
|
|
|
2016-07-26 14:58:37 -04:00
|
|
|
class Meta:
|
|
|
|
ordering = ['name']
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2021-03-26 15:07:29 -04:00
|
|
|
return reverse('tenancy:tenantgroup', args=[self.pk])
|
2016-07-26 14:58:37 -04:00
|
|
|
|
2018-02-02 14:26:16 -05:00
|
|
|
def to_csv(self):
|
|
|
|
return (
|
|
|
|
self.name,
|
|
|
|
self.slug,
|
2020-03-11 20:20:04 -04:00
|
|
|
self.parent.name if self.parent else '',
|
2020-03-13 16:24:37 -04:00
|
|
|
self.description,
|
2020-03-11 20:20:04 -04:00
|
|
|
)
|
|
|
|
|
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')
|
2021-02-24 21:01:16 -05:00
|
|
|
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(
|
2020-10-15 15:37:34 -04:00
|
|
|
max_length=100,
|
2018-03-30 13:57:26 -04:00
|
|
|
unique=True
|
|
|
|
)
|
|
|
|
slug = models.SlugField(
|
2020-10-15 15:37:34 -04:00
|
|
|
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(
|
2020-03-13 15:49:58 -04:00
|
|
|
max_length=200,
|
|
|
|
blank=True
|
2018-03-30 13:57:26 -04:00
|
|
|
)
|
|
|
|
comments = models.TextField(
|
|
|
|
blank=True
|
|
|
|
)
|
2018-05-10 12:53:11 -04:00
|
|
|
|
2020-05-29 16:27:36 -04:00
|
|
|
objects = RestrictedQuerySet.as_manager()
|
|
|
|
|
2018-06-13 15:40:16 -04:00
|
|
|
csv_headers = ['name', 'slug', 'group', 'description', 'comments']
|
2019-12-06 16:13:52 -05:00
|
|
|
clone_fields = [
|
|
|
|
'group', 'description',
|
|
|
|
]
|
2018-05-30 11:19:10 -04:00
|
|
|
|
2016-07-26 14:58:37 -04:00
|
|
|
class Meta:
|
|
|
|
ordering = ['group', 'name']
|
|
|
|
|
2017-01-23 22:44:29 +01:00
|
|
|
def __str__(self):
|
2016-07-26 14:58:37 -04:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2021-02-26 17:23:23 -05:00
|
|
|
return reverse('tenancy:tenant', args=[self.pk])
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
def to_csv(self):
|
2018-02-02 11:34:31 -05:00
|
|
|
return (
|
2016-07-26 14:58:37 -04:00
|
|
|
self.name,
|
|
|
|
self.slug,
|
2017-01-04 10:47:00 -05:00
|
|
|
self.group.name if self.group else None,
|
2016-07-27 11:29:20 -04:00
|
|
|
self.description,
|
2018-02-02 14:26:16 -05:00
|
|
|
self.comments,
|
2018-02-02 11:34:31 -05:00
|
|
|
)
|