some error validation, light refactor of ip checking in model

This commit is contained in:
Rachid Mrad 2023-10-18 15:36:41 -04:00
parent 16164f1f05
commit 51307d3b12
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
4 changed files with 45 additions and 13 deletions

View file

@ -26,7 +26,14 @@ class DomainNameserverForm(forms.Form):
server = forms.CharField(label="Name server", strip=True) server = forms.CharField(label="Name server", strip=True)
ip = forms.CharField(label="IP address", strip=True, required=False) ip = forms.CharField(
label="IP address",
strip=True,
required=False,
validators=[
# TODO in progress
],
)
NameserverFormset = formset_factory( NameserverFormset = formset_factory(

View file

@ -313,14 +313,14 @@ class Domain(TimeStampedModel, DomainHelper):
NameserverError (if exception hit) NameserverError (if exception hit)
Returns: Returns:
None""" None"""
if self.isSubdomain(nameserver) and (ip is None or ip == []): if self.isSubdomain(nameserver) and (ip is None or ip == [] or ip != []):
raise NameserverError(code=nsErrorCodes.MISSING_IP, nameserver=nameserver) raise NameserverError(code=nsErrorCodes.MISSING_IP, nameserver=nameserver)
elif not self.isSubdomain(nameserver) and (ip is not None and ip != []): elif not self.isSubdomain(nameserver) and (ip is not None and ip != [] and ip != ['']):
raise NameserverError( raise NameserverError(
code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED, nameserver=nameserver, ip=ip code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED, nameserver=nameserver, ip=ip
) )
elif ip is not None and ip != []: elif ip is not None and ip != [] and ip != ['']:
for addr in ip: for addr in ip:
logger.info(f"ip address {addr}") logger.info(f"ip address {addr}")
if not self._valid_ip_addr(addr): if not self._valid_ip_addr(addr):

View file

@ -16,7 +16,7 @@
{% include "includes/required_fields.html" %} {% include "includes/required_fields.html" %}
<form class="usa-form usa-form--large" method="post" novalidate id="form-container"> <form class="usa-form usa-form--extra-large" method="post" novalidate id="form-container">
{% csrf_token %} {% csrf_token %}
{{ formset.management_form }} {{ formset.management_form }}
@ -26,7 +26,7 @@
<div class="tablet:grid-col-6"> <div class="tablet:grid-col-6">
{% with sublabel_text="Example: ns"|concat:forloop.counter|concat:".example.com" %} {% with sublabel_text="Example: ns"|concat:forloop.counter|concat:".example.com" %}
{% if forloop.counter <= 2 %} {% if forloop.counter <= 2 %}
{% with attr_required=True %} {% with attr_required=True add_group_class="usa-form-group--unstyled-error" %}
{% input_with_errors form.server %} {% input_with_errors form.server %}
{% endwith %} {% endwith %}
{% else %} {% else %}
@ -35,7 +35,7 @@
{% endwith %} {% endwith %}
</div> </div>
<div class="tablet:grid-col-6"> <div class="tablet:grid-col-6">
{% with sublabel_text="Example: ns"|concat:forloop.counter|concat:".example.com" %} {% with sublabel_text="Example: ns"|concat:forloop.counter|concat:".example.com" add_group_class="usa-form-group--unstyled-error" %}
{% input_with_errors form.ip %} {% input_with_errors form.ip %}
{% endwith %} {% endwith %}
</div> </div>

View file

@ -23,6 +23,7 @@ from registrar.models import (
UserDomainRole, UserDomainRole,
) )
from registrar.models.public_contact import PublicContact from registrar.models.public_contact import PublicContact
from registrar.utility.errors import NameserverError
from ..forms import ( from ..forms import (
ContactForm, ContactForm,
@ -222,20 +223,44 @@ class DomainNameserversView(DomainPermissionView, FormMixin):
nameservers = [] nameservers = []
for form in formset: for form in formset:
try: try:
ip_string = form.cleaned_data["ip"]
# Split the string into a list using a comma as the delimiter
ip_list = ip_string.split(',')
# Remove any leading or trailing whitespace from each IP in the list
# this will return [''] if no ips have been entered, which is taken
# into account in the model in checkHostIPCombo
ip_list = [ip.strip() for ip in ip_list]
as_tuple = ( as_tuple = (
form.cleaned_data["server"], form.cleaned_data["server"],
[form.cleaned_data["ip"]] ip_list,
) )
nameservers.append(as_tuple) nameservers.append(as_tuple)
except KeyError: except KeyError:
# no server information in this field, skip it # no server information in this field, skip it
pass pass
domain = self.get_object() domain = self.get_object()
domain.nameservers = nameservers
try:
messages.success( domain.nameservers = nameservers
self.request, "The name servers for this domain have been updated." except NameserverError as Err:
) # TODO: move into literal
messages.error(self.request, 'Whoops, Nameservers Error')
# messages.error(self.request, GENERIC_ERROR)
logger.error(f"Nameservers error: {Err}")
# TODO: registry is not throwing an error when no connection
# TODO: merge 1103 and use literals
except RegistryError as Err:
if Err.is_connection_error():
messages.error(self.request, 'CANNOT_CONTACT_REGISTRY')
logger.error(f"Registry connection error: {Err}")
else:
messages.error(self.request, 'GENERIC_ERROR')
logger.error(f"Registry error: {Err}")
else:
messages.success(
self.request, "The name servers for this domain have been updated."
)
# superclass has the redirect # superclass has the redirect
return super().form_valid(formset) return super().form_valid(formset)