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

58 lines
1.8 KiB
Python
Raw Normal View History

2016-08-22 13:20:30 -04:00
from django.contrib.contenttypes.fields import GenericRelation
2016-07-26 14:58:37 -04:00
from django.db import models
from django.urls import reverse
from django.utils.encoding import python_2_unicode_compatible
2016-07-26 14:58:37 -04:00
2016-08-22 13:20:30 -04:00
from extras.models import CustomFieldModel, CustomFieldValue
2016-07-26 14:58:37 -04:00
from utilities.models import CreatedUpdatedModel
from utilities.utils import csv_format
2016-07-26 14:58:37 -04:00
@python_2_unicode_compatible
2016-07-26 14:58:37 -04:00
class TenantGroup(models.Model):
"""
An arbitrary collection of Tenants.
"""
name = models.CharField(max_length=50, unique=True)
slug = models.SlugField(unique=True)
class Meta:
ordering = ['name']
def __str__(self):
2016-07-26 14:58:37 -04:00
return self.name
def get_absolute_url(self):
return "{}?group={}".format(reverse('tenancy:tenant_list'), self.slug)
@python_2_unicode_compatible
2016-08-15 15:24:23 -04:00
class Tenant(CreatedUpdatedModel, CustomFieldModel):
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.
"""
2016-07-26 17:44:32 -04:00
name = models.CharField(max_length=30, unique=True)
2016-07-26 14:58:37 -04:00
slug = models.SlugField(unique=True)
group = models.ForeignKey('TenantGroup', related_name='tenants', blank=True, null=True, on_delete=models.SET_NULL)
2016-07-26 17:44:32 -04:00
description = models.CharField(max_length=100, blank=True, help_text="Long-form name (optional)")
2016-07-26 14:58:37 -04:00
comments = models.TextField(blank=True)
2016-08-22 13:20:30 -04:00
custom_field_values = GenericRelation(CustomFieldValue, content_type_field='obj_type', object_id_field='obj_id')
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.slug])
def to_csv(self):
return csv_format([
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,
2016-07-26 14:58:37 -04:00
])