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

This commit is contained in:
David Kennedy 2023-09-01 17:40:54 -04:00
parent b8b947a583
commit 888ddda9e9
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
3 changed files with 49 additions and 8 deletions

View file

@ -164,7 +164,6 @@ class MyHostAdmin(AuditedAdmin):
inlines = [HostIPInline] inlines = [HostIPInline]
class DomainAdmin(ListHeaderAdmin): class DomainAdmin(ListHeaderAdmin):
"""Custom domain admin class to add extra buttons.""" """Custom domain admin class to add extra buttons."""
@ -175,10 +174,12 @@ class DomainAdmin(ListHeaderAdmin):
readonly_fields = ["state"] readonly_fields = ["state"]
def response_change(self, request, obj): def response_change(self, request, obj):
ACTION_BUTTON = "_place_client_hold" PLACE_HOLD = "_place_client_hold"
if ACTION_BUTTON in request.POST: REMOVE_HOLD = "_remove_client_hold"
if PLACE_HOLD in request.POST:
try: try:
obj.place_client_hold() obj.place_client_hold()
obj.save()
except Exception as err: except Exception as err:
self.message_user(request, err, messages.ERROR) self.message_user(request, err, messages.ERROR)
else: else:
@ -191,9 +192,31 @@ class DomainAdmin(ListHeaderAdmin):
% obj.name, % obj.name,
) )
return HttpResponseRedirect(".") 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) 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): class ContactAdmin(ListHeaderAdmin):
"""Custom contact admin class to add search.""" """Custom contact admin class to add search."""

View file

@ -2,7 +2,7 @@ import logging
from datetime import date from datetime import date
from string import digits from string import digits
from django_fsm import FSMField # type: ignore from django_fsm import FSMField, transition # type: ignore
from django.db import models from django.db import models
@ -114,6 +114,12 @@ class Domain(TimeStampedModel, DomainHelper):
# the state is indeterminate # the state is indeterminate
UNKNOWN = "unknown" UNKNOWN = "unknown"
# the ready state for a domain object
READY = "ready"
# when a domain is on hold
ONHOLD="onhold"
class Cache(property): class Cache(property):
""" """
Python descriptor to turn class methods into properties. Python descriptor to turn class methods into properties.
@ -311,13 +317,21 @@ class Domain(TimeStampedModel, DomainHelper):
"""Time to renew. Not implemented.""" """Time to renew. Not implemented."""
raise NotImplementedError() raise NotImplementedError()
@transition(
field="state", source=[State.READY], target=State.ONHOLD
)
def place_client_hold(self): def place_client_hold(self):
"""This domain should not be active.""" """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): def remove_client_hold(self):
"""This domain is okay to be active.""" """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: def __str__(self) -> str:
return self.name return self.name

View file

@ -2,7 +2,11 @@
{% block field_sets %} {% block field_sets %}
<div class="submit-row"> <div class="submit-row">
<input type="submit" value="Place hold" name="_place_client_hold"> {% if original.state == original.State.READY %}
<input type="submit" value="Place hold" name="_place_client_hold">
{% elif original.state == original.State.ONHOLD %}
<input type="submit" value="Remove hold" name="_remove_client_hold">
{% endif %}
</div> </div>
{{ block.super }} {{ block.super }}
{% endblock %} {% endblock %}