<commit_before>from django.contrib.auth.mixins import PermissionRequiredMixin
from django.db.models import Count
from django.shortcuts import get_object_or_404, render

from circuits.models import Circuit
from dcim.models import Site, Rack, Device
from ipam.models import IPAddress, Prefix, VLAN, VRF
from utilities.views import (
    BulkDeleteView, BulkEditView, BulkImportView, ObjectDeleteView, ObjectEditView, ObjectListView,
)

from models import Tenant, TenantGroup
from . import filters, forms, tables


#
# Tenant groups
#

class TenantGroupListView(ObjectListView):
    queryset = TenantGroup.objects.annotate(tenant_count=Count('tenants'))
    table = tables.TenantGroupTable
    edit_permissions = ['tenancy.change_tenantgroup', 'tenancy.delete_tenantgroup']
    template_name = 'tenancy/tenantgroup_list.html'


class TenantGroupEditView(PermissionRequiredMixin, ObjectEditView):
    permission_required = 'tenancy.change_tenantgroup'
    model = TenantGroup
    form_class = forms.TenantGroupForm
    success_url = 'tenancy:tenantgroup_list'
    cancel_url = 'tenancy:tenantgroup_list'


class TenantGroupBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
    permission_required = 'tenancy.delete_tenantgroup'
    cls = TenantGroup
    default_redirect_url = 'tenancy:tenantgroup_list'


#
#  Tenants
#

class TenantListView(ObjectListView):
    queryset = Tenant.objects.select_related('group')
    filter = filters.TenantFilter
    filter_form = forms.TenantFilterForm
    table = tables.TenantTable
    edit_permissions = ['tenancy.change_tenant', 'tenancy.delete_tenant']
    template_name = 'tenancy/tenant_list.html'


def tenant(request, slug):

    tenant = get_object_or_404(Tenant, slug=slug)
    stats = {
        'site_count': Site.objects.filter(tenant=tenant).count(),
        'rack_count': Rack.objects.filter(tenant=tenant).count(),
        'device_count': Device.objects.filter(tenant=tenant).count(),
        'vrf_count': VRF.objects.filter(tenant=tenant).count(),
        'prefix_count': Prefix.objects.filter(tenant=tenant).count(),
        'ipaddress_count': IPAddress.objects.filter(tenant=tenant).count(),
        'vlan_count': VLAN.objects.filter(tenant=tenant).count(),
        'circuit_count': Circuit.objects.filter(tenant=tenant).count(),
    }

    return render(request, 'tenancy/tenant.html', {
        'tenant': tenant,
        'stats': stats,
    })


class TenantEditView(PermissionRequiredMixin, ObjectEditView):
    permission_required = 'tenancy.change_tenant'
    model = Tenant
    form_class = forms.TenantForm
    fields_initial = ['group']
    template_name = 'tenancy/tenant_edit.html'
    cancel_url = 'tenancy:tenant_list'


class TenantDeleteView(PermissionRequiredMixin, ObjectDeleteView):
    permission_required = 'tenancy.delete_tenant'
    model = Tenant
    redirect_url = 'tenancy:tenant_list'


class TenantBulkImportView(PermissionRequiredMixin, BulkImportView):
    permission_required = 'tenancy.add_tenant'
    form = forms.TenantImportForm
    table = tables.TenantTable
    template_name = 'tenancy/tenant_import.html'
    obj_list_url = 'tenancy:tenant_list'


class TenantBulkEditView(PermissionRequiredMixin, BulkEditView):
    permission_required = 'tenancy.change_tenant'
    cls = Tenant
    form = forms.TenantBulkEditForm
    template_name = 'tenancy/tenant_bulk_edit.html'
    default_redirect_url = 'tenancy:tenant_list'

    def update_objects(self, pk_list, form):

        fields_to_update = {}
        if form.cleaned_data['group'] == 0:
            fields_to_update['group'] = None
        elif form.cleaned_data['group']:
            fields_to_update['group'] = form.cleaned_data['group']

        return self.cls.objects.filter(pk__in=pk_list).update(**fields_to_update)


class TenantBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
    permission_required = 'tenancy.delete_tenant'
    cls = Tenant
    default_redirect_url = 'tenancy:tenant_list'
<commit_msg>Fixes #467: Include prefixes and IPs which inherit tenancy from their VRF in tenant stats<commit_after>from django.contrib.auth.mixins import PermissionRequiredMixin
from django.db.models import Count, Q
from django.shortcuts import get_object_or_404, render

from circuits.models import Circuit
from dcim.models import Site, Rack, Device
from ipam.models import IPAddress, Prefix, VLAN, VRF
from utilities.views import (
    BulkDeleteView, BulkEditView, BulkImportView, ObjectDeleteView, ObjectEditView, ObjectListView,
)

from models import Tenant, TenantGroup
from . import filters, forms, tables


#
# Tenant groups
#

class TenantGroupListView(ObjectListView):
    queryset = TenantGroup.objects.annotate(tenant_count=Count('tenants'))
    table = tables.TenantGroupTable
    edit_permissions = ['tenancy.change_tenantgroup', 'tenancy.delete_tenantgroup']
    template_name = 'tenancy/tenantgroup_list.html'


class TenantGroupEditView(PermissionRequiredMixin, ObjectEditView):
    permission_required = 'tenancy.change_tenantgroup'
    model = TenantGroup
    form_class = forms.TenantGroupForm
    success_url = 'tenancy:tenantgroup_list'
    cancel_url = 'tenancy:tenantgroup_list'


class TenantGroupBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
    permission_required = 'tenancy.delete_tenantgroup'
    cls = TenantGroup
    default_redirect_url = 'tenancy:tenantgroup_list'


#
#  Tenants
#

class TenantListView(ObjectListView):
    queryset = Tenant.objects.select_related('group')
    filter = filters.TenantFilter
    filter_form = forms.TenantFilterForm
    table = tables.TenantTable
    edit_permissions = ['tenancy.change_tenant', 'tenancy.delete_tenant']
    template_name = 'tenancy/tenant_list.html'


def tenant(request, slug):

    tenant = get_object_or_404(Tenant, slug=slug)
    stats = {
        'site_count': Site.objects.filter(tenant=tenant).count(),
        'rack_count': Rack.objects.filter(tenant=tenant).count(),
        'device_count': Device.objects.filter(tenant=tenant).count(),
        'vrf_count': VRF.objects.filter(tenant=tenant).count(),
        'prefix_count': Prefix.objects.filter(
            Q(tenant=tenant) |
            Q(tenant__isnull=True, vrf__tenant=tenant)
        ).count(),
        'ipaddress_count': IPAddress.objects.filter(
            Q(tenant=tenant) |
            Q(tenant__isnull=True, vrf__tenant=tenant)
        ).count(),
        'vlan_count': VLAN.objects.filter(tenant=tenant).count(),
        'circuit_count': Circuit.objects.filter(tenant=tenant).count(),
    }

    return render(request, 'tenancy/tenant.html', {
        'tenant': tenant,
        'stats': stats,
    })


class TenantEditView(PermissionRequiredMixin, ObjectEditView):
    permission_required = 'tenancy.change_tenant'
    model = Tenant
    form_class = forms.TenantForm
    fields_initial = ['group']
    template_name = 'tenancy/tenant_edit.html'
    cancel_url = 'tenancy:tenant_list'


class TenantDeleteView(PermissionRequiredMixin, ObjectDeleteView):
    permission_required = 'tenancy.delete_tenant'
    model = Tenant
    redirect_url = 'tenancy:tenant_list'


class TenantBulkImportView(PermissionRequiredMixin, BulkImportView):
    permission_required = 'tenancy.add_tenant'
    form = forms.TenantImportForm
    table = tables.TenantTable
    template_name = 'tenancy/tenant_import.html'
    obj_list_url = 'tenancy:tenant_list'


class TenantBulkEditView(PermissionRequiredMixin, BulkEditView):
    permission_required = 'tenancy.change_tenant'
    cls = Tenant
    form = forms.TenantBulkEditForm
    template_name = 'tenancy/tenant_bulk_edit.html'
    default_redirect_url = 'tenancy:tenant_list'

    def update_objects(self, pk_list, form):

        fields_to_update = {}
        if form.cleaned_data['group'] == 0:
            fields_to_update['group'] = None
        elif form.cleaned_data['group']:
            fields_to_update['group'] = form.cleaned_data['group']

        return self.cls.objects.filter(pk__in=pk_list).update(**fields_to_update)


class TenantBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
    permission_required = 'tenancy.delete_tenant'
    cls = Tenant
    default_redirect_url = 'tenancy:tenant_list'

{'fraction_non_alphanumeric': 0.09915637608509598, 'fraction_numerical': 0.002078493703386722, 'mean_word_length': 4.512129380053908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4379605', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2561, 'n_words': 433}