<commit_before>from copy import deepcopy

from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.http import Http404
from django.db.models import Q, F

from cyder.cydhcp.interface.static_intr.models import StaticInterface
from cyder.cydns.address_record.models import AddressRecord
from cyder.cydns.cname.models import CNAME
from cyder.cydns.domain.models import Domain
from cyder.cydns.mx.models import MX
from cyder.cydns.srv.models import SRV
from cyder.cydns.txt.models import TXT
from cyder.cydns.sshfp.models import SSHFP
from cyder.cydns.view.models import View


def slim_form(domain_pk=None, form=None):
    """ What is going on? We want only one domain showing up in the
    choices.  We are replacing the query set with just one object. Ther
    are two querysets. I'm not really sure what the first one does, but
    I know the second one (the widget) removes the choices. The third
    line removes the default u'--------' choice from the drop down.  """
    if not form:
        raise Http404
    if domain_pk:
        query_set = Domain.objects.filter(id=domain_pk)
        if not query_set:
            raise Http404
        if not form.fields.get('domain', False):
            raise Http404
        form.fields['domain']._queryset = query_set
        form.fields['domain'].widget.choices.queryset = query_set
        form.fields['domain'].empty_label = None
    return form


def get_clobbered(domain_name, **kwargs):
    classes = [MX, AddressRecord, CNAME, TXT, SRV, StaticInterface, SSHFP]
    clobber_objects = []  # Objects that have the same name as a domain
    for Klass in classes:
        objs = Klass.objects.filter(fqdn=domain_name)
        for obj in objs:
            obj_views = [view.name for view in obj.views.all()]
            new_obj = deepcopy(obj)
            new_obj.id = None
            new_obj.label = ""
            clobber_objects.append((new_obj, obj_views))
            if Klass == AddressRecord:
                kwargs["check_cname"] = False
                kwargs["call_prune_tree"] = False

            else:
                kwargs["call_prune_tree"] = False

            # We have to be careful here to not delete any domains due to them
            # being pruneable and not having any records or child domains. We
            # set the call_prune_tree flag to tell the object's delete function
            # to skip calling prune_tree
            if obj.__class__ in [StaticInterface, AddressRecord]:
                obj.delete(**kwargs)
            else:
                obj.delete()<commit_msg>Fixed some test errors due to bad kwarg logic<commit_after>from copy import deepcopy

from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.http import Http404
from django.db.models import Q, F

from cyder.cydhcp.interface.static_intr.models import StaticInterface
from cyder.cydns.address_record.models import AddressRecord
from cyder.cydns.cname.models import CNAME
from cyder.cydns.domain.models import Domain
from cyder.cydns.mx.models import MX
from cyder.cydns.srv.models import SRV
from cyder.cydns.txt.models import TXT
from cyder.cydns.sshfp.models import SSHFP
from cyder.cydns.view.models import View


def slim_form(domain_pk=None, form=None):
    """ What is going on? We want only one domain showing up in the
    choices.  We are replacing the query set with just one object. Ther
    are two querysets. I'm not really sure what the first one does, but
    I know the second one (the widget) removes the choices. The third
    line removes the default u'--------' choice from the drop down.  """
    if not form:
        raise Http404
    if domain_pk:
        query_set = Domain.objects.filter(id=domain_pk)
        if not query_set:
            raise Http404
        if not form.fields.get('domain', False):
            raise Http404
        form.fields['domain']._queryset = query_set
        form.fields['domain'].widget.choices.queryset = query_set
        form.fields['domain'].empty_label = None
    return form


def get_clobbered(domain_name, **kwargs):
    classes = [MX, AddressRecord, CNAME, TXT, SRV, StaticInterface, SSHFP]
    clobber_objects = []  # Objects that have the same name as a domain
    for Klass in classes:
        objs = Klass.objects.filter(fqdn=domain_name)
        for obj in objs:
            obj_views = [view.name for view in obj.views.all()]
            new_obj = deepcopy(obj)
            new_obj.id = None
            new_obj.label = ""
            clobber_objects.append((new_obj, obj_views))
            if Klass == AddressRecord:
                kwargs["check_cname"] = False
                kwargs["call_prune_tree"] = False

            else:
                kwargs["call_prune_tree"] = False

            # We have to be careful here to not delete any domains due to them
            # being pruneable and not having any records or child domains. We
            # set the call_prune_tree flag to tell the object's delete function
            # to skip calling prune_tree
            if obj.__class__ not in [StaticInterface, AddressRecord]:
                kwargs.pop('update_range_usage', True)

            obj.delete(**kwargs)
{'fraction_non_alphanumeric': 0.07699711260827719, 'fraction_numerical': 0.0046198267564966315, 'mean_word_length': 2.5908776779543885, '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': '2316544', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1470, 'n_words': 500}