<commit_before>import ipaddr

from django.db import models
from django.core.exceptions import ValidationError

from cyder.base.constants import IP_TYPES, IP_TYPE_4, IP_TYPE_6
from cyder.base.eav.constants import ATTRIBUTE_OPTION, ATTRIBUTE_STATEMENT
from cyder.base.eav.fields import EAVAttributeField
from cyder.base.eav.models import Attribute, EAVBase
from cyder.base.mixins import ObjectUrlMixin
from cyder.base.helpers import get_display
from cyder.base.models import BaseModel
from cyder.base.utils import safe_delete, safe_save
from cyder.cydhcp.constants import DYNAMIC
from cyder.cydhcp.utils import IPFilter, join_dhcp_args
from cyder.cydhcp.vlan.models import Vlan
from cyder.cydhcp.vrf.models import Vrf
from cyder.cydhcp.site.models import Site
from cyder.cydns.validation import validate_ip_type
from cyder.cydns.ip.models import ipv6_to_longs

# import reversion


class Network(BaseModel, ObjectUrlMixin):
    id = models.AutoField(primary_key=True)
    vlan = models.ForeignKey(Vlan, null=True,
                             blank=True, on_delete=models.SET_NULL)
    site = models.ForeignKey(Site, null=True,
                             blank=True, on_delete=models.SET_NULL)
    vrf = models.ForeignKey('cyder.Vrf',
                            default=lambda: Vrf.objects.get(name='Legacy'))

    # NETWORK/NETMASK FIELDS
    ip_type = models.CharField(
        verbose_name='IP address type', max_length=1,
        choices=IP_TYPES.items(), default=IP_TYPE_4,
        validators=[validate_ip_type]
    )
    ip_upper = models.BigIntegerField(null=False, blank=True)
    ip_lower = models.BigIntegerField(null=False, blank=True)
    # This field is here so ES can search this model easier.
    network_str = models.CharField(
        max_length=49, editable=True,
        help_text="The network address of this network.",
        verbose_name="Network address")
    prefixlen = models.PositiveIntegerField(
        null=False, help_text="The number of binary 1's in the netmask.")
    enabled = models.BooleanField(default=True)
    dhcpd_raw_include = models.TextField(
        blank=True,
        help_text="The config options in this box will be included "
                  "*as is* in the dhcpd.conf file for this subnet.")
    network = None

    search_fields = ('vlan__name', 'site__name', 'network_str')
    display_fields = ('network_str',)

    class Meta:
        app_label = 'cyder'
        db_table = 'network'
        unique_together = ('ip_upper', 'ip_lower', 'prefixlen')<commit_msg>Replace Vrf query (which could fail) with pk<commit_after>import ipaddr

from django.db import models
from django.core.exceptions import ValidationError

from cyder.base.constants import IP_TYPES, IP_TYPE_4, IP_TYPE_6
from cyder.base.eav.constants import ATTRIBUTE_OPTION, ATTRIBUTE_STATEMENT
from cyder.base.eav.fields import EAVAttributeField
from cyder.base.eav.models import Attribute, EAVBase
from cyder.base.mixins import ObjectUrlMixin
from cyder.base.helpers import get_display
from cyder.base.models import BaseModel
from cyder.base.utils import safe_delete, safe_save
from cyder.cydhcp.constants import DYNAMIC
from cyder.cydhcp.utils import IPFilter, join_dhcp_args
from cyder.cydhcp.vlan.models import Vlan
from cyder.cydhcp.vrf.models import Vrf
from cyder.cydhcp.site.models import Site
from cyder.cydns.validation import validate_ip_type
from cyder.cydns.ip.models import ipv6_to_longs

# import reversion


class Network(BaseModel, ObjectUrlMixin):
    id = models.AutoField(primary_key=True)
    vlan = models.ForeignKey(Vlan, null=True,
                             blank=True, on_delete=models.SET_NULL)
    site = models.ForeignKey(Site, null=True,
                             blank=True, on_delete=models.SET_NULL)
    vrf = models.ForeignKey('cyder.Vrf',
                            default=1)  # "Legacy"

    # NETWORK/NETMASK FIELDS
    ip_type = models.CharField(
        verbose_name='IP address type', max_length=1,
        choices=IP_TYPES.items(), default=IP_TYPE_4,
        validators=[validate_ip_type]
    )
    ip_upper = models.BigIntegerField(null=False, blank=True)
    ip_lower = models.BigIntegerField(null=False, blank=True)
    # This field is here so ES can search this model easier.
    network_str = models.CharField(
        max_length=49, editable=True,
        help_text="The network address of this network.",
        verbose_name="Network address")
    prefixlen = models.PositiveIntegerField(
        null=False, help_text="The number of binary 1's in the netmask.")
    enabled = models.BooleanField(default=True)
    dhcpd_raw_include = models.TextField(
        blank=True,
        help_text="The config options in this box will be included "
                  "*as is* in the dhcpd.conf file for this subnet.")
    network = None

    search_fields = ('vlan__name', 'site__name', 'network_str')
    display_fields = ('network_str',)

    class Meta:
        app_label = 'cyder'
        db_table = 'network'
        unique_together = ('ip_upper', 'ip_lower', 'prefixlen')
{'fraction_non_alphanumeric': 0.08624107850911975, 'fraction_numerical': 0.0033703409992069788, 'mean_word_length': 3.528725314183124, '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': '2950404', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1575, 'n_words': 384}