Allow reseller account creation within assigned limits
This commit is contained in:
1 parent
39243a8ab3
commit
4a96829adc
8 files changed
+250
-12
No files matched your search
@@ -1,4 +1,6 @@
|
||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import IntegrityError
|
||||
from django.db.models import Q
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import redirect
|
||||
@@ -9,6 +11,7 @@ from django.views.generic.detail import SingleObjectMixin
|
||||
|
||||
from .access import OperatorOnlyMixin, ResellerScopeMixin, reseller_for_user
|
||||
from .models import HostingAccount, HostingPlan, WebProvisioningPlan
|
||||
from .reseller_accounts import create_reseller_account
|
||||
from .web_node import build_web_smoke_bundle, serialize_deployment_manifest
|
||||
|
||||
VIEW_ACCOUNTS = "hosting.view_hostingaccount"
|
||||
@@ -38,7 +41,14 @@ class AccountList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
|
||||
return accounts
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
return super().get_context_data(search=self.search, **kwargs)
|
||||
context = super().get_context_data(search=self.search, **kwargs)
|
||||
reseller = reseller_for_user(self.request.user)
|
||||
if reseller is not None and reseller.may_create_accounts():
|
||||
context["account_allowance"] = {
|
||||
"used": reseller.hostingaccount_set.count(),
|
||||
"limit": reseller.account_limit,
|
||||
}
|
||||
return context
|
||||
|
||||
|
||||
class AccountEditor:
|
||||
@@ -47,16 +57,24 @@ class AccountEditor:
|
||||
|
||||
def get_form_class(self):
|
||||
self.fields = ("name", "primary_domain")
|
||||
if self.request.user.has_perm(VIEW_HOSTING_PLANS):
|
||||
reseller = reseller_for_user(self.request.user)
|
||||
if reseller is not None or self.request.user.has_perm(VIEW_HOSTING_PLANS):
|
||||
self.fields += ("hosting_plan",)
|
||||
if self.request.user.has_perm(VIEW_RESELLERS):
|
||||
if reseller is None and self.request.user.has_perm(VIEW_RESELLERS):
|
||||
self.fields += ("reseller",)
|
||||
return super().get_form_class()
|
||||
|
||||
def get_form(self, form_class=None):
|
||||
form = super().get_form(form_class)
|
||||
if "hosting_plan" in form.fields:
|
||||
form.fields["hosting_plan"].empty_label = "Unassigned"
|
||||
field = form.fields["hosting_plan"]
|
||||
reseller = reseller_for_user(self.request.user)
|
||||
if reseller is None:
|
||||
field.empty_label = "Unassigned"
|
||||
else:
|
||||
field.queryset = reseller.hosting_plans.all()
|
||||
field.required = True
|
||||
field.empty_label = "Select a hosting plan"
|
||||
if "reseller" in form.fields:
|
||||
form.fields["reseller"].empty_label = "Unassigned"
|
||||
form.fields["reseller"].queryset = (
|
||||
@@ -67,12 +85,36 @@ class AccountEditor:
|
||||
return form
|
||||
|
||||
|
||||
class AccountCreate(
|
||||
OperatorOnlyMixin, PermissionRequiredMixin, AccountEditor, CreateView
|
||||
):
|
||||
class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView):
|
||||
permission_required = (VIEW_ACCOUNTS, "hosting.add_hostingaccount")
|
||||
extra_context = {"page_title": "Add hosting account", "submit_label": "Add account"}
|
||||
|
||||
def has_permission(self):
|
||||
reseller = reseller_for_user(self.request.user)
|
||||
if reseller is not None:
|
||||
return reseller.may_create_accounts()
|
||||
return super().has_permission()
|
||||
|
||||
def form_valid(self, form):
|
||||
reseller = reseller_for_user(self.request.user)
|
||||
if reseller is None:
|
||||
return super().form_valid(form)
|
||||
|
||||
self.object = form.save(commit=False)
|
||||
try:
|
||||
create_reseller_account(reseller, self.object)
|
||||
except ValidationError as error:
|
||||
form.add_error(None, error)
|
||||
return self.form_invalid(form)
|
||||
except IntegrityError:
|
||||
if not HostingAccount.objects.filter(
|
||||
primary_domain=self.object.primary_domain
|
||||
).exists():
|
||||
raise
|
||||
form.add_error("primary_domain", "This domain is unavailable.")
|
||||
return self.form_invalid(form)
|
||||
return redirect(self.object)
|
||||
|
||||
|
||||
class AccountEdit(
|
||||
OperatorOnlyMixin, PermissionRequiredMixin, AccountEditor, UpdateView
|
||||
|
||||
Reference in new issue
Block a user