added functions to admin buttons

This commit is contained in:
Alysia Broddrick 2023-08-10 17:34:09 -07:00
parent d3bc00fdce
commit d1a5f6943c
No known key found for this signature in database
GPG key ID: 03917052CD0F06B7
5 changed files with 99 additions and 51 deletions

View file

@ -31,7 +31,7 @@ Finally, you'll need to craft a request and send it.
```
request = ...
response = registry.send(request)
response = registry.send(request, cleaned=True)
```
Note that you'll need to attest that the data you are sending has been sanitized to remove malicious or invalid strings. Use `send(..., cleaned=True)` to do that.

View file

@ -83,7 +83,7 @@ class EPPLibWrapper:
logger.warning(message, cmd_type, exc_info=True)
raise RegistryError(message) from err
except Exception as err:
message = "%s failed to execute due to an unknown error."
message = '%s failed to execute due to an unknown error.' % err
logger.warning(message, cmd_type, exc_info=True)
raise RegistryError(message) from err
else:

View file

@ -124,10 +124,15 @@ class DomainAdmin(ListHeaderAdmin):
readonly_fields = ["state"]
def response_change(self, request, obj):
print(request.POST)
ACTION_BUTTON = "_place_client_hold"
GET_SECURITY_EMAIL="_get_security_contact"
SET_SECURITY_EMAIL="_set_security_contact"
GET_SECURITY_EMAIL="_get_security_email"
SET_SECURITY_CONTACT="_set_security_contact"
MAKE_DOMAIN="_make_domain_in_registry"
logger.info("in response")
if ACTION_BUTTON in request.POST:
logger.info("in action button")
print("in action button")
try:
obj.place_client_hold()
except Exception as err:
@ -160,27 +165,62 @@ class DomainAdmin(ListHeaderAdmin):
)
return HttpResponseRedirect(".")
return super().response_change(request, obj)
def response_change(self, request, obj):
ACTION_BUTTON = "_get_security_email"
if ACTION_BUTTON in request.POST:
if SET_SECURITY_CONTACT in request.POST:
try:
obj.security
security_contact = obj.get_default_security_contact()
security_contact.email="ab@test.gov"
obj.security_contact=security_contact
except Exception as err:
self.message_user(request, err, messages.ERROR)
else:
self.message_user(
request,
self.message_user(request,
(
"%s is in client hold. This domain is no longer accessible on"
" the public internet."
"The security email is %"
". Thanks!"
)
% security_email,
)
print("above make domain")
if MAKE_DOMAIN in request.POST:
print("in make domain")
try:
obj._get_or_create_domain()
except Exception as err:
self.message_user(request, err, messages.ERROR)
else:
self.message_user(request,
(
"Domain created with %"
". Thanks!"
)
% obj.name,
)
return HttpResponseRedirect(".")
return super().response_change(request, obj)
# def response_change(self, request, obj):
# ACTION_BUTTON = "_get_security_email"
# if ACTION_BUTTON in request.POST:
# try:
# obj.security
# except Exception as err:
# self.message_user(request, err, messages.ERROR)
# else:
# self.message_user(
# request,
# (
# "%s is in client hold. This domain is no longer accessible on"
# " the public internet."
# )
# % obj.name,
# )
# return HttpResponseRedirect(".")
# return super().response_change(request, obj)
class ContactAdmin(ListHeaderAdmin):

View file

@ -103,15 +103,19 @@ class Domain(TimeStampedModel, DomainHelper):
class State(models.TextChoices):
"""These capture (some of) the states a domain object can be in."""
# the state is indeterminate
UNKNOWN = "unknown"
# the normal state of a domain object -- may or may not be active!
#The domain object exists in the registry but nameservers don't exist for it yet
PENDING_CREATE="pending create"
# Domain has had nameservers set, may or may not be active
CREATED = "created"
# previously existed but has been deleted from the registry
DELETED = "deleted"
# the state is indeterminate
UNKNOWN = "unknown"
class Cache(property):
"""
@ -284,11 +288,12 @@ class Domain(TimeStampedModel, DomainHelper):
updateDomain=commands.UpdateDomain(name=self.name, add=[domainContact] )
if rem:
updateDomain=commands.UpdateDomain(name=self.name, rem=[domainContact] )
logger.info("Send updated")
try:
registry.send(updateDomain, cleaned=True)
except RegistryError as e:
logger.error("Error removing old secuity contact code was %s error was %s" % (e.code, e))
logger.error("Error removing old security contact code was %s error was %s" % (e.code, e))
@Cache
def security_contact(self) -> PublicContact:
"""Get or set the security contact for this domain."""
@ -340,20 +345,20 @@ class Domain(TimeStampedModel, DomainHelper):
#create update domain command with security contact
current_security_contact=self.security_contact
if self.security_contact.email is not None:
#if there is already a security contact
domainContact=epp.DomainContact(contact=current_security_contact.registry_id,type=current_security_contact.contact_type)
updateDomain=commands.UpdateDomain(name=self.name, rem=[domainContact] )
try:
registry.send(updateDomain, cleaned=True)
except RegistryError as e:
logger.error("Error removing old secuity contact code was %s error was %s" % (e.code, e))
# current_security_contact=self.security_contact
# if current_security_contact.email is not None:
# #if there is already a security contact
# domainContact=epp.DomainContact(contact=current_security_contact.registry_id,type=current_security_contact.contact_type)
# updateDomain=commands.UpdateDomain(name=self.name, rem=[domainContact] )
# try:
# registry.send(updateDomain, cleaned=True)
# except RegistryError as e:
# logger.error("Error removing old secuity contact code was %s error was %s" % (e.code, e))
addDomainContact=epp.DomainContact(contact=contact.registry_id,type=contact.contact_type)
updateDomainAdd=commands.UpdateDomain(name=self.name, rem=[addDomainContact] )
updateDomainAddContact=commands.UpdateDomain(name=self.name, rem=[addDomainContact] )
try:
registry.send(updateDomainAdd, cleaned=True)
registry.send(updateDomainAddContact, cleaned=True)
except RegistryError as e:
logger.error("Error removing old security contact code was %s error was %s" % (e.code, e))
@ -479,7 +484,7 @@ class Domain(TimeStampedModel, DomainHelper):
)
#TODO-notes no chg item for registrant in the epplib should
already_tried_to_create = True
security_contact = self._get_or_create_contact(self.get_default_security_contact())
req = commands.CreateDomain(
@ -495,9 +500,10 @@ class Domain(TimeStampedModel, DomainHelper):
logger.info("_get_or_create_domain()-> registry received create for "+self.name)
logger.info(response)
# no error, so go ahead and update state
self.state = Domain.State.CREATED
self.state = Domain.State.PENDING_CREATE
self.save()
self._update_domain_with_contact(security_contact)
self._update_domain_with_contact(security_contact, rem=False)
def _make_contact_in_registry(self, contact: PublicContact):
"""Create the contact in the registry, ignore duplicate contact errors"""
create = commands.CreateContact(
@ -535,7 +541,7 @@ class Domain(TimeStampedModel, DomainHelper):
types={DF.ADDR: "loc"},
)
try:
registry.send(create)
registry.send(create, cleaned=True)
return contact
except RegistryError as err:
#don't throw an error if it is just saying this is a duplicate contact

View file

@ -3,8 +3,10 @@
{% block field_sets %}
<div class="submit-row">
<input type="submit" value="Place hold" name="_place_client_hold">
<input type="submit" value="Place hold" name="_get_security_email">
<input type="submit" value="Place hold" name="_set_security_contact">
<input type="submit" value="Get the email" name="_get_security_email">
<input type="submit" value="Set Security Contact" name="_set_security_contact">
<input type="submit" value="Create the domain obj" name="_make_domain_in_registry">
<input type="submit" value="Create the domain obj" name="_make_domain_in_registry">
</div>
{{ block.super }}
{% endblock %}