Add logic to extend from current date

This commit is contained in:
zandercymatics 2024-02-07 15:23:49 -07:00
parent 68b6c8b46a
commit 942422b7bf
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 36 additions and 3 deletions

View file

@ -3,6 +3,7 @@ import logging
from django import forms from django import forms
from django.db.models.functions import Concat from django.db.models.functions import Concat
from django.http import HttpResponse from django.http import HttpResponse
from dateutil.relativedelta import relativedelta
from django.shortcuts import redirect from django.shortcuts import redirect
from django_fsm import get_available_FIELD_transitions from django_fsm import get_available_FIELD_transitions
from django.contrib import admin, messages from django.contrib import admin, messages
@ -23,6 +24,9 @@ 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__)
@ -1151,7 +1155,17 @@ class DomainAdmin(ListHeaderAdmin):
return None return None
try: try:
obj.renew_domain(date_to_extend=date.today()) exp_date = obj.registry_expiration_date
except KeyError:
# if no expiration date from registry, set it to today
logger.warning("current expiration date not set; setting to today")
exp_date = date.today()
desired_date = exp_date + relativedelta(years=1)
month_length = self._month_diff(desired_date, exp_date)
try:
obj.renew_domain(length=month_length, unit=epp.Unit.MONTH)
except RegistryError as err: except RegistryError as err:
if err.code: if err.code:
self.message_user( self.message_user(
@ -1185,6 +1199,24 @@ class DomainAdmin(ListHeaderAdmin):
) )
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)
# 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,

View file

@ -257,10 +257,11 @@ 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: if extend_year_past_current_date:
# TODO - handle unit == month # TODO - handle unit == month
expected_renewal_year = exp_date.year + length expected_renewal_year = exp_date.year + length
current_year = date.today().year current_year = date.today().year
if expected_renewal_year < current_year: if expected_renewal_year < current_year:
# Modify the length such that it will exceed the current year by the length # Modify the length such that it will exceed the current year by the length
@ -271,7 +272,7 @@ class Domain(TimeStampedModel, DomainHelper):
# we need to apply double "length" for it shoot past the current date # we need to apply double "length" for it shoot past the current date
# at the correct interval. # at the correct interval.
length = length * 2 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))