diff --git a/src/registrar/admin.py b/src/registrar/admin.py index de89e0150..d505f71d8 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -3,6 +3,7 @@ import logging from django import forms from django.db.models.functions import Concat from django.http import HttpResponse +from dateutil.relativedelta import relativedelta from django.shortcuts import redirect from django_fsm import get_available_FIELD_transitions 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.utils.safestring import mark_safe from django.utils.html import escape +from epplibwrapper import ( + common as epp, +) logger = logging.getLogger(__name__) @@ -1151,7 +1155,17 @@ class DomainAdmin(ListHeaderAdmin): return None 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: if err.code: self.message_user( @@ -1185,6 +1199,24 @@ class DomainAdmin(ListHeaderAdmin): ) 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): if not isinstance(obj, Domain): # Could be problematic if the type is similar, diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index be4c61301..c72487761 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -257,10 +257,11 @@ class Domain(TimeStampedModel, DomainHelper): # if no expiration date from registry, set it to today logger.warning("current expiration date not set; setting to 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 @@ -271,7 +272,7 @@ class Domain(TimeStampedModel, DomainHelper): # we need to apply double "length" for it shoot past the current date # at the correct interval. length = length * 2 - + """ # create RenewDomain request request = commands.RenewDomain(name=self.name, cur_exp_date=exp_date, period=epp.Period(length, unit))