mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-25 03:58:39 +02:00
Code cleanup
This commit is contained in:
parent
591f67f6ec
commit
b06babd6f0
3 changed files with 31 additions and 88 deletions
|
@ -24,9 +24,7 @@ from auditlog.admin import LogEntryAdmin # type: ignore
|
||||||
from django_fsm import TransitionNotAllowed # type: ignore
|
from django_fsm import TransitionNotAllowed # type: ignore
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django.utils.html import escape
|
from django.utils.html import escape
|
||||||
from epplibwrapper import (
|
|
||||||
common as epp,
|
|
||||||
)
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -1150,10 +1148,17 @@ class DomainAdmin(ListHeaderAdmin):
|
||||||
return super().response_change(request, obj)
|
return super().response_change(request, obj)
|
||||||
|
|
||||||
def do_extend_expiration_date(self, request, obj):
|
def do_extend_expiration_date(self, request, obj):
|
||||||
|
"""Extends a domains expiration date by one year from the current date"""
|
||||||
|
|
||||||
|
# Make sure we're dealing with a Domain
|
||||||
if not isinstance(obj, Domain):
|
if not isinstance(obj, Domain):
|
||||||
self.message_user(request, "Object is not of type Domain.", messages.ERROR)
|
self.message_user(request, "Object is not of type Domain.", messages.ERROR)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
# Get the date we want to update to
|
||||||
|
desired_date = date.today() + relativedelta(years=1)
|
||||||
|
|
||||||
|
# Grab the current expiration date
|
||||||
try:
|
try:
|
||||||
exp_date = obj.registry_expiration_date
|
exp_date = obj.registry_expiration_date
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
@ -1161,46 +1166,29 @@ class DomainAdmin(ListHeaderAdmin):
|
||||||
logger.warning("current expiration date not set; setting to today")
|
logger.warning("current expiration date not set; setting to today")
|
||||||
exp_date = date.today()
|
exp_date = date.today()
|
||||||
|
|
||||||
desired_date = date.today() + relativedelta(years=1)
|
# If the expiration date is super old (2020, for example), we need to
|
||||||
logger.info(f"do_extend_expiration_date -> exp {exp_date} des {desired_date}")
|
# "catch up" to the current year, so we add the difference.
|
||||||
|
# If both years match, then lets just proceed as normal.
|
||||||
# Get the difference in months between the expiration date, and the
|
|
||||||
# desired date (today + 1). Then, add one year to that.
|
|
||||||
# TODO - error: Periods for domain registrations must be specified in years.???
|
|
||||||
# one_year = 12
|
|
||||||
# month_length = self._month_diff(exp_date, desired_date) + one_year
|
|
||||||
years = 1
|
years = 1
|
||||||
if desired_date > exp_date:
|
if desired_date > exp_date:
|
||||||
years = (desired_date.year - exp_date.year)
|
year_difference = relativedelta(desired_date.year, exp_date.year).years
|
||||||
|
years = year_difference
|
||||||
|
|
||||||
|
# Renew the domain.
|
||||||
try:
|
try:
|
||||||
# logger.info(f"do_extend_expiration_date -> month length: {month_length}")
|
obj.renew_domain(length=years)
|
||||||
logger.info(f"do_extend_expiration_date -> years {years}")
|
updated_domain = Domain.objects.filter(id=obj.id).get()
|
||||||
# TODO why cant I specify months
|
self.message_user(
|
||||||
#obj.renew_domain(length=month_length, unit=epp.Unit.MONTH)
|
request,
|
||||||
if years >= 1:
|
f"Successfully extended expiration date to {updated_domain.registry_expiration_date}.",
|
||||||
obj.renew_domain(length=years)
|
)
|
||||||
else:
|
|
||||||
self.message_user(
|
|
||||||
request,
|
|
||||||
f"Error extending this domain: Can't extend date by 0 years.",
|
|
||||||
messages.ERROR,
|
|
||||||
)
|
|
||||||
except RegistryError as err:
|
except RegistryError as err:
|
||||||
if err.code:
|
if err.is_connection_error():
|
||||||
self.message_user(
|
error_message = "Error connecting to the registry."
|
||||||
request,
|
|
||||||
f"Error extending this domain: {err}.",
|
|
||||||
messages.ERROR,
|
|
||||||
)
|
|
||||||
elif err.is_connection_error():
|
|
||||||
self.message_user(
|
|
||||||
request,
|
|
||||||
"Error connecting to the registry.",
|
|
||||||
messages.ERROR,
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
self.message_user(request, err, messages.ERROR)
|
error_message = f"Error extending this domain: {err}."
|
||||||
|
|
||||||
|
self.message_user(request, error_message, messages.ERROR)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# In normal code flow, a keyerror can only occur when
|
# In normal code flow, a keyerror can only occur when
|
||||||
# fresh data can't be pulled from the registry, and thus there is no cache.
|
# fresh data can't be pulled from the registry, and thus there is no cache.
|
||||||
|
@ -1210,34 +1198,11 @@ class DomainAdmin(ListHeaderAdmin):
|
||||||
messages.ERROR,
|
messages.ERROR,
|
||||||
)
|
)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
self.message_user(request, err, messages.ERROR)
|
logger.error(err, stack_info=True)
|
||||||
else:
|
self.message_user(request, "Could not delete: An unspecified error occured", messages.ERROR)
|
||||||
updated_domain = Domain.objects.filter(id=obj.id).get()
|
|
||||||
self.message_user(
|
|
||||||
request,
|
|
||||||
f"Successfully extended expiration date to {updated_domain.registry_expiration_date}.",
|
|
||||||
)
|
|
||||||
return HttpResponseRedirect(".")
|
return HttpResponseRedirect(".")
|
||||||
|
|
||||||
def _month_diff(self, date_1, date_2):
|
|
||||||
"""
|
|
||||||
Calculate the difference in months between two dates using dateutil's relativedelta.
|
|
||||||
|
|
||||||
:param date_1: The first date.
|
|
||||||
:param date_2: The second date.
|
|
||||||
:return: The difference in months as an integer.
|
|
||||||
"""
|
|
||||||
# Ensure date_1 is always the earlier date
|
|
||||||
start_date, end_date = sorted([date_1, date_2])
|
|
||||||
|
|
||||||
# Grab the delta between the two
|
|
||||||
rdelta = relativedelta(end_date, start_date)
|
|
||||||
logger.info(f"rdelta is: {rdelta}, years {rdelta.years}, months {rdelta.months}")
|
|
||||||
|
|
||||||
# Calculate total months as years * 12 + months
|
|
||||||
total_months = rdelta.years * 12 + rdelta.months
|
|
||||||
return total_months
|
|
||||||
|
|
||||||
def do_delete_domain(self, request, obj):
|
def do_delete_domain(self, request, obj):
|
||||||
if not isinstance(obj, Domain):
|
if not isinstance(obj, Domain):
|
||||||
# Could be problematic if the type is similar,
|
# Could be problematic if the type is similar,
|
||||||
|
@ -1392,11 +1357,7 @@ class DomainAdmin(ListHeaderAdmin):
|
||||||
def changelist_view(self, request, extra_context=None):
|
def changelist_view(self, request, extra_context=None):
|
||||||
extra_context = extra_context or {}
|
extra_context = extra_context or {}
|
||||||
# Create HTML for the modal button
|
# Create HTML for the modal button
|
||||||
modal_button = (
|
modal_button = '<button type="submit" ' 'class="usa-button" ' 'name="_extend_expiration_date">Confirm</button>'
|
||||||
'<button type="submit" '
|
|
||||||
'class="usa-button" '
|
|
||||||
'name="_extend_expiration_date">Confirm</button>'
|
|
||||||
)
|
|
||||||
extra_context["modal_button"] = modal_button
|
extra_context["modal_button"] = modal_button
|
||||||
return super().changelist_view(request, extra_context=extra_context)
|
return super().changelist_view(request, extra_context=extra_context)
|
||||||
|
|
||||||
|
|
|
@ -272,7 +272,7 @@ h1, h2, h3,
|
||||||
}
|
}
|
||||||
|
|
||||||
.cancel-extend-button{
|
.cancel-extend-button{
|
||||||
background-color: var(--delete-button-bg);
|
background-color: var(--delete-button-bg) !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
input.admin-confirm-button{
|
input.admin-confirm-button{
|
||||||
|
|
|
@ -239,15 +239,12 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
To update the expiration date, use renew_domain method."""
|
To update the expiration date, use renew_domain method."""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def renew_domain(self, length: int = 1, unit: epp.Unit = epp.Unit.YEAR, extend_year_past_current_date = False):
|
def renew_domain(self, length: int = 1, unit: epp.Unit = epp.Unit.YEAR):
|
||||||
"""
|
"""
|
||||||
Renew the domain to a length and unit of time relative to the current
|
Renew the domain to a length and unit of time relative to the current
|
||||||
expiration date.
|
expiration date.
|
||||||
|
|
||||||
Default length and unit of time are 1 year.
|
Default length and unit of time are 1 year.
|
||||||
|
|
||||||
extend_past_current_date (bool): Specifies if the "desired" date
|
|
||||||
should exceed the present date + length. For instance,
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# If no date is specified, grab the registry_expiration_date
|
# If no date is specified, grab the registry_expiration_date
|
||||||
|
@ -257,22 +254,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
||||||
# if no expiration date from registry, set it to today
|
# if no expiration date from registry, set it to today
|
||||||
logger.warning("current expiration date not set; setting to today")
|
logger.warning("current expiration date not set; setting to today")
|
||||||
exp_date = date.today()
|
exp_date = date.today()
|
||||||
"""
|
|
||||||
if extend_year_past_current_date:
|
|
||||||
# TODO - handle unit == month
|
|
||||||
expected_renewal_year = exp_date.year + length
|
|
||||||
|
|
||||||
current_year = date.today().year
|
|
||||||
if expected_renewal_year < current_year:
|
|
||||||
# Modify the length such that it will exceed the current year by the length
|
|
||||||
length = (current_year - exp_date.year) + length
|
|
||||||
# length = (current_year - expected_renewal_year) + length * 2
|
|
||||||
elif expected_renewal_year == current_year:
|
|
||||||
# In the event that the expected renewal date will equal the current year,
|
|
||||||
# we need to apply double "length" for it shoot past the current date
|
|
||||||
# at the correct interval.
|
|
||||||
length = length * 2
|
|
||||||
"""
|
|
||||||
# create RenewDomain request
|
# create RenewDomain request
|
||||||
request = commands.RenewDomain(name=self.name, cur_exp_date=exp_date, period=epp.Period(length, unit))
|
request = commands.RenewDomain(name=self.name, cur_exp_date=exp_date, period=epp.Period(length, unit))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue