2021-10-18 15:09:57 -04:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.http import Http404
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
2016-08-03 14:24:09 -04:00
|
|
|
from circuits.models import Circuit
|
2017-11-15 12:54:49 -06:00
|
|
|
from dcim.models import Site, Rack, Device, RackReservation
|
2021-05-05 09:53:06 -04:00
|
|
|
from ipam.models import Aggregate, IPAddress, Prefix, VLAN, VRF
|
2020-11-11 16:07:38 -05:00
|
|
|
from netbox.views import generic
|
2021-03-26 15:07:29 -04:00
|
|
|
from utilities.tables import paginate_table
|
2021-10-18 15:30:28 -04:00
|
|
|
from utilities.utils import count_related
|
2019-10-07 08:29:32 +02:00
|
|
|
from virtualization.models import VirtualMachine, Cluster
|
2021-04-29 16:38:56 -04:00
|
|
|
from . import filtersets, forms, tables
|
2021-10-18 11:45:05 -04:00
|
|
|
from .models import *
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Tenant groups
|
|
|
|
#
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantGroupListView(generic.ObjectListView):
|
2020-03-11 20:20:04 -04:00
|
|
|
queryset = TenantGroup.objects.add_related_count(
|
|
|
|
TenantGroup.objects.all(),
|
|
|
|
Tenant,
|
|
|
|
'group',
|
|
|
|
'tenant_count',
|
|
|
|
cumulative=True
|
|
|
|
)
|
2021-07-20 15:39:58 -04:00
|
|
|
filterset = filtersets.TenantGroupFilterSet
|
|
|
|
filterset_form = forms.TenantGroupFilterForm
|
2016-07-26 14:58:37 -04:00
|
|
|
table = tables.TenantGroupTable
|
|
|
|
|
|
|
|
|
2021-03-26 15:07:29 -04:00
|
|
|
class TenantGroupView(generic.ObjectView):
|
|
|
|
queryset = TenantGroup.objects.all()
|
|
|
|
|
|
|
|
def get_extra_context(self, request, instance):
|
|
|
|
tenants = Tenant.objects.restrict(request.user, 'view').filter(
|
|
|
|
group=instance
|
|
|
|
)
|
2021-09-17 14:25:02 -04:00
|
|
|
tenants_table = tables.TenantTable(tenants, exclude=('group',))
|
2021-03-26 15:07:29 -04:00
|
|
|
paginate_table(tenants_table, request)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'tenants_table': tenants_table,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantGroupEditView(generic.ObjectEditView):
|
2020-05-11 12:37:22 -04:00
|
|
|
queryset = TenantGroup.objects.all()
|
2017-09-12 13:54:44 -04:00
|
|
|
model_form = forms.TenantGroupForm
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantGroupDeleteView(generic.ObjectDeleteView):
|
2020-07-01 12:08:26 -04:00
|
|
|
queryset = TenantGroup.objects.all()
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantGroupBulkImportView(generic.BulkImportView):
|
2020-05-21 11:58:27 -04:00
|
|
|
queryset = TenantGroup.objects.all()
|
2017-10-09 15:09:40 -04:00
|
|
|
model_form = forms.TenantGroupCSVForm
|
|
|
|
table = tables.TenantGroupTable
|
|
|
|
|
|
|
|
|
2021-03-12 16:14:42 -05:00
|
|
|
class TenantGroupBulkEditView(generic.BulkEditView):
|
|
|
|
queryset = TenantGroup.objects.add_related_count(
|
|
|
|
TenantGroup.objects.all(),
|
|
|
|
Tenant,
|
|
|
|
'group',
|
|
|
|
'tenant_count',
|
|
|
|
cumulative=True
|
|
|
|
)
|
2021-04-29 16:38:56 -04:00
|
|
|
filterset = filtersets.TenantGroupFilterSet
|
2021-03-12 16:14:42 -05:00
|
|
|
table = tables.TenantGroupTable
|
|
|
|
form = forms.TenantGroupBulkEditForm
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantGroupBulkDeleteView(generic.BulkDeleteView):
|
2020-07-20 12:07:19 -04:00
|
|
|
queryset = TenantGroup.objects.add_related_count(
|
|
|
|
TenantGroup.objects.all(),
|
|
|
|
Tenant,
|
|
|
|
'group',
|
|
|
|
'tenant_count',
|
|
|
|
cumulative=True
|
|
|
|
)
|
2017-07-13 17:39:28 -04:00
|
|
|
table = tables.TenantGroupTable
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Tenants
|
|
|
|
#
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantListView(generic.ObjectListView):
|
2020-10-30 16:52:40 -04:00
|
|
|
queryset = Tenant.objects.all()
|
2021-04-29 16:38:56 -04:00
|
|
|
filterset = filtersets.TenantFilterSet
|
2020-01-09 20:57:13 -05:00
|
|
|
filterset_form = forms.TenantFilterForm
|
2016-07-26 14:58:37 -04:00
|
|
|
table = tables.TenantTable
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantView(generic.ObjectView):
|
2020-05-21 15:39:07 -04:00
|
|
|
queryset = Tenant.objects.prefetch_related('group')
|
2017-05-18 17:00:57 -04:00
|
|
|
|
2020-11-19 15:59:11 -05:00
|
|
|
def get_extra_context(self, request, instance):
|
2017-05-18 17:00:57 -04:00
|
|
|
stats = {
|
2020-11-19 15:59:11 -05:00
|
|
|
'site_count': Site.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
|
|
|
'rack_count': Rack.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
|
|
|
'rackreservation_count': RackReservation.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
|
|
|
'device_count': Device.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
|
|
|
'vrf_count': VRF.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
|
|
|
'prefix_count': Prefix.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
2021-05-05 09:53:06 -04:00
|
|
|
'aggregate_count': Aggregate.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
2020-11-19 15:59:11 -05:00
|
|
|
'ipaddress_count': IPAddress.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
|
|
|
'vlan_count': VLAN.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
|
|
|
'circuit_count': Circuit.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
|
|
|
'virtualmachine_count': VirtualMachine.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
|
|
|
'cluster_count': Cluster.objects.restrict(request.user, 'view').filter(tenant=instance).count(),
|
2017-05-18 17:00:57 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:59:11 -05:00
|
|
|
return {
|
2017-05-18 17:00:57 -04:00
|
|
|
'stats': stats,
|
2020-11-19 15:59:11 -05:00
|
|
|
}
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantEditView(generic.ObjectEditView):
|
2020-05-11 12:37:22 -04:00
|
|
|
queryset = Tenant.objects.all()
|
2017-09-12 13:54:44 -04:00
|
|
|
model_form = forms.TenantForm
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantDeleteView(generic.ObjectDeleteView):
|
2020-05-11 12:47:01 -04:00
|
|
|
queryset = Tenant.objects.all()
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantBulkImportView(generic.BulkImportView):
|
2020-05-21 11:58:27 -04:00
|
|
|
queryset = Tenant.objects.all()
|
2017-05-31 17:40:11 -04:00
|
|
|
model_form = forms.TenantCSVForm
|
2016-07-26 14:58:37 -04:00
|
|
|
table = tables.TenantTable
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantBulkEditView(generic.BulkEditView):
|
2019-08-19 01:53:39 -04:00
|
|
|
queryset = Tenant.objects.prefetch_related('group')
|
2021-04-29 16:38:56 -04:00
|
|
|
filterset = filtersets.TenantFilterSet
|
2017-07-13 16:31:47 -04:00
|
|
|
table = tables.TenantTable
|
2016-07-26 14:58:37 -04:00
|
|
|
form = forms.TenantBulkEditForm
|
|
|
|
|
|
|
|
|
2020-11-11 16:07:38 -05:00
|
|
|
class TenantBulkDeleteView(generic.BulkDeleteView):
|
2019-08-19 01:53:39 -04:00
|
|
|
queryset = Tenant.objects.prefetch_related('group')
|
2021-04-29 16:38:56 -04:00
|
|
|
filterset = filtersets.TenantFilterSet
|
2017-07-13 17:39:28 -04:00
|
|
|
table = tables.TenantTable
|
2021-10-18 11:45:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Contact groups
|
|
|
|
#
|
|
|
|
|
|
|
|
class ContactGroupListView(generic.ObjectListView):
|
|
|
|
queryset = ContactGroup.objects.add_related_count(
|
|
|
|
ContactGroup.objects.all(),
|
|
|
|
Contact,
|
|
|
|
'group',
|
|
|
|
'contact_count',
|
|
|
|
cumulative=True
|
|
|
|
)
|
|
|
|
filterset = filtersets.ContactGroupFilterSet
|
|
|
|
filterset_form = forms.ContactGroupFilterForm
|
|
|
|
table = tables.ContactGroupTable
|
|
|
|
|
|
|
|
|
|
|
|
class ContactGroupView(generic.ObjectView):
|
|
|
|
queryset = ContactGroup.objects.all()
|
|
|
|
|
|
|
|
def get_extra_context(self, request, instance):
|
|
|
|
contacts = Contact.objects.restrict(request.user, 'view').filter(
|
|
|
|
group=instance
|
|
|
|
)
|
|
|
|
contacts_table = tables.ContactTable(contacts, exclude=('group',))
|
|
|
|
paginate_table(contacts_table, request)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'contacts_table': contacts_table,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ContactGroupEditView(generic.ObjectEditView):
|
|
|
|
queryset = ContactGroup.objects.all()
|
|
|
|
model_form = forms.ContactGroupForm
|
|
|
|
|
|
|
|
|
|
|
|
class ContactGroupDeleteView(generic.ObjectDeleteView):
|
|
|
|
queryset = ContactGroup.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class ContactGroupBulkImportView(generic.BulkImportView):
|
|
|
|
queryset = ContactGroup.objects.all()
|
|
|
|
model_form = forms.ContactGroupCSVForm
|
|
|
|
table = tables.ContactGroupTable
|
|
|
|
|
|
|
|
|
|
|
|
class ContactGroupBulkEditView(generic.BulkEditView):
|
|
|
|
queryset = ContactGroup.objects.add_related_count(
|
|
|
|
ContactGroup.objects.all(),
|
|
|
|
Contact,
|
|
|
|
'group',
|
|
|
|
'contact_count',
|
|
|
|
cumulative=True
|
|
|
|
)
|
|
|
|
filterset = filtersets.ContactGroupFilterSet
|
|
|
|
table = tables.ContactGroupTable
|
|
|
|
form = forms.ContactGroupBulkEditForm
|
|
|
|
|
|
|
|
|
|
|
|
class ContactGroupBulkDeleteView(generic.BulkDeleteView):
|
|
|
|
queryset = ContactGroup.objects.add_related_count(
|
|
|
|
ContactGroup.objects.all(),
|
|
|
|
Contact,
|
|
|
|
'group',
|
|
|
|
'contact_count',
|
|
|
|
cumulative=True
|
|
|
|
)
|
|
|
|
table = tables.ContactGroupTable
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Contact roles
|
|
|
|
#
|
|
|
|
|
|
|
|
class ContactRoleListView(generic.ObjectListView):
|
|
|
|
queryset = ContactRole.objects.all()
|
|
|
|
filterset = filtersets.ContactRoleFilterSet
|
|
|
|
filterset_form = forms.ContactRoleFilterForm
|
|
|
|
table = tables.ContactRoleTable
|
|
|
|
|
|
|
|
|
|
|
|
class ContactRoleView(generic.ObjectView):
|
|
|
|
queryset = ContactRole.objects.all()
|
|
|
|
|
|
|
|
def get_extra_context(self, request, instance):
|
|
|
|
contact_assignments = ContactAssignment.objects.restrict(request.user, 'view').filter(
|
|
|
|
role=instance
|
|
|
|
)
|
|
|
|
contacts_table = tables.ContactAssignmentTable(contact_assignments)
|
2021-10-18 15:30:28 -04:00
|
|
|
contacts_table.columns.hide('role')
|
2021-10-18 11:45:05 -04:00
|
|
|
paginate_table(contacts_table, request)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'contacts_table': contacts_table,
|
2021-10-18 15:30:28 -04:00
|
|
|
'assignment_count': ContactAssignment.objects.filter(role=instance).count(),
|
2021-10-18 11:45:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ContactRoleEditView(generic.ObjectEditView):
|
|
|
|
queryset = ContactRole.objects.all()
|
|
|
|
model_form = forms.ContactRoleForm
|
|
|
|
|
|
|
|
|
|
|
|
class ContactRoleDeleteView(generic.ObjectDeleteView):
|
|
|
|
queryset = ContactRole.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class ContactRoleBulkImportView(generic.BulkImportView):
|
|
|
|
queryset = ContactRole.objects.all()
|
|
|
|
model_form = forms.ContactRoleCSVForm
|
|
|
|
table = tables.ContactRoleTable
|
|
|
|
|
|
|
|
|
|
|
|
class ContactRoleBulkEditView(generic.BulkEditView):
|
|
|
|
queryset = ContactRole.objects.all()
|
|
|
|
filterset = filtersets.ContactRoleFilterSet
|
|
|
|
table = tables.ContactRoleTable
|
|
|
|
form = forms.ContactRoleBulkEditForm
|
|
|
|
|
|
|
|
|
|
|
|
class ContactRoleBulkDeleteView(generic.BulkDeleteView):
|
|
|
|
queryset = ContactRole.objects.all()
|
|
|
|
table = tables.ContactRoleTable
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Contacts
|
|
|
|
#
|
|
|
|
|
|
|
|
class ContactListView(generic.ObjectListView):
|
2021-10-18 15:30:28 -04:00
|
|
|
queryset = Contact.objects.annotate(
|
|
|
|
assignment_count=count_related(ContactAssignment, 'contact')
|
|
|
|
)
|
2021-10-18 11:45:05 -04:00
|
|
|
filterset = filtersets.ContactFilterSet
|
|
|
|
filterset_form = forms.ContactFilterForm
|
|
|
|
table = tables.ContactTable
|
|
|
|
|
|
|
|
|
|
|
|
class ContactView(generic.ObjectView):
|
|
|
|
queryset = Contact.objects.all()
|
|
|
|
|
2021-10-18 15:30:28 -04:00
|
|
|
def get_extra_context(self, request, instance):
|
|
|
|
contact_assignments = ContactAssignment.objects.restrict(request.user, 'view').filter(
|
|
|
|
contact=instance
|
|
|
|
)
|
|
|
|
contacts_table = tables.ContactAssignmentTable(contact_assignments)
|
|
|
|
contacts_table.columns.hide('contact')
|
|
|
|
paginate_table(contacts_table, request)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'contacts_table': contacts_table,
|
|
|
|
'assignment_count': ContactAssignment.objects.filter(contact=instance).count(),
|
|
|
|
}
|
|
|
|
|
2021-10-18 11:45:05 -04:00
|
|
|
|
|
|
|
class ContactEditView(generic.ObjectEditView):
|
|
|
|
queryset = Contact.objects.all()
|
|
|
|
model_form = forms.ContactForm
|
|
|
|
|
|
|
|
|
|
|
|
class ContactDeleteView(generic.ObjectDeleteView):
|
|
|
|
queryset = Contact.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class ContactBulkImportView(generic.BulkImportView):
|
|
|
|
queryset = Contact.objects.all()
|
|
|
|
model_form = forms.ContactCSVForm
|
|
|
|
table = tables.ContactTable
|
|
|
|
|
|
|
|
|
|
|
|
class ContactBulkEditView(generic.BulkEditView):
|
|
|
|
queryset = Contact.objects.prefetch_related('group')
|
|
|
|
filterset = filtersets.ContactFilterSet
|
|
|
|
table = tables.ContactTable
|
|
|
|
form = forms.ContactBulkEditForm
|
|
|
|
|
|
|
|
|
|
|
|
class ContactBulkDeleteView(generic.BulkDeleteView):
|
|
|
|
queryset = Contact.objects.prefetch_related('group')
|
|
|
|
filterset = filtersets.ContactFilterSet
|
|
|
|
table = tables.ContactTable
|
2021-10-18 15:09:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Contact assignments
|
|
|
|
#
|
|
|
|
|
|
|
|
class ContactAssignmentEditView(generic.ObjectEditView):
|
|
|
|
queryset = ContactAssignment.objects.all()
|
|
|
|
model_form = forms.ContactAssignmentForm
|
|
|
|
|
|
|
|
def alter_obj(self, instance, request, args, kwargs):
|
|
|
|
if not instance.pk:
|
|
|
|
# Assign the object based on URL kwargs
|
|
|
|
try:
|
|
|
|
app_label, model = request.GET.get('content_type').split('.')
|
|
|
|
except (AttributeError, ValueError):
|
|
|
|
raise Http404("Content type not specified")
|
|
|
|
content_type = get_object_or_404(ContentType, app_label=app_label, model=model)
|
|
|
|
instance.object = get_object_or_404(content_type.model_class(), pk=request.GET.get('object_id'))
|
|
|
|
return instance
|
|
|
|
|
|
|
|
def get_return_url(self, request, obj=None):
|
|
|
|
return obj.object.get_absolute_url() if obj else super().get_return_url(request)
|
|
|
|
|
|
|
|
|
|
|
|
class ContactAssignmentDeleteView(generic.ObjectDeleteView):
|
|
|
|
queryset = ContactAssignment.objects.all()
|
|
|
|
|
|
|
|
def get_return_url(self, request, obj=None):
|
|
|
|
return obj.object.get_absolute_url() if obj else super().get_return_url(request)
|