Backend stuff

This commit is contained in:
zandercymatics 2024-02-02 15:33:42 -07:00
parent 055d163c3f
commit 2891391382
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 32 additions and 8 deletions

View file

@ -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 @@ class DomainAdmin(ListHeaderAdmin):
"_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 @@ class DomainAdmin(ListHeaderAdmin):
# 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,

View file

@ -239,22 +239,25 @@ class Domain(TimeStampedModel, DomainHelper):
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
# If no date is specified, grab the registry_expiration_date
if date_to_extend is None:
try:
cur_exp_date = self.registry_expiration_date
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")
cur_exp_date = date.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