120 lines
4.0 KiB
Python
120 lines
4.0 KiB
Python
from django import forms
|
|
from django.conf import settings
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
|
from django.core.exceptions import ValidationError
|
|
from django.http import Http404
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
from django.urls import reverse
|
|
from django.utils.functional import cached_property
|
|
from django.views.generic import FormView, ListView
|
|
|
|
from .access import OperatorOnlyMixin
|
|
from .account_retirement import (
|
|
Policy,
|
|
account_inventory,
|
|
inventory_digest,
|
|
request_retirement,
|
|
retry_retirement,
|
|
)
|
|
from .models import AccountRetirement, HostingAccount
|
|
|
|
RETIRE_ACCOUNTS = (
|
|
"hosting.view_hostingaccount",
|
|
"hosting.retire_hosting_account",
|
|
)
|
|
|
|
|
|
class AccountRetirementForm(forms.Form):
|
|
policy = forms.ChoiceField(choices=Policy.choices, initial=Policy.HOLD)
|
|
confirmation = forms.CharField(label="Primary domain", max_length=253)
|
|
digest = forms.CharField(
|
|
widget=forms.HiddenInput, min_length=64, max_length=64
|
|
)
|
|
|
|
|
|
class RetirementList(OperatorOnlyMixin, PermissionRequiredMixin, ListView):
|
|
queryset = AccountRetirement.objects.select_related("account")
|
|
permission_required = RETIRE_ACCOUNTS
|
|
template_name = "hosting/retirement_list.html"
|
|
context_object_name = "retirements"
|
|
paginate_by = 20
|
|
|
|
|
|
class RetirementView(OperatorOnlyMixin, PermissionRequiredMixin, FormView):
|
|
permission_required = RETIRE_ACCOUNTS
|
|
template_name = "hosting/account_retirement.html"
|
|
form_class = AccountRetirementForm
|
|
|
|
@cached_property
|
|
def account(self):
|
|
return get_object_or_404(HostingAccount, pk=self.kwargs["pk"])
|
|
|
|
@cached_property
|
|
def retirement(self):
|
|
return getattr(self.account, "retirement", None)
|
|
|
|
@cached_property
|
|
def inventory(self):
|
|
if self.retirement is not None:
|
|
return self.retirement.inventory
|
|
return account_inventory(self.account)
|
|
|
|
def get_initial(self):
|
|
return {
|
|
"digest": inventory_digest(self.inventory),
|
|
"policy": Policy.RELEASE
|
|
if self.retirement is not None
|
|
else Policy.HOLD,
|
|
}
|
|
|
|
def get_context_data(self, **kwargs):
|
|
form = kwargs.get("form")
|
|
if form is None:
|
|
form = self.get_form()
|
|
jobs = list(self.retirement.jobs.all()) if self.retirement else []
|
|
return super().get_context_data(
|
|
form=form,
|
|
account=self.account,
|
|
retirement=self.retirement,
|
|
inventory=self.inventory,
|
|
jobs=jobs,
|
|
failures=any(job.problem for job in jobs),
|
|
can_view_backups=settings.LOCAL_ACCOUNT_BACKUPS
|
|
and self.request.user.has_perms(
|
|
(
|
|
"hosting.add_accountbackupjob",
|
|
"hosting.view_webprovisioningplan",
|
|
"hosting.view_hostingdatabase",
|
|
)
|
|
),
|
|
may_choose_policy=not self.retirement
|
|
or self.retirement.policy == Policy.HOLD,
|
|
)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
if request.POST.get("action") == "retry":
|
|
if not self.retirement:
|
|
raise Http404
|
|
retry_retirement(self.account.pk, request.user.get_username())
|
|
return redirect(self.get_success_url())
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
def form_valid(self, form):
|
|
try:
|
|
request_retirement(
|
|
self.account.pk,
|
|
form.cleaned_data["policy"],
|
|
form.cleaned_data["confirmation"],
|
|
form.cleaned_data["digest"],
|
|
self.request.user.get_username(),
|
|
)
|
|
except ValidationError as error:
|
|
form.add_error(None, error)
|
|
return self.render_to_response(
|
|
self.get_context_data(form=form), status=409
|
|
)
|
|
return super().form_valid(form)
|
|
|
|
def get_success_url(self):
|
|
return reverse("panel:account_retirement", args=[self.account.pk])
|