Beispiel #1
0
def modify_all(request, name):
    """
    Creates formset and populates data for modify_all page
    Records the changes that are made as appropriate
    """
    pylib.backup_database()
    MappingModelFormSet = modelformset_factory(Mapping, form=ModifyAllForm, can_delete=True, extra=0)
    if request.method == "POST":
        deletions = []
        modifications = []
        formset = MappingModelFormSet(request.POST)
        if formset.is_valid():
            for form in formset:  # Adds the subnet name and sets the record_type for each database entry
                modified_data = form.save(commit=False)
                modified_data.subnet_id = name
                if not pylib.is_valid_ip(modified_data.ip_or_cname):
                    modified_data.record_type = "CNAME"
                if form.changed_data and form not in formset.deleted_forms:
                    modifications.append(
                        "%s, %s, %s" % (modified_data.record_type, modified_data.ip_or_cname, modified_data.hostname)
                    )
                modified_data.save()
            deletions += pylib.remove_dependent_cnames(name, formset.deleted_forms)
            formset.save()  # Have to call this in 1.6 in order to delete the selected forms
            modifications, deletions = remove_mod_message_for_deleted(modifications, deletions)
            add_messages(request, deletions, "del")
            add_messages(request, modifications, "mod")
            domain_name = Subnet.objects.filter(subnet_name=name)[0].domain
            save_success = pylib.write_domain_zone_file(domain_name, name)
            if not save_success:
                messages.error(request, "ERROR: THE ZONE FILE DID NOT PASS NAMED-CHECKZONE. NO CHANGES SAVED")
                pylib.restore_database()
                return HttpResponseRedirect("/privatedns/index/error/")
            CHANGELOG.save(request)
            return HttpResponseRedirect("/privatedns/index/summary/")
        else:
            print formset.errors
    else:
        formset = MappingModelFormSet(queryset=Mapping.objects.filter(subnet=name))
    proper_name_obj = Subnet.objects.get(subnet_name=name)
    proper_name = proper_name_obj.proper_name
    context = {"formset": formset, "subnet_name": name, "proper_name": proper_name}
    return render(request, "pdns/modify_all.html", context)
Beispiel #2
0
def add_new(request, name, number):
    """
    Creates formset and data for new entry page. Will create the selected number of new forms for new entries

    arguments:
        request: request object
        name: subnet_name
        number: number of new entry forms to create
    """
    pylib.backup_database()
    MappingFormSet = formset_factory(MappingForm, extra=int(number), formset=RequiredFormSet)
    if request.method == "POST":
        additions = []
        formset = MappingFormSet(request.POST)
        if formset.is_valid():
            for form in formset:
                new_entry = form.save(commit=False)
                new_entry.subnet_id = name
                if new_entry.ip_or_cname == "":
                    new_entry.ip_or_cname = pylib.get_available_ip(name)
                if not pylib.is_valid_ip(new_entry.ip_or_cname):
                    new_entry.record_type = "CNAME"
                additions.append("%s, %s, %s" % (new_entry.record_type, new_entry.ip_or_cname, new_entry.hostname))
                new_entry.save()
            add_messages(request, additions, "add")
            print "name=", name
            domain_name = Subnet.objects.filter(subnet_name=name)[0].domain
            save_success = pylib.write_domain_zone_file(domain_name, name)
            if not save_success:
                messages.error(request, "ERROR: THE ZONE FILE DID NOT PASS NAMED-CHECKZONE. NO CHANGES SAVED")
                pylib.restore_database()
                return HttpResponseRedirect("/privatedns/index/error/")
            CHANGELOG.save(request)
            return HttpResponseRedirect("/privatedns/index/summary/")
        else:
            print formset.errors
    else:
        formset = MappingFormSet()
    proper_name_obj = Subnet.objects.get(subnet_name=name)
    proper_name = proper_name_obj.proper_name
    context = {"formset": formset, "subnet_name": name, "number": number, "proper_name": proper_name}
    return render(request, "pdns/add_new.html", context)
Beispiel #3
0
def mod_single_record(request, name, record):
    """
    Creates the form and populates the data for a single selected entry
    """
    pylib.backup_database()
    RecordModelFormSet = modelformset_factory(Mapping, form=RecordForm, can_delete=True, extra=0)
    if request.method == "POST":
        deletions = []
        modifications = []
        formset = RecordModelFormSet(request.POST)
        if formset.is_valid():
            for form in formset:
                modified_data = form.save(commit=False)
                modified_data.subnet_id = name
                if not pylib.is_valid_ip(modified_data.ip_or_cname):
                    modified_data.record_type = "CNAME"
                modifications.append(
                    "%s, %s, %s" % (modified_data.record_type, modified_data.ip_or_cname, modified_data.hostname)
                )
                modified_data.save()
            deletions += pylib.remove_dependent_cnames(name, formset.deleted_forms)
            formset.save()  # FYI - when testing, use a valid IP format. is_valid() will change the record_type to CNAME if not and you get an error when save() cant delete the obj
            modifications, deletions = remove_mod_message_for_deleted(modifications, deletions)
            add_messages(request, deletions, "del")
            add_messages(request, modifications, "mod")
            domain_name = Subnet.objects.filter(subnet_name=name)[0].domain
            save_success = pylib.write_domain_zone_file(domain_name, name)
            if not save_success:
                messages.error(request, "ERROR: THE ZONE FILE DID NOT PASS NAMED-CHECKZONE. NO CHANGES SAVED")
                pylib.restore_database()
                return HttpResponseRedirect("/privatedns/index/error/")
            CHANGELOG.save(request)
            return HttpResponseRedirect("/privatedns/index/summary/")
        else:
            print formset.errors
    else:
        formset = RecordModelFormSet(queryset=Mapping.objects.filter(hostname=record))
    proper_name_obj = Subnet.objects.get(subnet_name=name)
    proper_name = proper_name_obj.proper_name
    context = {"formset": formset, "subnet_name": name, "record": record, "proper_name": proper_name}
    return render(request, "pdns/modify_single_record.html", context)