From 888ddda9e982e46e1b23eaa1783f4584bff904bb Mon Sep 17 00:00:00 2001 From: David Kennedy Date: Fri, 1 Sep 2023 17:40:54 -0400 Subject: [PATCH] implemented add client hold and remove client hold; extended change permission on domain admin to staff; conditionally display buttons based on state; added states to domain to match state diagram --- src/registrar/admin.py | 31 ++++++++++++++++--- src/registrar/models/domain.py | 20 ++++++++++-- .../django/admin/domain_change_form.html | 6 +++- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 4696a15bf..f4b395524 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -164,7 +164,6 @@ class MyHostAdmin(AuditedAdmin): inlines = [HostIPInline] - class DomainAdmin(ListHeaderAdmin): """Custom domain admin class to add extra buttons.""" @@ -175,10 +174,12 @@ class DomainAdmin(ListHeaderAdmin): readonly_fields = ["state"] def response_change(self, request, obj): - ACTION_BUTTON = "_place_client_hold" - if ACTION_BUTTON in request.POST: + PLACE_HOLD = "_place_client_hold" + REMOVE_HOLD = "_remove_client_hold" + if PLACE_HOLD in request.POST: try: obj.place_client_hold() + obj.save() except Exception as err: self.message_user(request, err, messages.ERROR) else: @@ -191,9 +192,31 @@ class DomainAdmin(ListHeaderAdmin): % obj.name, ) return HttpResponseRedirect(".") - + elif REMOVE_HOLD in request.POST: + try: + obj.remove_client_hold() + obj.save() + except Exception as err: + self.message_user(request, err, messages.ERROR) + else: + self.message_user( + request, + ( + "%s is ready. This domain is accessible on the public " + "internet." + ) + % obj.name, + ) + return HttpResponseRedirect(".") return super().response_change(request, obj) + def has_change_permission(self, request, obj=None): + # Fixes a bug wherein users which are only is_staff + # can access 'change' when GET, + # but cannot access this page when it is a request of type POST. + if request.user.is_staff: + return True + return super().has_change_permission(request, obj) class ContactAdmin(ListHeaderAdmin): """Custom contact admin class to add search.""" diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index a7e46f888..2192bd370 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -2,7 +2,7 @@ import logging from datetime import date from string import digits -from django_fsm import FSMField # type: ignore +from django_fsm import FSMField, transition # type: ignore from django.db import models @@ -114,6 +114,12 @@ class Domain(TimeStampedModel, DomainHelper): # the state is indeterminate UNKNOWN = "unknown" + # the ready state for a domain object + READY = "ready" + + # when a domain is on hold + ONHOLD="onhold" + class Cache(property): """ Python descriptor to turn class methods into properties. @@ -311,13 +317,21 @@ class Domain(TimeStampedModel, DomainHelper): """Time to renew. Not implemented.""" raise NotImplementedError() + @transition( + field="state", source=[State.READY], target=State.ONHOLD + ) def place_client_hold(self): """This domain should not be active.""" - raise NotImplementedError("This is not implemented yet.") + # This method is changing the state of the domain in registrar + # TODO: implement EPP call + @transition( + field="state", source=[State.ONHOLD], target=State.READY + ) def remove_client_hold(self): """This domain is okay to be active.""" - raise NotImplementedError() + # This method is changing the state of the domain in registrar + # TODO: implement EPP call def __str__(self) -> str: return self.name diff --git a/src/registrar/templates/django/admin/domain_change_form.html b/src/registrar/templates/django/admin/domain_change_form.html index 5fa89f20a..933d7ae25 100644 --- a/src/registrar/templates/django/admin/domain_change_form.html +++ b/src/registrar/templates/django/admin/domain_change_form.html @@ -2,7 +2,11 @@ {% block field_sets %}
- + {% if original.state == original.State.READY %} + + {% elif original.state == original.State.ONHOLD %} + + {% endif %}
{{ block.super }} {% endblock %} \ No newline at end of file