Add hosting plans and account assignment

Manage hosting plans in the panel and allow authorized operators to
assign them to hosting accounts.
This commit is contained in:
parkel committed 2026-11-19 22:09:10 +01:00
1 parent e59ee72940
commit bb95b86173
13 files changed
+346 -15

No files matched your search

+18 -1
View File
@@ -1,7 +1,7 @@
from django.contrib import admin
from django.utils.html import format_html
from .models import HostingAccount, WebProvisioningPlan
from .models import HostingAccount, HostingPlan, WebProvisioningPlan
@admin.register(HostingAccount)
@@ -11,6 +11,17 @@ class HostingAccountAdmin(admin.ModelAdmin):
ordering = ("primary_domain",)
actions = ("request_web_plans",)
def get_exclude(self, request, obj=None):
if not request.user.has_perm("hosting.view_hostingplan"):
return ("hosting_plan",)
return super().get_exclude(request, obj)
def save_model(self, request, obj, form, change):
if change:
obj.save(update_fields=form.changed_data)
else:
super().save_model(request, obj, form, change)
@admin.action(permissions=["change"], description="Request Apache plans")
def request_web_plans(self, request, queryset):
plans = WebProvisioningPlan.objects.bulk_create(
@@ -20,6 +31,12 @@ class HostingAccountAdmin(admin.ModelAdmin):
self.message_user(request, f"Queued {len(plans)} Apache plan(s).")
@admin.register(HostingPlan)
class HostingPlanAdmin(admin.ModelAdmin):
list_display = ("name", "description")
search_fields = ("name",)
@admin.register(WebProvisioningPlan)
class WebProvisioningPlanAdmin(admin.ModelAdmin):
list_display = ("primary_domain", "account", "requested_at", "completed_at")
@@ -0,0 +1,42 @@
# Generated by Django 5.2.19 on 2026-11-19 21:07
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("hosting", "0002_webprovisioningplan"),
]
operations = [
migrations.CreateModel(
name="HostingPlan",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=120, unique=True)),
("description", models.CharField(blank=True, max_length=240)),
],
options={
"ordering": ("name", "pk"),
},
),
migrations.AddField(
model_name="hostingaccount",
name="hosting_plan",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="hosting.hostingplan",
),
),
]
+14
View File
@@ -4,9 +4,23 @@ from django.urls import reverse
from .fields import DomainNameField
class HostingPlan(models.Model):
name = models.CharField(max_length=120, unique=True)
description = models.CharField(max_length=240, blank=True)
class Meta:
ordering = ("name", "pk")
def __str__(self):
return self.name
class HostingAccount(models.Model):
name = models.CharField(max_length=120)
primary_domain = DomainNameField(max_length=253, unique=True)
hosting_plan = models.ForeignKey(
HostingPlan, on_delete=models.PROTECT, null=True, blank=True
)
created_at = models.DateTimeField(auto_now_add=True)
def get_absolute_url(self):
@@ -29,7 +29,7 @@ a {
text-underline-offset: 0.2em;
}
button, input {
button, input, select {
font: inherit;
}
@@ -155,6 +155,10 @@ button, input {
linear-gradient(currentColor, currentColor) center 65% / 55% 1px no-repeat;
}
.nav-icon-packages {
background: linear-gradient(currentColor, currentColor) center 35% / 40% 1px no-repeat;
}
.sidebar-account {
display: grid;
gap: 0.3rem;
@@ -288,7 +292,7 @@ h2 {
font-size: 0.88rem;
}
input:not([type="hidden"]) {
input:not([type="hidden"]), select {
min-width: 0;
height: 2.55rem;
padding: 0.48rem 0.68rem;
@@ -463,7 +467,7 @@ dd {
font-weight: 680;
}
.editor input:not([type="hidden"]) {
.editor input:not([type="hidden"]), .editor select {
width: 100%;
}
@@ -13,6 +13,9 @@
</header>
<dl class="details surface">
<div><dt>Primary domain</dt><dd class="domain">{{ account.primary_domain }}</dd></div>
{% if perms.hosting.view_hostingplan %}
<div><dt>Hosting plan</dt><dd>{% if account.hosting_plan %}{{ account.hosting_plan.name }}{% else %}Unassigned{% endif %}</dd></div>
{% endif %}
<div><dt>Added</dt><dd>{{ account.created_at|date:"j M Y, H:i" }} UTC</dd></div>
</dl>
{% if perms.hosting.view_webprovisioningplan %}
@@ -21,13 +21,21 @@
<div class="table-scroll">
<table>
<thead>
<tr><th scope="col">Account</th><th scope="col">Primary domain</th><th scope="col" class="cell-date">Added</th></tr>
<tr>
<th scope="col">Account</th>
<th scope="col">Primary domain</th>
{% if perms.hosting.view_hostingplan %}<th scope="col">Hosting plan</th>{% endif %}
<th scope="col" class="cell-date">Added</th>
</tr>
</thead>
<tbody>
{% for account in accounts %}
<tr>
<td><a class="record-link" href="{{ account.get_absolute_url }}">{{ account.name }}</a></td>
<td class="domain">{{ account.primary_domain }}</td>
{% if perms.hosting.view_hostingplan %}
<td>{% if account.hosting_plan %}{{ account.hosting_plan.name }}{% else %}<span class="muted">Unassigned</span>{% endif %}</td>
{% endif %}
<td class="cell-date muted">{{ account.created_at|date:"j M Y" }}</td>
</tr>
{% endfor %}
@@ -23,6 +23,12 @@
Hosting accounts
</a>
{% endif %}
{% if perms.hosting.view_hostingplan %}
<a href="{% url 'panel:hosting_plans' %}"{% if current == 'hosting_plans' or current == 'hosting_plan_create' or current == 'hosting_plan_edit' %} aria-current="page"{% endif %}>
<span class="nav-icon nav-icon-packages" aria-hidden="true"></span>
Hosting plans
</a>
{% endif %}
{% if perms.hosting.view_webprovisioningplan %}
<a href="{% url 'panel:plans' %}"{% if current == 'plans' or current == 'plan' %} aria-current="page"{% endif %}>
<span class="nav-icon nav-icon-plans" aria-hidden="true"></span>
@@ -0,0 +1,16 @@
{% extends "hosting/base.html" %}
{% block title %}{{ page_title }} · Coulobre Panel{% endblock %}
{% block content %}
<a class="back-link" href="{% url 'panel:hosting_plans' %}">← Hosting plans</a>
<header class="page-heading"><h1>{{ page_title }}</h1></header>
<section class="surface editor-surface">
<form method="post" class="editor">
{% csrf_token %}
{{ form.as_div }}
<div class="form-actions">
<button class="button primary" type="submit">{{ submit_label }}</button>
<a class="button secondary" href="{% url 'panel:hosting_plans' %}">Cancel</a>
</div>
</form>
</section>
{% endblock %}
@@ -0,0 +1,36 @@
{% extends "hosting/base.html" %}
{% block title %}Hosting plans · Coulobre Panel{% endblock %}
{% block content %}
<header class="page-heading">
<h1>Hosting plans</h1>
{% if perms.hosting.add_hostingplan %}
<a class="button primary" href="{% url 'panel:hosting_plan_create' %}">Add hosting plan</a>
{% endif %}
</header>
<section class="surface" aria-label="Hosting plans">
{% if hosting_plans %}
<div class="table-scroll">
<table>
<thead><tr><th scope="col">Plan</th><th scope="col">Description</th></tr></thead>
<tbody>
{% for hosting_plan in hosting_plans %}
<tr>
<td>
{% if perms.hosting.change_hostingplan %}
<a class="record-link" href="{% url 'panel:hosting_plan_edit' hosting_plan.pk %}">{{ hosting_plan.name }}</a>
{% else %}
<strong>{{ hosting_plan.name }}</strong>
{% endif %}
</td>
<td>{{ hosting_plan.description|default:"—" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<p class="empty">No hosting plans yet.</p>
{% endif %}
{% include "hosting/pagination.html" %}
</section>
{% endblock %}
+125
View File
@@ -0,0 +1,125 @@
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.db.models.deletion import ProtectedError
from django.test import TestCase
from django.urls import reverse
from .models import HostingAccount, HostingPlan
class HostingPlanTests(TestCase):
@classmethod
def setUpTestData(cls):
users = get_user_model().objects
cls.operator = users.create_superuser("operator")
cls.editor = users.create_user("editor", is_staff=True)
cls.editor.user_permissions.add(
*Permission.objects.filter(
content_type__app_label="hosting",
codename__in=("view_hostingaccount", "change_hostingaccount"),
)
)
cls.account = HostingAccount.objects.create(
name="Example hosting", primary_domain="example.org"
)
def test_operator_can_create_assign_and_edit_a_plan(self):
self.client.force_login(self.operator)
list_url = reverse("panel:hosting_plans")
response = self.client.post(
reverse("panel:hosting_plan_create"),
{"name": "Starter", "description": "Shared hosting for small sites"},
)
self.assertRedirects(response, list_url)
plan = HostingPlan.objects.get()
self.assertContains(
self.client.get(self.account.get_absolute_url()), "Unassigned"
)
response = self.client.post(
reverse("panel:account_edit", args=[self.account.pk]),
{
"name": self.account.name,
"primary_domain": "example.org",
"hosting_plan": plan.pk,
},
)
self.assertRedirects(response, self.account.get_absolute_url())
self.account.refresh_from_db()
self.assertEqual(self.account.hosting_plan_id, plan.pk)
response = self.client.post(
reverse("panel:hosting_plan_edit", args=[plan.pk]),
{"name": "Standard", "description": "Updated description"},
)
self.assertRedirects(response, list_url)
self.assertContains(self.client.get(reverse("panel:accounts")), "Standard")
response = self.client.post(
reverse("panel:account_create"),
{
"name": "New hosting",
"primary_domain": "new.example",
"hosting_plan": plan.pk,
},
)
new_account = HostingAccount.objects.get(primary_domain="new.example")
self.assertRedirects(response, new_account.get_absolute_url())
self.assertEqual(new_account.hosting_plan_id, plan.pk)
def test_editor_without_plan_permission_cannot_assign_in_either_interface(self):
current = HostingPlan.objects.create(name="Private plan")
other = HostingPlan.objects.create(name="Other plan")
HostingAccount.objects.filter(pk=self.account.pk).update(hosting_plan=current)
self.client.force_login(self.editor)
self.assertEqual(
self.client.get(reverse("panel:hosting_plans")).status_code, 403
)
self.assertNotContains(
self.client.get(self.account.get_absolute_url()), current.name
)
for route in ("panel:account_edit", "admin:hosting_hostingaccount_change"):
with self.subTest(route=route):
url = reverse(route, args=[self.account.pk])
self.assertNotContains(self.client.get(url), 'name="hosting_plan"')
response = self.client.post(
url,
{
"name": route,
"primary_domain": "example.org",
"hosting_plan": other.pk,
},
)
self.assertEqual(response.status_code, 302)
self.account.refresh_from_db()
self.assertEqual(self.account.name, route)
self.assertEqual(self.account.hosting_plan_id, current.pk)
def test_unrelated_account_edit_preserves_a_concurrent_plan_assignment(self):
plan = HostingPlan.objects.create(name="Starter")
self.client.force_login(self.editor)
save = HostingAccount.save
def assign_then_save(account, *args, **kwargs):
HostingAccount.objects.filter(pk=account.pk).update(hosting_plan=plan)
return save(account, *args, **kwargs)
with patch.object(HostingAccount, "save", assign_then_save):
response = self.client.post(
reverse("panel:account_edit", args=[self.account.pk]),
{"name": "Renamed hosting", "primary_domain": "example.org"},
)
self.assertRedirects(response, self.account.get_absolute_url())
self.account.refresh_from_db()
self.assertEqual(self.account.name, "Renamed hosting")
self.assertEqual(self.account.hosting_plan_id, plan.pk)
def test_cannot_delete_an_assigned_plan(self):
plan = HostingPlan.objects.create(name="Assigned plan")
HostingAccount.objects.filter(pk=self.account.pk).update(hosting_plan=plan)
with self.assertRaises(ProtectedError):
plan.delete()
+11
View File
@@ -22,6 +22,17 @@ urlpatterns = [
path(
"accounts/<int:pk>/plans/", views.RequestWebPlan.as_view(), name="request_plan"
),
path("hosting-plans/", views.HostingPlanList.as_view(), name="hosting_plans"),
path(
"hosting-plans/new/",
views.HostingPlanCreate.as_view(),
name="hosting_plan_create",
),
path(
"hosting-plans/<int:pk>/edit/",
views.HostingPlanEdit.as_view(),
name="hosting_plan_edit",
),
path("plans/", views.PlanList.as_view(), name="plans"),
path("plans/<int:pk>/", views.PlanDetail.as_view(), name="plan"),
path(
+55 -10
View File
@@ -2,16 +2,18 @@ from django.contrib.auth.mixins import PermissionRequiredMixin
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.views import View
from django.views.generic import CreateView, DetailView, ListView, UpdateView
from django.views.generic.detail import SingleObjectMixin
from .models import HostingAccount, WebProvisioningPlan
from .models import HostingAccount, HostingPlan, WebProvisioningPlan
from .web_node import build_web_smoke_bundle, serialize_deployment_manifest
VIEW_ACCOUNTS = "hosting.view_hostingaccount"
CHANGE_ACCOUNTS = "hosting.change_hostingaccount"
VIEW_PLANS = "hosting.view_webprovisioningplan"
VIEW_HOSTING_PLANS = "hosting.view_hostingplan"
class AccountList(PermissionRequiredMixin, ListView):
@@ -23,7 +25,9 @@ class AccountList(PermissionRequiredMixin, ListView):
def get_queryset(self):
self.search = self.request.GET.get("q", "").strip()[:253]
accounts = HostingAccount.objects.order_by("primary_domain")
accounts = HostingAccount.objects.select_related("hosting_plan").order_by(
"primary_domain"
)
if self.search:
accounts = accounts.filter(
Q(name__icontains=self.search)
@@ -35,28 +39,44 @@ class AccountList(PermissionRequiredMixin, ListView):
return super().get_context_data(search=self.search, **kwargs)
class AccountCreate(PermissionRequiredMixin, CreateView):
class AccountEditor:
model = HostingAccount
permission_required = (VIEW_ACCOUNTS, "hosting.add_hostingaccount")
template_name = "hosting/account_form.html"
fields = ("name", "primary_domain")
def get_form_class(self):
self.fields = ("name", "primary_domain")
if self.request.user.has_perm(VIEW_HOSTING_PLANS):
self.fields += ("hosting_plan",)
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"
return form
class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView):
permission_required = (VIEW_ACCOUNTS, "hosting.add_hostingaccount")
extra_context = {"page_title": "Add hosting account", "submit_label": "Add account"}
class AccountEdit(PermissionRequiredMixin, UpdateView):
model = HostingAccount
class AccountEdit(PermissionRequiredMixin, AccountEditor, UpdateView):
permission_required = (VIEW_ACCOUNTS, CHANGE_ACCOUNTS)
template_name = "hosting/account_form.html"
context_object_name = "account"
fields = ("name", "primary_domain")
extra_context = {
"page_title": "Edit hosting account",
"submit_label": "Save changes",
}
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.save(update_fields=form.changed_data)
return redirect(self.object)
class AccountDetail(PermissionRequiredMixin, DetailView):
model = HostingAccount
queryset = HostingAccount.objects.select_related("hosting_plan")
permission_required = VIEW_ACCOUNTS
template_name = "hosting/account_detail.html"
context_object_name = "account"
@@ -70,6 +90,31 @@ class AccountDetail(PermissionRequiredMixin, DetailView):
return context
class HostingPlanList(PermissionRequiredMixin, ListView):
model = HostingPlan
permission_required = VIEW_HOSTING_PLANS
template_name = "hosting/hosting_plan_list.html"
context_object_name = "hosting_plans"
paginate_by = 20
class HostingPlanEditor:
model = HostingPlan
fields = ("name", "description")
template_name = "hosting/hosting_plan_form.html"
success_url = reverse_lazy("panel:hosting_plans")
class HostingPlanCreate(PermissionRequiredMixin, HostingPlanEditor, CreateView):
permission_required = (VIEW_HOSTING_PLANS, "hosting.add_hostingplan")
extra_context = {"page_title": "Add hosting plan", "submit_label": "Add plan"}
class HostingPlanEdit(PermissionRequiredMixin, HostingPlanEditor, UpdateView):
permission_required = (VIEW_HOSTING_PLANS, "hosting.change_hostingplan")
extra_context = {"page_title": "Edit hosting plan", "submit_label": "Save changes"}
class RequestWebPlan(PermissionRequiredMixin, SingleObjectMixin, View):
model = HostingAccount
permission_required = (VIEW_ACCOUNTS, CHANGE_ACCOUNTS, VIEW_PLANS)