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

113 lines
2.6 KiB
Python
Raw Normal View History

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 mptt.models import MPTTModel, TreeForeignKey
2016-07-26 14:58:37 -04:00
from extras.utils import extras_features
from netbox.models import NestedGroupModel, PrimaryModel
from utilities.querysets import RestrictedQuerySet
2016-07-26 14:58:37 -04:00
__all__ = (
'Tenant',
'TenantGroup',
)
@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
csv_headers = ['name', 'slug', 'parent', 'description']
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
def to_csv(self):
return (
self.name,
self.slug,
self.parent.name if self.parent else '',
self.description,
)
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
)
objects = RestrictedQuerySet.as_manager()
csv_headers = ['name', 'slug', 'group', 'description', 'comments']
clone_fields = [
'group', 'description',
]
2016-07-26 14:58:37 -04:00
class Meta:
ordering = ['group', 'name']
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])
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,
self.group.name if self.group else None,
2016-07-27 11:29:20 -04:00
self.description,
self.comments,
2018-02-02 11:34:31 -05:00
)