From 2891391382d9ca227f0d43960df851ea1d949b4a Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 2 Feb 2024 15:33:42 -0700 Subject: [PATCH 01/55] Backend stuff --- src/registrar/admin.py | 21 +++++++++++++++++++++ src/registrar/models/domain.py | 19 +++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 77d827d05..8e2f7af68 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1,3 +1,4 @@ +from datetime import date import logging from django import forms from django.db.models.functions import Concat @@ -1133,6 +1134,7 @@ def response_change(self, request, obj): "_edit_domain": self.do_edit_domain, "_delete_domain": self.do_delete_domain, "_get_status": self.do_get_status, + "_extend_expiration_date": self.do_extend_expiration_date } # Check which action button was pressed and call the corresponding function @@ -1143,6 +1145,25 @@ def response_change(self, request, obj): # If no matching action button is found, return the super method return super().response_change(request, obj) + def do_extend_expiration_date(self, request, obj): + if not isinstance(obj, Domain): + # Could be problematic if the type is similar, + # but not the same (same field/func names). + # We do not want to accidentally delete records. + self.message_user(request, "Object is not of type Domain", messages.ERROR) + return None + try: + obj.renew_domain(date_to_extend=date.today()) + except Exception as err: + self.message_user(request, err, messages.ERROR) + else: + updated_domain = Domain.objects.filter(id=obj).get() + self.message_user( + request, + f"Successfully extended expiration date to {updated_domain.registry_expiration_date}", + ) + return HttpResponseRedirect(".") + 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 27a8364bc..84f451f56 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -239,22 +239,25 @@ def registry_expiration_date(self, ex_date: date): To update the expiration date, use renew_domain method.""" raise NotImplementedError() - def renew_domain(self, length: int = 1, unit: epp.Unit = epp.Unit.YEAR): + def renew_domain(self, length: int = 1, date_to_extend = None, unit: epp.Unit = epp.Unit.YEAR): """ Renew the domain to a length and unit of time relative to the current expiration date. Default length and unit of time are 1 year. """ - # if no expiration date from registry, set to today - try: - cur_exp_date = self.registry_expiration_date - except KeyError: - logger.warning("current expiration date not set; setting to today") - cur_exp_date = date.today() + + # If no date is specified, grab the registry_expiration_date + if date_to_extend is None: + try: + date_to_extend = self.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") + date_to_extend = date.today() # create RenewDomain request - request = commands.RenewDomain(name=self.name, cur_exp_date=cur_exp_date, period=epp.Period(length, unit)) + request = commands.RenewDomain(name=self.name, cur_exp_date=date_to_extend, period=epp.Period(length, unit)) try: # update expiration date in registry, and set the updated From b3401d8bfc492b1b5d09f7c62b028107df274d18 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 2 Feb 2024 15:39:51 -0700 Subject: [PATCH 02/55] Basic button --- src/registrar/admin.py | 3 ++- src/registrar/templates/django/admin/domain_change_form.html | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 8e2f7af68..0571a1743 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1134,7 +1134,7 @@ def response_change(self, request, obj): "_edit_domain": self.do_edit_domain, "_delete_domain": self.do_delete_domain, "_get_status": self.do_get_status, - "_extend_expiration_date": self.do_extend_expiration_date + "_extend_expiration_date": self.do_extend_expiration_date, } # Check which action button was pressed and call the corresponding function @@ -1152,6 +1152,7 @@ def do_extend_expiration_date(self, request, obj): # We do not want to accidentally delete records. self.message_user(request, "Object is not of type Domain", messages.ERROR) return None + try: obj.renew_domain(date_to_extend=date.today()) except Exception as err: diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index c4461d07f..bf2be1754 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -5,6 +5,7 @@
+
{% if original.state == original.State.READY %} From aff6aad1d493aaa978f77d9673618bc526c9bec2 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Tue, 6 Feb 2024 10:40:24 -0700 Subject: [PATCH 03/55] Add some additional errors --- src/registrar/admin.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 0571a1743..74424b56a 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1156,7 +1156,21 @@ def do_extend_expiration_date(self, request, obj): try: obj.renew_domain(date_to_extend=date.today()) except Exception as err: - self.message_user(request, err, messages.ERROR) + if err.code: + self.message_user( + 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: + # all other type error messages, display the error + self.message_user(request, err, messages.ERROR) else: updated_domain = Domain.objects.filter(id=obj).get() self.message_user( From 8d476d5a80ef3fa8e93a1653b21cda5fd2a76723 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Tue, 6 Feb 2024 12:58:44 -0700 Subject: [PATCH 04/55] Better logging --- src/registrar/admin.py | 8 +++----- src/registrar/models/domain.py | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 74424b56a..7c164ae27 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1147,15 +1147,12 @@ def response_change(self, request, obj): def do_extend_expiration_date(self, request, obj): if not isinstance(obj, Domain): - # Could be problematic if the type is similar, - # but not the same (same field/func names). - # We do not want to accidentally delete records. self.message_user(request, "Object is not of type Domain", messages.ERROR) return None try: obj.renew_domain(date_to_extend=date.today()) - except Exception as err: + except RegistryError as err: if err.code: self.message_user( request, @@ -1169,8 +1166,9 @@ def do_extend_expiration_date(self, request, obj): messages.ERROR, ) else: - # all other type error messages, display the error self.message_user(request, err, messages.ERROR) + except Exception as err: + self.message_user(request, err, messages.ERROR) else: updated_domain = Domain.objects.filter(id=obj).get() self.message_user( diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index 84f451f56..74461f4c9 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -255,7 +255,7 @@ def renew_domain(self, length: int = 1, date_to_extend = None, unit: epp.Unit = # if no expiration date from registry, set it to today logger.warning("current expiration date not set; setting to today") date_to_extend = date.today() - + print(f"This is the date to extend: {date_to_extend} vs registry {self.registry_expiration_date}") # create RenewDomain request request = commands.RenewDomain(name=self.name, cur_exp_date=date_to_extend, period=epp.Period(length, unit)) From b91572f9d301908dd55c8f4e7cf10640cddfe431 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Tue, 6 Feb 2024 13:06:56 -0700 Subject: [PATCH 05/55] Update domain.py --- src/registrar/models/domain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index 74461f4c9..577786dd0 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -255,7 +255,7 @@ def renew_domain(self, length: int = 1, date_to_extend = None, unit: epp.Unit = # if no expiration date from registry, set it to today logger.warning("current expiration date not set; setting to today") date_to_extend = date.today() - print(f"This is the date to extend: {date_to_extend} vs registry {self.registry_expiration_date}") + logger.info(f"This is the date to extend: {date_to_extend} vs registry {self.registry_expiration_date}") # create RenewDomain request request = commands.RenewDomain(name=self.name, cur_exp_date=date_to_extend, period=epp.Period(length, unit)) From 0ab48468dc1f870383928cc422fd546e74b34dfd Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Tue, 6 Feb 2024 15:28:27 -0700 Subject: [PATCH 06/55] Import USWDS Experimental changes --- src/registrar/templates/admin/base_site.html | 3 ++- .../templates/django/admin/domain_change_form.html | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/registrar/templates/admin/base_site.html b/src/registrar/templates/admin/base_site.html index c0884c912..da77c98d3 100644 --- a/src/registrar/templates/admin/base_site.html +++ b/src/registrar/templates/admin/base_site.html @@ -18,11 +18,12 @@ + + {% endblock %} {% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} - {% block extrastyle %}{{ block.super }} {% endblock %} diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index bf2be1754..c67d95235 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -6,6 +6,19 @@ + Disable DNSSEC +
+ {% include 'includes/modal.html' with modal_heading="Are you sure you want to disable DNSSEC?" modal_button=modal_button|safe %} +
{% if original.state == original.State.READY %} From f53df4f150f7df094c583995167128feeea5a99a Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 10:42:00 -0700 Subject: [PATCH 07/55] Add popup --- src/registrar/admin.py | 29 +++++++++-- src/registrar/assets/js/get-gov-admin.js | 23 +++++++++ src/registrar/assets/sass/_theme/_admin.scss | 6 +++ .../django/admin/domain_change_form.html | 48 +++++++++++++++++-- 4 files changed, 96 insertions(+), 10 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 7c164ae27..ed5f143bb 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1136,7 +1136,7 @@ def response_change(self, request, obj): "_get_status": self.do_get_status, "_extend_expiration_date": self.do_extend_expiration_date, } - + print(f"this is the response! {request.POST}") # Check which action button was pressed and call the corresponding function for action, function in ACTION_FUNCTIONS.items(): if action in request.POST: @@ -1147,7 +1147,7 @@ def response_change(self, request, obj): def do_extend_expiration_date(self, request, obj): 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 try: @@ -1156,24 +1156,32 @@ def do_extend_expiration_date(self, request, obj): if err.code: self.message_user( request, - f"Error extending this domain: {err}", + f"Error extending this domain: {err}.", messages.ERROR, ) elif err.is_connection_error(): self.message_user( request, - "Error connecting to the registry", + "Error connecting to the registry.", messages.ERROR, ) else: self.message_user(request, err, messages.ERROR) + except KeyError: + # 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. + self.message_user( + request, + "Error connecting to the registry. No expiration date was found.", + messages.ERROR, + ) except Exception as err: self.message_user(request, err, messages.ERROR) else: updated_domain = Domain.objects.filter(id=obj).get() self.message_user( request, - f"Successfully extended expiration date to {updated_domain.registry_expiration_date}", + f"Successfully extended expiration date to {updated_domain.registry_expiration_date}.", ) return HttpResponseRedirect(".") @@ -1328,6 +1336,17 @@ def has_change_permission(self, request, obj=None): return True return super().has_change_permission(request, obj) + def changelist_view(self, request, extra_context=None): + extra_context = extra_context or {} + # Create HTML for the modal button + modal_button = ( + '' + ) + extra_context["modal_button"] = modal_button + return super().changelist_view(request, extra_context=extra_context) + class DraftDomainAdmin(ListHeaderAdmin): """Custom draft domain admin class.""" diff --git a/src/registrar/assets/js/get-gov-admin.js b/src/registrar/assets/js/get-gov-admin.js index 866c7bd7d..007ded215 100644 --- a/src/registrar/assets/js/get-gov-admin.js +++ b/src/registrar/assets/js/get-gov-admin.js @@ -44,6 +44,29 @@ function openInNewTab(el, removeAttribute = false){ domainSubmitButton.addEventListener("mouseover", () => openInNewTab(domainFormElement, true)); domainSubmitButton.addEventListener("mouseout", () => openInNewTab(domainFormElement, false)); } + + let extendExpirationDateButton = document.getElementById("extend_expiration_date_button") + if (extendExpirationDateButton){ + extendExpirationDateButton.addEventListener("click", () => { + form = document.getElementById("domain_form") + /* + For some reason, Django admin has the propensity to ignore nested + inputs and delete nested form objects. + The workaround is to manually create an element as so, after the DOM + has been generated. + + Its not the most beautiful thing every, but it works. + */ + var input = document.createElement("input"); + input.type = "hidden"; + input.name = "_extend_expiration_date"; + // The value doesn't matter, just needs to be present + input.value = "1"; + // Add the hidden input to the form + form.appendChild(input); + form.submit(); + }) + } } prepareDjangoAdmin(); diff --git a/src/registrar/assets/sass/_theme/_admin.scss b/src/registrar/assets/sass/_theme/_admin.scss index 760c4f13a..4c0a1f7cc 100644 --- a/src/registrar/assets/sass/_theme/_admin.scss +++ b/src/registrar/assets/sass/_theme/_admin.scss @@ -270,3 +270,9 @@ h1, h2, h3, margin: 0!important; } } + +// Button groups in /admin incorrectly have bullets. +// Remove that! +.usa-button-group .usa-button-group__item { + list-style-type: none; +} diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index c67d95235..bf8a0de2e 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -5,19 +5,57 @@
- + {{ modal_button }} Disable DNSSEC + > + Extend expiration date +
- {% include 'includes/modal.html' with modal_heading="Are you sure you want to disable DNSSEC?" modal_button=modal_button|safe %} +
+
+ +
+ +
+ + +
+
+ {# {% include 'includes/modal.html' with modal_heading="Are you sure you want to extend this expiration date?" modal_button=modal_button|safe %} #}
{% if original.state == original.State.READY %} From c9957bcd2c381a811179e9f4c4990635c3df4a4d Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 10:54:22 -0700 Subject: [PATCH 08/55] Hotfix --- src/registrar/admin.py | 2 +- src/registrar/models/domain.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index ed5f143bb..de89e0150 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1136,7 +1136,7 @@ def response_change(self, request, obj): "_get_status": self.do_get_status, "_extend_expiration_date": self.do_extend_expiration_date, } - print(f"this is the response! {request.POST}") + # Check which action button was pressed and call the corresponding function for action, function in ACTION_FUNCTIONS.items(): if action in request.POST: diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index 577786dd0..db17a29b4 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -246,7 +246,7 @@ def renew_domain(self, length: int = 1, date_to_extend = None, unit: epp.Unit = Default length and unit of time are 1 year. """ - + logger.info(f"This is the date to extend: {date_to_extend}") # If no date is specified, grab the registry_expiration_date if date_to_extend is None: try: @@ -255,7 +255,7 @@ def renew_domain(self, length: int = 1, date_to_extend = None, unit: epp.Unit = # if no expiration date from registry, set it to today logger.warning("current expiration date not set; setting to today") date_to_extend = date.today() - logger.info(f"This is the date to extend: {date_to_extend} vs registry {self.registry_expiration_date}") + # create RenewDomain request request = commands.RenewDomain(name=self.name, cur_exp_date=date_to_extend, period=epp.Period(length, unit)) From 68b6c8b46a5f479fb6e229d5b47ffb74da4aa233 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 14:25:00 -0700 Subject: [PATCH 09/55] Update domain.py --- src/registrar/models/domain.py | 36 ++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index db17a29b4..be4c61301 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -239,25 +239,41 @@ def registry_expiration_date(self, ex_date: date): To update the expiration date, use renew_domain method.""" raise NotImplementedError() - def renew_domain(self, length: int = 1, date_to_extend = None, unit: epp.Unit = epp.Unit.YEAR): + def renew_domain(self, length: int = 1, unit: epp.Unit = epp.Unit.YEAR, extend_year_past_current_date = False): """ Renew the domain to a length and unit of time relative to the current expiration date. 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, """ - logger.info(f"This is the date to extend: {date_to_extend}") + # If no date is specified, grab the registry_expiration_date - if date_to_extend is None: - try: - date_to_extend = self.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") - date_to_extend = date.today() + try: + exp_date = self.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() + + 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 - request = commands.RenewDomain(name=self.name, cur_exp_date=date_to_extend, period=epp.Period(length, unit)) + request = commands.RenewDomain(name=self.name, cur_exp_date=exp_date, period=epp.Period(length, unit)) try: # update expiration date in registry, and set the updated From 942422b7bf150bc5548295e47d2a0527ceffe444 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 15:23:49 -0700 Subject: [PATCH 10/55] Add logic to extend from current date --- src/registrar/admin.py | 34 +++++++++++++++++++++++++++++++++- src/registrar/models/domain.py | 5 +++-- 2 files changed, 36 insertions(+), 3 deletions(-) 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 @@ 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 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 @@ def do_extend_expiration_date(self, request, obj): 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 @@ def do_extend_expiration_date(self, request, obj): ) 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 @@ def renew_domain(self, length: int = 1, unit: epp.Unit = epp.Unit.YEAR, extend_y # 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 @@ def renew_domain(self, length: int = 1, unit: epp.Unit = epp.Unit.YEAR, extend_y # 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)) From 1f1a537752f1ab3f72c9e2cfb582ac196548e762 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 15:25:52 -0700 Subject: [PATCH 11/55] Fix bad logic --- src/registrar/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index d505f71d8..13ecaeaab 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1161,7 +1161,7 @@ def do_extend_expiration_date(self, request, obj): logger.warning("current expiration date not set; setting to today") exp_date = date.today() - desired_date = exp_date + relativedelta(years=1) + desired_date = date.today() + relativedelta(years=1) month_length = self._month_diff(desired_date, exp_date) try: From f46988ef675886da06abd23ae84ab110542c7461 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 15:35:57 -0700 Subject: [PATCH 12/55] Add logging --- src/registrar/admin.py | 5 +- .../django/admin/domain_change_form.html | 82 ++++++++++--------- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 13ecaeaab..fc3dd861b 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1162,9 +1162,11 @@ def do_extend_expiration_date(self, request, obj): exp_date = date.today() desired_date = date.today() + relativedelta(years=1) - month_length = self._month_diff(desired_date, exp_date) + logger.info(f"do_extend_expiration_date -> exp {exp_date} des {desired_date}") + month_length = self._month_diff(exp_date, desired_date) try: + logger.info(f"do_extend_expiration_date -> month length: {month_length}") obj.renew_domain(length=month_length, unit=epp.Unit.MONTH) except RegistryError as err: if err.code: @@ -1212,6 +1214,7 @@ def _month_diff(self, 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 diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index bf8a0de2e..a07c514f3 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -13,48 +13,50 @@ > Extend expiration date -
-
-
- -
- -
- -
From 6c909dcc64846941877dbcc5d21c5ad4c9237b48 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 15:39:29 -0700 Subject: [PATCH 13/55] Update admin.py --- src/registrar/admin.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index fc3dd861b..c17e4e514 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1163,7 +1163,11 @@ def do_extend_expiration_date(self, request, obj): desired_date = date.today() + relativedelta(years=1) logger.info(f"do_extend_expiration_date -> exp {exp_date} des {desired_date}") - month_length = self._month_diff(exp_date, desired_date) + + # Get the difference in months between the expiration date, and the + # desired date (today + 1). Then, add one year to that. + one_year = 12 + month_length = self._month_diff(exp_date, desired_date) + one_year try: logger.info(f"do_extend_expiration_date -> month length: {month_length}") From 7b878c2195c003ce74007f8eacb51d5b51646e7d Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 15:54:38 -0700 Subject: [PATCH 14/55] Temp changes --- src/registrar/admin.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index c17e4e514..bef82cdfd 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1166,12 +1166,23 @@ def do_extend_expiration_date(self, request, obj): # 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 try: logger.info(f"do_extend_expiration_date -> month length: {month_length}") - obj.renew_domain(length=month_length, unit=epp.Unit.MONTH) + # TODO why cant I specify months + #obj.renew_domain(length=month_length, unit=epp.Unit.MONTH) + years = month_length/12 + if years >= 1: + obj.renew_domain(length=month_length/12) + else: + self.message_user( + request, + f"Error extending this domain: Can't extend date by 0 years.", + messages.ERROR, + ) except RegistryError as err: if err.code: self.message_user( From 8700a05fbf6a55089fb5792c603a9b34bb606e8b Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 16:02:57 -0700 Subject: [PATCH 15/55] Change years to int Temp changes because I cant pass in months specifically --- src/registrar/admin.py | 2 +- .../django/admin/domain_change_form.html | 84 +++++++++---------- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index bef82cdfd..de8e11606 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1174,7 +1174,7 @@ def do_extend_expiration_date(self, request, obj): logger.info(f"do_extend_expiration_date -> month length: {month_length}") # TODO why cant I specify months #obj.renew_domain(length=month_length, unit=epp.Unit.MONTH) - years = month_length/12 + years = int(month_length/12) if years >= 1: obj.renew_domain(length=month_length/12) else: diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index a07c514f3..8ee7605a5 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -6,6 +6,7 @@ {{ modal_button }} + {% if original.state != original.State.DELETED %} Extend expiration date - {% if original.state != original.State.DELETED %} -
-
-
- -
- -
- - +
+
+
+ +
+ +
+ +
- {% endif %} +
+ {% endif %} {# {% include 'includes/modal.html' with modal_heading="Are you sure you want to extend this expiration date?" modal_button=modal_button|safe %} #}
From 83269447aed7a18b122f851ff421bbe38fa8bbcd Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 7 Feb 2024 16:10:45 -0700 Subject: [PATCH 16/55] Update admin.py --- src/registrar/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index de8e11606..b266c0c57 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1176,7 +1176,7 @@ def do_extend_expiration_date(self, request, obj): #obj.renew_domain(length=month_length, unit=epp.Unit.MONTH) years = int(month_length/12) if years >= 1: - obj.renew_domain(length=month_length/12) + obj.renew_domain(length=years) else: self.message_user( request, From abdc8fa7fdbd726db62ab7a015e56e90f8b9c97a Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 8 Feb 2024 09:08:51 -0700 Subject: [PATCH 17/55] Change to years --- src/registrar/admin.py | 13 ++++++++----- .../templates/django/admin/domain_change_form.html | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index b266c0c57..5b756f689 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1167,14 +1167,17 @@ def do_extend_expiration_date(self, request, obj): # 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 + # one_year = 12 + # month_length = self._month_diff(exp_date, desired_date) + one_year + years = 1 + if desired_date > exp_date: + years = (desired_date.year - exp_date.year) try: - logger.info(f"do_extend_expiration_date -> month length: {month_length}") + # logger.info(f"do_extend_expiration_date -> month length: {month_length}") + logger.info(f"do_extend_expiration_date -> years {years}") # TODO why cant I specify months #obj.renew_domain(length=month_length, unit=epp.Unit.MONTH) - years = int(month_length/12) if years >= 1: obj.renew_domain(length=years) else: @@ -1209,7 +1212,7 @@ def do_extend_expiration_date(self, request, obj): except Exception as err: self.message_user(request, err, messages.ERROR) else: - updated_domain = Domain.objects.filter(id=obj).get() + updated_domain = Domain.objects.filter(id=obj.id).get() self.message_user( request, f"Successfully extended expiration date to {updated_domain.registry_expiration_date}.", diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index 8ee7605a5..2dddc4dcb 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -60,6 +60,10 @@
+ {% if original.state != original.State.DELETED %} + + {% endif %} + | {% if original.state == original.State.READY %} {% elif original.state == original.State.ON_HOLD %} From 65b7091a8fc230f2c1070b9cf63647eb2dc8cd9b Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 8 Feb 2024 10:38:28 -0700 Subject: [PATCH 18/55] Add USWDS classes for spacing, etc --- src/registrar/assets/sass/_theme/_admin.scss | 6 ++ .../django/admin/domain_change_form.html | 93 +++++-------------- 2 files changed, 28 insertions(+), 71 deletions(-) diff --git a/src/registrar/assets/sass/_theme/_admin.scss b/src/registrar/assets/sass/_theme/_admin.scss index 4c0a1f7cc..098e9bb81 100644 --- a/src/registrar/assets/sass/_theme/_admin.scss +++ b/src/registrar/assets/sass/_theme/_admin.scss @@ -271,6 +271,12 @@ h1, h2, h3, } } +@include at-media(mobile){ + .button-list-mobile { + display: contents; + } +} + // Button groups in /admin incorrectly have bullets. // Remove that! .usa-button-group .usa-button-group__item { diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index 2dddc4dcb..6420f8b5f 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -2,79 +2,30 @@ {% load i18n static %} {% block field_sets %} -
- - - {{ modal_button }} - {% if original.state != original.State.DELETED %} - - Extend expiration date - -
-
-
- -
- -
- - -
-
- {% endif %} - {# {% include 'includes/modal.html' with modal_heading="Are you sure you want to extend this expiration date?" modal_button=modal_button|safe %} #} +
+
+ + {# todo: avoid this #} +   +
-
- {% if original.state != original.State.DELETED %} - - {% endif %} - | - {% if original.state == original.State.READY %} - - {% elif original.state == original.State.ON_HOLD %} - - {% endif %} - {% if original.state == original.State.READY or original.state == original.State.ON_HOLD %} - | - {% endif %} - {% if original.state != original.State.DELETED %} - +
+ {% if original.state != original.State.DELETED %} + + {% endif %} + | + {% if original.state == original.State.READY %} + + {% elif original.state == original.State.ON_HOLD %} + + {% endif %} + {% if original.state == original.State.READY or original.state == original.State.ON_HOLD %} + | + {% endif %} + {% if original.state != original.State.DELETED %} + {% endif %} +
{{ block.super }} {% endblock %} From 612f967c6fea2867cd08b2d874a6100ce5b667de Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 8 Feb 2024 10:48:09 -0700 Subject: [PATCH 19/55] Desktop/mobile styling --- src/registrar/assets/sass/_theme/_admin.scss | 6 ++++++ .../templates/django/admin/domain_change_form.html | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/registrar/assets/sass/_theme/_admin.scss b/src/registrar/assets/sass/_theme/_admin.scss index 098e9bb81..7432da46d 100644 --- a/src/registrar/assets/sass/_theme/_admin.scss +++ b/src/registrar/assets/sass/_theme/_admin.scss @@ -271,6 +271,12 @@ h1, h2, h3, } } +@include at-media(desktop){ + .button-list-mobile { + display: block; + } +} + @include at-media(mobile){ .button-list-mobile { display: contents; diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index 6420f8b5f..8c0093d60 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -7,7 +7,7 @@ {# todo: avoid this #}   - +
{% if original.state != original.State.DELETED %} From df70a179e883d0a9aca842c7569887bb69e6d734 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 8 Feb 2024 11:43:56 -0700 Subject: [PATCH 20/55] Add conditional "confirm" logic --- src/registrar/assets/js/get-gov-admin.js | 35 +++++++++---------- src/registrar/assets/sass/_theme/_admin.scss | 5 +++ .../django/admin/domain_change_form.html | 6 +++- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/registrar/assets/js/get-gov-admin.js b/src/registrar/assets/js/get-gov-admin.js index 007ded215..8005c68cd 100644 --- a/src/registrar/assets/js/get-gov-admin.js +++ b/src/registrar/assets/js/get-gov-admin.js @@ -44,27 +44,24 @@ function openInNewTab(el, removeAttribute = false){ domainSubmitButton.addEventListener("mouseover", () => openInNewTab(domainFormElement, true)); domainSubmitButton.addEventListener("mouseout", () => openInNewTab(domainFormElement, false)); } + + let extendExpirationDateButton = document.getElementById("extend-expiration-button") + let confirmationButtons = document.querySelector(".admin-confirmation-buttons") + let cancelExpirationButton = document.getElementById("cancel-extend-button") + if (extendExpirationDateButton && confirmationButtons && cancelExpirationButton){ - let extendExpirationDateButton = document.getElementById("extend_expiration_date_button") - if (extendExpirationDateButton){ + // Tie logic to the extend button to show confirmation options extendExpirationDateButton.addEventListener("click", () => { - form = document.getElementById("domain_form") - /* - For some reason, Django admin has the propensity to ignore nested - inputs and delete nested form objects. - The workaround is to manually create an element as so, after the DOM - has been generated. - - Its not the most beautiful thing every, but it works. - */ - var input = document.createElement("input"); - input.type = "hidden"; - input.name = "_extend_expiration_date"; - // The value doesn't matter, just needs to be present - input.value = "1"; - // Add the hidden input to the form - form.appendChild(input); - form.submit(); + extendExpirationDateButton.hidden = true + console.log("these are the buttons: ") + console.log(confirmationButtons) + confirmationButtons.hidden = false + }) + + // Tie logic to the cancel button to hide the confirmation options + cancelExpirationButton.addEventListener("click", () => { + confirmationButtons.hidden = true + extendExpirationDateButton.hidden = false }) } } diff --git a/src/registrar/assets/sass/_theme/_admin.scss b/src/registrar/assets/sass/_theme/_admin.scss index 7432da46d..98f3a7835 100644 --- a/src/registrar/assets/sass/_theme/_admin.scss +++ b/src/registrar/assets/sass/_theme/_admin.scss @@ -283,6 +283,11 @@ h1, h2, h3, } } +.usa-button{ + font-size: 14px; + font-weight: normal; +} + // Button groups in /admin incorrectly have bullets. // Remove that! .usa-button-group .usa-button-group__item { diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index 8c0093d60..1a21f4a90 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -11,7 +11,11 @@
{% if original.state != original.State.DELETED %} - + + {% endif %} | {% if original.state == original.State.READY %} From 37430c552f0a54c46e4cf6130c05ec3eecdc1da5 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 8 Feb 2024 11:54:52 -0700 Subject: [PATCH 21/55] Change button styling on /admin --- src/registrar/assets/sass/_theme/_admin.scss | 19 ++++++------------- .../django/admin/domain_change_form.html | 4 ++-- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/registrar/assets/sass/_theme/_admin.scss b/src/registrar/assets/sass/_theme/_admin.scss index 98f3a7835..e9c13b895 100644 --- a/src/registrar/assets/sass/_theme/_admin.scss +++ b/src/registrar/assets/sass/_theme/_admin.scss @@ -271,21 +271,14 @@ h1, h2, h3, } } -@include at-media(desktop){ - .button-list-mobile { - display: block; - } -} - -@include at-media(mobile){ - .button-list-mobile { - display: contents; - } -} - -.usa-button{ +.usa-button.admin-button{ font-size: 14px; font-weight: normal; + background-color: var(--button-bg); +} + +.usa-button--secondary.admin-button{ + background-color: var(--delete-button-bg); } // Button groups in /admin incorrectly have bullets. diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index 1a21f4a90..d45122422 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -12,8 +12,8 @@
{% if original.state != original.State.DELETED %} {% endif %} From efa8b7d0ef9adf2d3c37efe7f716e2a71585cab1 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 8 Feb 2024 12:21:16 -0700 Subject: [PATCH 22/55] Use default admin input --- src/registrar/assets/sass/_theme/_admin.scss | 14 +++----------- .../templates/django/admin/domain_change_form.html | 8 +++++--- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/registrar/assets/sass/_theme/_admin.scss b/src/registrar/assets/sass/_theme/_admin.scss index e9c13b895..2ed0594dd 100644 --- a/src/registrar/assets/sass/_theme/_admin.scss +++ b/src/registrar/assets/sass/_theme/_admin.scss @@ -271,18 +271,10 @@ h1, h2, h3, } } -.usa-button.admin-button{ - font-size: 14px; - font-weight: normal; - background-color: var(--button-bg); -} - -.usa-button--secondary.admin-button{ +.cancel-extend-button{ background-color: var(--delete-button-bg); } -// Button groups in /admin incorrectly have bullets. -// Remove that! -.usa-button-group .usa-button-group__item { - list-style-type: none; +.admin-confirm-button{ + text-transform: none; } diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index d45122422..0835c6680 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -12,12 +12,14 @@
{% if original.state != original.State.DELETED %} - {% endif %} | + {% endif %} {% if original.state == original.State.READY %} {% elif original.state == original.State.ON_HOLD %} From 591f67f6ecdbed3de3cd2cfdac80a5851ccaaca9 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 8 Feb 2024 12:33:53 -0700 Subject: [PATCH 23/55] QOL Changes --- src/registrar/assets/js/get-gov-admin.js | 8 +++----- src/registrar/assets/sass/_theme/_admin.scss | 4 ++-- .../templates/django/admin/domain_change_form.html | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/registrar/assets/js/get-gov-admin.js b/src/registrar/assets/js/get-gov-admin.js index 8005c68cd..fa429ba7f 100644 --- a/src/registrar/assets/js/get-gov-admin.js +++ b/src/registrar/assets/js/get-gov-admin.js @@ -41,20 +41,18 @@ function openInNewTab(el, removeAttribute = false){ let domainFormElement = document.getElementById("domain_form"); let domainSubmitButton = document.getElementById("manageDomainSubmitButton"); if(domainSubmitButton && domainFormElement){ - domainSubmitButton.addEventListener("mouseover", () => openInNewTab(domainFormElement, true)); - domainSubmitButton.addEventListener("mouseout", () => openInNewTab(domainFormElement, false)); + domainSubmitButton.addEventListener("mouseover", () => openInNewTab(domainFormElement, true)); + domainSubmitButton.addEventListener("mouseout", () => openInNewTab(domainFormElement, false)); } let extendExpirationDateButton = document.getElementById("extend-expiration-button") let confirmationButtons = document.querySelector(".admin-confirmation-buttons") - let cancelExpirationButton = document.getElementById("cancel-extend-button") + let cancelExpirationButton = document.querySelector(".cancel-extend-button") if (extendExpirationDateButton && confirmationButtons && cancelExpirationButton){ // Tie logic to the extend button to show confirmation options extendExpirationDateButton.addEventListener("click", () => { extendExpirationDateButton.hidden = true - console.log("these are the buttons: ") - console.log(confirmationButtons) confirmationButtons.hidden = false }) diff --git a/src/registrar/assets/sass/_theme/_admin.scss b/src/registrar/assets/sass/_theme/_admin.scss index 2ed0594dd..f3bd0bbd8 100644 --- a/src/registrar/assets/sass/_theme/_admin.scss +++ b/src/registrar/assets/sass/_theme/_admin.scss @@ -275,6 +275,6 @@ h1, h2, h3, background-color: var(--delete-button-bg); } -.admin-confirm-button{ - text-transform: none; +input.admin-confirm-button{ + text-transform: none !important; } diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index 0835c6680..2b4461a49 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -15,7 +15,7 @@ {# todo: avoid this #}   - + | From b06babd6f00cac606691d889050a49187e475624 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 8 Feb 2024 13:40:02 -0700 Subject: [PATCH 24/55] Code cleanup --- src/registrar/admin.py | 97 ++++++-------------- src/registrar/assets/sass/_theme/_admin.scss | 2 +- src/registrar/models/domain.py | 22 +---- 3 files changed, 32 insertions(+), 89 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 5b756f689..72d587855 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -24,9 +24,7 @@ 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__) @@ -1150,10 +1148,17 @@ def response_change(self, request, obj): return super().response_change(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): self.message_user(request, "Object is not of type Domain.", messages.ERROR) return None + # Get the date we want to update to + desired_date = date.today() + relativedelta(years=1) + + # Grab the current expiration date try: exp_date = obj.registry_expiration_date except KeyError: @@ -1161,46 +1166,29 @@ def do_extend_expiration_date(self, request, obj): logger.warning("current expiration date not set; setting to today") exp_date = date.today() - desired_date = date.today() + relativedelta(years=1) - logger.info(f"do_extend_expiration_date -> exp {exp_date} des {desired_date}") - - # 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 + # If the expiration date is super old (2020, for example), we need to + # "catch up" to the current year, so we add the difference. + # If both years match, then lets just proceed as normal. years = 1 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: - # logger.info(f"do_extend_expiration_date -> month length: {month_length}") - logger.info(f"do_extend_expiration_date -> years {years}") - # TODO why cant I specify months - #obj.renew_domain(length=month_length, unit=epp.Unit.MONTH) - if years >= 1: - obj.renew_domain(length=years) - else: - self.message_user( - request, - f"Error extending this domain: Can't extend date by 0 years.", - messages.ERROR, - ) + obj.renew_domain(length=years) + updated_domain = Domain.objects.filter(id=obj.id).get() + self.message_user( + request, + f"Successfully extended expiration date to {updated_domain.registry_expiration_date}.", + ) except RegistryError as err: - if err.code: - self.message_user( - 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, - ) + if err.is_connection_error(): + error_message = "Error connecting to the registry." 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: # 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. @@ -1210,33 +1198,10 @@ def do_extend_expiration_date(self, request, obj): messages.ERROR, ) except Exception as err: - self.message_user(request, err, messages.ERROR) - else: - 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(".") - - def _month_diff(self, date_1, date_2): - """ - Calculate the difference in months between two dates using dateutil's relativedelta. + logger.error(err, stack_info=True) + self.message_user(request, "Could not delete: An unspecified error occured", messages.ERROR) - :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 + return HttpResponseRedirect(".") def do_delete_domain(self, request, obj): if not isinstance(obj, Domain): @@ -1392,11 +1357,7 @@ def has_change_permission(self, request, obj=None): def changelist_view(self, request, extra_context=None): extra_context = extra_context or {} # Create HTML for the modal button - modal_button = ( - '' - ) + modal_button = '' extra_context["modal_button"] = modal_button return super().changelist_view(request, extra_context=extra_context) diff --git a/src/registrar/assets/sass/_theme/_admin.scss b/src/registrar/assets/sass/_theme/_admin.scss index f3bd0bbd8..4e4c15dd9 100644 --- a/src/registrar/assets/sass/_theme/_admin.scss +++ b/src/registrar/assets/sass/_theme/_admin.scss @@ -272,7 +272,7 @@ h1, h2, h3, } .cancel-extend-button{ - background-color: var(--delete-button-bg); + background-color: var(--delete-button-bg) !important; } input.admin-confirm-button{ diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index c72487761..dbde2be10 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -239,15 +239,12 @@ def registry_expiration_date(self, ex_date: date): To update the expiration date, use renew_domain method.""" 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 expiration date. 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 @@ -257,22 +254,7 @@ def renew_domain(self, length: int = 1, unit: epp.Unit = epp.Unit.YEAR, extend_y # 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 - 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 request = commands.RenewDomain(name=self.name, cur_exp_date=exp_date, period=epp.Period(length, unit)) From 773d6e0cee6f777be401e50de19d8372905a967e Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 9 Feb 2024 10:06:46 -0700 Subject: [PATCH 25/55] Unit test --- src/registrar/admin.py | 2 +- src/registrar/tests/test_admin.py | 64 ++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 72d587855..10d23be15 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1171,7 +1171,7 @@ def do_extend_expiration_date(self, request, obj): # If both years match, then lets just proceed as normal. years = 1 if desired_date > exp_date: - year_difference = relativedelta(desired_date.year, exp_date.year).years + year_difference = desired_date.year - exp_date.year years = year_difference # Renew the domain. diff --git a/src/registrar/tests/test_admin.py b/src/registrar/tests/test_admin.py index ad616d6b0..082ad9b2b 100644 --- a/src/registrar/tests/test_admin.py +++ b/src/registrar/tests/test_admin.py @@ -1,6 +1,8 @@ +from datetime import date from django.test import TestCase, RequestFactory, Client from django.contrib.admin.sites import AdminSite from contextlib import ExitStack +from django_webtest import WebTest # type: ignore from django.contrib import messages from django.urls import reverse from registrar.admin import ( @@ -35,7 +37,7 @@ ) from django.contrib.sessions.backends.db import SessionStore from django.contrib.auth import get_user_model -from unittest.mock import patch +from unittest.mock import call, patch from unittest import skip from django.conf import settings @@ -45,7 +47,8 @@ logger = logging.getLogger(__name__) -class TestDomainAdmin(MockEppLib): +class TestDomainAdmin(MockEppLib, WebTest): + csrf_checks = False def setUp(self): self.site = AdminSite() self.admin = DomainAdmin(model=Domain, admin_site=self.site) @@ -53,8 +56,65 @@ def setUp(self): self.superuser = create_superuser() self.staffuser = create_user() self.factory = RequestFactory() + self.app.set_user(self.superuser.username) + self.client.force_login(self.superuser) super().setUp() + def test_extend_expiration_date_button(self): + """ + Tests if extend_expiration_date button sends the right epp command + """ + + # Create a ready domain with a preset expiration date + domain, _ = Domain.objects.get_or_create(name="fake.gov", state=Domain.State.READY) + + response = self.app.get(reverse("admin:registrar_domain_change", args=[domain.pk])) + + # Make sure that the page is loading as expected + self.assertEqual(response.status_code, 200) + self.assertContains(response, domain.name) + self.assertContains(response, "Extend expiration date") + + # Grab the form to submit + form = response.forms["domain_form"] + + with patch("django.contrib.messages.add_message") as mock_add_message: + with patch("registrar.models.Domain.renew_domain") as renew_mock: + # Submit the form + response = form.submit("_extend_expiration_date") + + # Follow the response + response = response.follow() + + # We need to use date.today() here, as it is not trivial + # to mock "date.today()". To do so requires libraries like freezegun, + # or convoluted workarounds. + extension_length = (date.today().year + 1) - 2023 + + # Assert that it is calling the function + renew_mock.assert_has_calls([call(length=extension_length)], any_order=False) + self.assertEqual(renew_mock.call_count, 1) + + # Assert that everything on the page looks correct + self.assertEqual(response.status_code, 200) + self.assertContains(response, domain.name) + self.assertContains(response, "Extend expiration date") + + # Ensure the message we recieve is in line with what we expect + expected_message = f"Successfully extended expiration date to 2024-01-01." + #self.assertContains(response, expected_message) + expected_call = call( + response, + messages.INFO, + expected_message, + extra_tags="", + fail_silently=False, + ) + mock_add_message.assert_has_calls(expected_call, 1) + # Assert that the domain was updated correctly + expected_date = date(year=2025, month=1, day=1) + self.assertEqual(domain.expiration_date, expected_date) + def test_short_org_name_in_domains_list(self): """ Make sure the short name is displaying in admin on the list page From 3189b50baaa443b2e8c65abca03725fe7be6e818 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 9 Feb 2024 10:13:10 -0700 Subject: [PATCH 26/55] Fix unit test --- src/registrar/admin.py | 3 +-- src/registrar/tests/test_admin.py | 13 +++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 10d23be15..3dc0d0e56 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1177,10 +1177,9 @@ def do_extend_expiration_date(self, request, obj): # Renew the domain. try: obj.renew_domain(length=years) - updated_domain = Domain.objects.filter(id=obj.id).get() self.message_user( request, - f"Successfully extended expiration date to {updated_domain.registry_expiration_date}.", + f"Successfully extended expiration date.", ) except RegistryError as err: if err.is_connection_error(): diff --git a/src/registrar/tests/test_admin.py b/src/registrar/tests/test_admin.py index 082ad9b2b..4da7de85a 100644 --- a/src/registrar/tests/test_admin.py +++ b/src/registrar/tests/test_admin.py @@ -37,7 +37,7 @@ ) from django.contrib.sessions.backends.db import SessionStore from django.contrib.auth import get_user_model -from unittest.mock import call, patch +from unittest.mock import ANY, call, patch from unittest import skip from django.conf import settings @@ -101,19 +101,16 @@ def test_extend_expiration_date_button(self): self.assertContains(response, "Extend expiration date") # Ensure the message we recieve is in line with what we expect - expected_message = f"Successfully extended expiration date to 2024-01-01." - #self.assertContains(response, expected_message) + expected_message = f"Successfully extended expiration date." expected_call = call( - response, + # The WGSI request doesn't need to be tested + ANY, messages.INFO, expected_message, extra_tags="", fail_silently=False, ) - mock_add_message.assert_has_calls(expected_call, 1) - # Assert that the domain was updated correctly - expected_date = date(year=2025, month=1, day=1) - self.assertEqual(domain.expiration_date, expected_date) + mock_add_message.assert_has_calls([expected_call], 1) def test_short_org_name_in_domains_list(self): """ From d18baa773ca3c3ec129d0b2150ab239483dfce1c Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 9 Feb 2024 10:18:08 -0700 Subject: [PATCH 27/55] Update admin.py --- src/registrar/admin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 3dc0d0e56..e77df1ce5 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1169,8 +1169,9 @@ def do_extend_expiration_date(self, request, obj): # If the expiration date is super old (2020, for example), we need to # "catch up" to the current year, so we add the difference. # If both years match, then lets just proceed as normal. + calculated_exp_date = exp_date + relativedelta(years=1) years = 1 - if desired_date > exp_date: + if desired_date > calculated_exp_date: year_difference = desired_date.year - exp_date.year years = year_difference From a1769b50c660fa046f7c8d20ee88a8c9d0212db4 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 9 Feb 2024 11:50:05 -0700 Subject: [PATCH 28/55] Use modal --- src/registrar/assets/js/get-gov-admin.js | 45 ++++++++----- src/registrar/assets/sass/_theme/_admin.scss | 7 ++ .../django/admin/domain_change_form.html | 66 ++++++++++++++++++- 3 files changed, 99 insertions(+), 19 deletions(-) diff --git a/src/registrar/assets/js/get-gov-admin.js b/src/registrar/assets/js/get-gov-admin.js index fa429ba7f..29aa9ce03 100644 --- a/src/registrar/assets/js/get-gov-admin.js +++ b/src/registrar/assets/js/get-gov-admin.js @@ -23,6 +23,33 @@ function openInNewTab(el, removeAttribute = false){ // <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>> // Initialization code. +/** An IIFE for pages in DjangoAdmin that use modals. + * Dja strips out form elements, and modals generate their content outside + * of the current form scope, so we need to "inject" these inputs. +*/ +(function (){ + function createPhantomModalFormButtons(){ + let submitButtons = document.querySelectorAll('.usa-modal button[type="submit"]'); + form = document.querySelector("form") + submitButtons.forEach((button) => { + + let input = document.createElement("input"); + input.type = "submit"; + input.name = button.name; + input.value = button.value; + input.style.display = "none" + + // Add the hidden input to the form + form.appendChild(input); + button.addEventListener("click", () => { + console.log("clicking") + input.click(); + }) + }) + } + + createPhantomModalFormButtons(); +})(); /** An IIFE for pages in DjangoAdmin which may need custom JS implementation. * Currently only appends target="_blank" to the domain_form object, * but this can be expanded. @@ -44,24 +71,6 @@ function openInNewTab(el, removeAttribute = false){ domainSubmitButton.addEventListener("mouseover", () => openInNewTab(domainFormElement, true)); domainSubmitButton.addEventListener("mouseout", () => openInNewTab(domainFormElement, false)); } - - let extendExpirationDateButton = document.getElementById("extend-expiration-button") - let confirmationButtons = document.querySelector(".admin-confirmation-buttons") - let cancelExpirationButton = document.querySelector(".cancel-extend-button") - if (extendExpirationDateButton && confirmationButtons && cancelExpirationButton){ - - // Tie logic to the extend button to show confirmation options - extendExpirationDateButton.addEventListener("click", () => { - extendExpirationDateButton.hidden = true - confirmationButtons.hidden = false - }) - - // Tie logic to the cancel button to hide the confirmation options - cancelExpirationButton.addEventListener("click", () => { - confirmationButtons.hidden = true - extendExpirationDateButton.hidden = false - }) - } } prepareDjangoAdmin(); diff --git a/src/registrar/assets/sass/_theme/_admin.scss b/src/registrar/assets/sass/_theme/_admin.scss index 4e4c15dd9..bee4f1466 100644 --- a/src/registrar/assets/sass/_theme/_admin.scss +++ b/src/registrar/assets/sass/_theme/_admin.scss @@ -278,3 +278,10 @@ h1, h2, h3, input.admin-confirm-button{ text-transform: none !important; } + + +// Button groups in /admin incorrectly have bullets. +// Remove that! +.usa-modal__footer .usa-button-group__item{ + list-style-type: none; +} \ No newline at end of file diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index 2b4461a49..28ae01aec 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -17,7 +17,14 @@   - + + Extend expiration date + | {% endif %} {% if original.state == original.State.READY %} @@ -35,3 +42,60 @@
{{ block.super }} {% endblock %} + +{% block submit_buttons_bottom %} +
+
+
+ +
+ +
+ + +
+ +
+
+{{ block.super }} +{% endblock %} \ No newline at end of file From bfc82bda0149fabb9ee4bf2b213748b6c98ea2eb Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 9 Feb 2024 11:55:47 -0700 Subject: [PATCH 29/55] Add "the" --- src/registrar/admin.py | 2 +- src/registrar/tests/test_admin.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index e77df1ce5..7439217a1 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1180,7 +1180,7 @@ def do_extend_expiration_date(self, request, obj): obj.renew_domain(length=years) self.message_user( request, - f"Successfully extended expiration date.", + f"Successfully extended the expiration date.", ) except RegistryError as err: if err.is_connection_error(): diff --git a/src/registrar/tests/test_admin.py b/src/registrar/tests/test_admin.py index 4da7de85a..6cc6f96ea 100644 --- a/src/registrar/tests/test_admin.py +++ b/src/registrar/tests/test_admin.py @@ -101,7 +101,7 @@ def test_extend_expiration_date_button(self): self.assertContains(response, "Extend expiration date") # Ensure the message we recieve is in line with what we expect - expected_message = f"Successfully extended expiration date." + expected_message = f"Successfully extended the expiration date." expected_call = call( # The WGSI request doesn't need to be tested ANY, From 10e8317e3c31096f6236634c46cf4ab69fb008db Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 9 Feb 2024 15:08:08 -0700 Subject: [PATCH 30/55] Test cases, black linting --- src/registrar/admin.py | 14 +++- src/registrar/tests/common.py | 25 ++++-- src/registrar/tests/test_admin.py | 126 ++++++++++++++++++++++++++++-- 3 files changed, 149 insertions(+), 16 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 7439217a1..0e86e7764 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1156,7 +1156,7 @@ def do_extend_expiration_date(self, request, obj): return None # Get the date we want to update to - desired_date = date.today() + relativedelta(years=1) + desired_date = self._get_current_date() + relativedelta(years=1) # Grab the current expiration date try: @@ -1164,7 +1164,7 @@ def do_extend_expiration_date(self, request, obj): 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() + exp_date = self._get_current_date() # If the expiration date is super old (2020, for example), we need to # "catch up" to the current year, so we add the difference. @@ -1178,9 +1178,10 @@ def do_extend_expiration_date(self, request, obj): # Renew the domain. try: obj.renew_domain(length=years) + self.message_user( request, - f"Successfully extended the expiration date.", + "Successfully extended the expiration date.", ) except RegistryError as err: if err.is_connection_error(): @@ -1203,6 +1204,13 @@ def do_extend_expiration_date(self, request, obj): return HttpResponseRedirect(".") + # Workaround for unit tests, as we cannot mock date directly. + # it is immutable. Rather than dealing with a convoluted workaround, + # lets wrap this in a function. + def _get_current_date(self): + """Gets the current date""" + return date.today() + 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/tests/common.py b/src/registrar/tests/common.py index 023e5319e..e3bb2adc9 100644 --- a/src/registrar/tests/common.py +++ b/src/registrar/tests/common.py @@ -920,6 +920,11 @@ def dummyInfoContactResultData( ex_date=datetime.date(2023, 5, 25), ) + mockButtonRenewedDomainExpDate = fakedEppObject( + "fakefuture.gov", + ex_date=datetime.date(2025, 5, 25), + ) + mockDnsNeededRenewedDomainExpDate = fakedEppObject( "fakeneeded.gov", ex_date=datetime.date(2023, 2, 15), @@ -1031,6 +1036,7 @@ def mockDeleteDomainCommands(self, _request, cleaned): return None def mockRenewDomainCommand(self, _request, cleaned): + print(f"What is the request at this time? {_request}") if getattr(_request, "name", None) == "fake-error.gov": raise RegistryError(code=ErrorCode.PARAMETER_VALUE_RANGE_ERROR) elif getattr(_request, "name", None) == "waterbutpurple.gov": @@ -1048,11 +1054,20 @@ def mockRenewDomainCommand(self, _request, cleaned): res_data=[self.mockMaximumRenewedDomainExpDate], code=ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY, ) - else: - return MagicMock( - res_data=[self.mockRenewedDomainExpDate], - code=ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY, - ) + elif getattr(_request, "name", None) == "fake.gov": + period = getattr(_request, "period", None) + extension_period = getattr(period, "length", None) + + if extension_period == 2: + return MagicMock( + res_data=[self.mockButtonRenewedDomainExpDate], + code=ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY, + ) + else: + return MagicMock( + res_data=[self.mockRenewedDomainExpDate], + code=ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY, + ) def mockInfoDomainCommands(self, _request, cleaned): request_name = getattr(_request, "name", None) diff --git a/src/registrar/tests/test_admin.py b/src/registrar/tests/test_admin.py index 6cc6f96ea..f5a60011c 100644 --- a/src/registrar/tests/test_admin.py +++ b/src/registrar/tests/test_admin.py @@ -49,6 +49,7 @@ class TestDomainAdmin(MockEppLib, WebTest): csrf_checks = False + def setUp(self): self.site = AdminSite() self.admin = DomainAdmin(model=Domain, admin_site=self.site) @@ -61,6 +62,56 @@ def setUp(self): super().setUp() def test_extend_expiration_date_button(self): + """ + Tests if extend_expiration_date button extends correctly + """ + + # Create a ready domain with a preset expiration date + domain, _ = Domain.objects.get_or_create(name="fake.gov", state=Domain.State.READY) + + response = self.app.get(reverse("admin:registrar_domain_change", args=[domain.pk])) + + # Make sure that the page is loading as expected + self.assertEqual(response.status_code, 200) + self.assertContains(response, domain.name) + self.assertContains(response, "Extend expiration date") + + # Grab the form to submit + form = response.forms["domain_form"] + + with patch("django.contrib.messages.add_message") as mock_add_message: + # Submit the form + response = form.submit("_extend_expiration_date") + + # Follow the response + response = response.follow() + + # refresh_from_db() does not work for objects with protected=True. + # https://github.com/viewflow/django-fsm/issues/89 + new_domain = Domain.objects.get(id=domain.id) + + # Check that the current expiration date is what we expect + self.assertEqual(new_domain.expiration_date, date(2025, 5, 25)) + + # Assert that everything on the page looks correct + self.assertEqual(response.status_code, 200) + self.assertContains(response, domain.name) + self.assertContains(response, "Extend expiration date") + + # Ensure the message we recieve is in line with what we expect + expected_message = "Successfully extended the expiration date." + expected_call = call( + # The WGSI request doesn't need to be tested + ANY, + messages.INFO, + expected_message, + extra_tags="", + fail_silently=False, + ) + mock_add_message.assert_has_calls([expected_call], 1) + + @patch("registrar.admin.DomainAdmin._get_current_date", return_value=date(2024, 1, 1)) + def test_extend_expiration_date_button_epp(self, mock_date_today): """ Tests if extend_expiration_date button sends the right epp command """ @@ -74,7 +125,7 @@ def test_extend_expiration_date_button(self): self.assertEqual(response.status_code, 200) self.assertContains(response, domain.name) self.assertContains(response, "Extend expiration date") - + # Grab the form to submit form = response.forms["domain_form"] @@ -85,14 +136,73 @@ def test_extend_expiration_date_button(self): # Follow the response response = response.follow() - - # We need to use date.today() here, as it is not trivial - # to mock "date.today()". To do so requires libraries like freezegun, - # or convoluted workarounds. - extension_length = (date.today().year + 1) - 2023 - # Assert that it is calling the function + # This value is based off of the current year - the expiration date. + # We "freeze" time to 2024, so 2024 - 2023 will always result in an + # "extension" of 2, as that will be one year of extension from that date. + extension_length = 2 + + # Assert that it is calling the function with the right extension length. + # We only need to test the value that EPP sends, as we can assume the other + # test cases cover the "renew" function. renew_mock.assert_has_calls([call(length=extension_length)], any_order=False) + + # We should not make duplicate calls + self.assertEqual(renew_mock.call_count, 1) + + # Assert that everything on the page looks correct + self.assertEqual(response.status_code, 200) + self.assertContains(response, domain.name) + self.assertContains(response, "Extend expiration date") + + # Ensure the message we recieve is in line with what we expect + expected_message = "Successfully extended the expiration date." + expected_call = call( + # The WGSI request doesn't need to be tested + ANY, + messages.INFO, + expected_message, + extra_tags="", + fail_silently=False, + ) + mock_add_message.assert_has_calls([expected_call], 1) + + @patch("registrar.admin.DomainAdmin._get_current_date", return_value=date(2023, 1, 1)) + def test_extend_expiration_date_button_date_matches_epp(self, mock_date_today): + """ + Tests if extend_expiration_date button sends the right epp command + when the current year matches the expiration date + """ + + # Create a ready domain with a preset expiration date + domain, _ = Domain.objects.get_or_create(name="fake.gov", state=Domain.State.READY) + + response = self.app.get(reverse("admin:registrar_domain_change", args=[domain.pk])) + + # Make sure that the page is loading as expected + self.assertEqual(response.status_code, 200) + self.assertContains(response, domain.name) + self.assertContains(response, "Extend expiration date") + + # Grab the form to submit + form = response.forms["domain_form"] + + with patch("django.contrib.messages.add_message") as mock_add_message: + with patch("registrar.models.Domain.renew_domain") as renew_mock: + # Submit the form + response = form.submit("_extend_expiration_date") + + # Follow the response + response = response.follow() + + extension_length = 1 + + # Assert that it is calling the function with the right extension length. + # We only need to test the value that EPP sends, as we can assume the other + # test cases cover the "renew" function. + renew_mock.assert_has_calls([call(length=extension_length)], any_order=False) + + # We should not make duplicate calls self.assertEqual(renew_mock.call_count, 1) # Assert that everything on the page looks correct @@ -101,7 +211,7 @@ def test_extend_expiration_date_button(self): self.assertContains(response, "Extend expiration date") # Ensure the message we recieve is in line with what we expect - expected_message = f"Successfully extended the expiration date." + expected_message = "Successfully extended the expiration date." expected_call = call( # The WGSI request doesn't need to be tested ANY, From d989d9581a70884fa9fad2ea9f5b2dfb4713e4d6 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 9 Feb 2024 15:35:07 -0700 Subject: [PATCH 31/55] Minor code cleanup --- src/registrar/admin.py | 25 ++-- .../django/admin/domain_change_form.html | 115 ++++++++++-------- 2 files changed, 71 insertions(+), 69 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 0e86e7764..393cdf23d 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -2,19 +2,17 @@ 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.http import HttpResponse, HttpResponseRedirect from django.shortcuts import redirect from django_fsm import get_available_FIELD_transitions from django.contrib import admin, messages from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.models import Group from django.contrib.contenttypes.models import ContentType -from django.http.response import HttpResponseRedirect from django.urls import reverse +from dateutil.relativedelta import relativedelta # type: ignore from epplibwrapper.errors import ErrorCode, RegistryError -from registrar.models.domain import Domain -from registrar.models.user import User +from registrar.models import Domain, User from registrar.utility import csv_export from registrar.views.utility.mixins import OrderableFieldsMixin from django.contrib.admin.views.main import ORDER_VAR @@ -1170,15 +1168,14 @@ def do_extend_expiration_date(self, request, obj): # "catch up" to the current year, so we add the difference. # If both years match, then lets just proceed as normal. calculated_exp_date = exp_date + relativedelta(years=1) - years = 1 - if desired_date > calculated_exp_date: - year_difference = desired_date.year - exp_date.year - years = year_difference + + year_difference = desired_date.year - exp_date.year + # Max probably isn't needed here (no code flow), but it guards against negative and 0. + years = max(1, year_difference) if desired_date > calculated_exp_date else 1 # Renew the domain. try: obj.renew_domain(length=years) - self.message_user( request, "Successfully extended the expiration date.", @@ -1188,7 +1185,6 @@ def do_extend_expiration_date(self, request, obj): error_message = "Error connecting to the registry." else: error_message = f"Error extending this domain: {err}." - self.message_user(request, error_message, messages.ERROR) except KeyError: # In normal code flow, a keyerror can only occur when @@ -1362,13 +1358,6 @@ def has_change_permission(self, request, obj=None): return True return super().has_change_permission(request, obj) - def changelist_view(self, request, extra_context=None): - extra_context = extra_context or {} - # Create HTML for the modal button - modal_button = '' - extra_context["modal_button"] = modal_button - return super().changelist_view(request, extra_context=extra_context) - class DraftDomainAdmin(ListHeaderAdmin): """Custom draft domain admin class.""" diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index 28ae01aec..fbb3380a7 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -44,58 +44,71 @@ {% endblock %} {% block submit_buttons_bottom %} -
-
-
- -
- -
- - -
- + {% comment %} + Modals behave very weirdly in django admin. + They tend to "strip out" any injected form elements, leaving only the main form. + In addition, USWDS handles modals by first destroying the element, then repopulating it toward the end of the page. + In effect, this means that the modal is not, and cannot, be surrounded by any form element at compile time. + + The current workaround for this is to use javascript to inject a hidden input, and bind submit of that + element to the click of the confirmation button within this modal. + + This is controlled by the class `dja-form-placeholder` on the button. + + In addition, the modal element MUST be placed low in the DOM. The script loads slower on DJA than on other portions + of the application, so this means that it will briefly "populate", causing unintended visual effects. + {% endcomment %} +
+
+
+ +
+ +
+ +
+ +
+
{{ block.super }} {% endblock %} \ No newline at end of file From 2765f46d3ae7fe65d99bcbb013e2e19e27850a22 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Fri, 9 Feb 2024 15:41:21 -0700 Subject: [PATCH 32/55] Update domain_change_form.html --- src/registrar/templates/django/admin/domain_change_form.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index fbb3380a7..d0fd46800 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -61,13 +61,13 @@
-

+ This will extend the expiration date by one year. +

+

+ Domain: {{ original.name }} +
+ New expiration date: {{ extended_expiration_date }} + {{test}} +

+

+ This action cannot be undone.

@@ -83,7 +92,7 @@