From bb95b861736b624c31890d82486f15777e234f27 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Thu, 19 Nov 2026 22:09:10 +0100 Subject: [PATCH 1/3] Add hosting plans and account assignment Manage hosting plans in the panel and allow authorized operators to assign them to hosting accounts. --- docs/development.md | 4 + src/coulobre/hosting/admin.py | 19 ++- ...hostingplan_hostingaccount_hosting_plan.py | 42 ++++++ src/coulobre/hosting/models.py | 14 ++ src/coulobre/hosting/static/hosting/panel.css | 10 +- .../templates/hosting/account_detail.html | 3 + .../templates/hosting/account_list.html | 10 +- .../hosting/templates/hosting/base.html | 6 + .../templates/hosting/hosting_plan_form.html | 16 +++ .../templates/hosting/hosting_plan_list.html | 36 +++++ src/coulobre/hosting/test_hosting_plans.py | 125 ++++++++++++++++++ src/coulobre/hosting/urls.py | 11 ++ src/coulobre/hosting/views.py | 65 +++++++-- 13 files changed, 346 insertions(+), 15 deletions(-) create mode 100644 src/coulobre/hosting/migrations/0003_hostingplan_hostingaccount_hosting_plan.py create mode 100644 src/coulobre/hosting/templates/hosting/hosting_plan_form.html create mode 100644 src/coulobre/hosting/templates/hosting/hosting_plan_list.html create mode 100644 src/coulobre/hosting/test_hosting_plans.py diff --git a/docs/development.md b/docs/development.md index bbaab14..3f1c3f5 100644 --- a/docs/development.md +++ b/docs/development.md @@ -19,6 +19,10 @@ Select **Add hosting account** and enter a name and a primary domain such as `example.org`. Open an account from the list to edit its details or request an Apache preview. Manage users, groups, and permissions at `/admin/`. +Use **Hosting plans** to add named plans with an optional description. Choose a +plan when creating or editing an account. Existing accounts start unassigned. +Operators need permission to view hosting plans before they can assign one. + The local database defaults to `db.sqlite3`. Set `COULOBRE_SQLITE_PATH` to use a different file. Keep `COULOBRE_DEBUG` for local development. diff --git a/src/coulobre/hosting/admin.py b/src/coulobre/hosting/admin.py index 395052b..6a0e11d 100644 --- a/src/coulobre/hosting/admin.py +++ b/src/coulobre/hosting/admin.py @@ -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") diff --git a/src/coulobre/hosting/migrations/0003_hostingplan_hostingaccount_hosting_plan.py b/src/coulobre/hosting/migrations/0003_hostingplan_hostingaccount_hosting_plan.py new file mode 100644 index 0000000..12fbd4f --- /dev/null +++ b/src/coulobre/hosting/migrations/0003_hostingplan_hostingaccount_hosting_plan.py @@ -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", + ), + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 23eb19a..a6e9643 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -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): diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 4985ffe..986d254 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -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%; } diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 2865053..103b90e 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -13,6 +13,9 @@
Primary domain
{{ account.primary_domain }}
+ {% if perms.hosting.view_hostingplan %} +
Hosting plan
{% if account.hosting_plan %}{{ account.hosting_plan.name }}{% else %}Unassigned{% endif %}
+ {% endif %}
Added
{{ account.created_at|date:"j M Y, H:i" }} UTC
{% if perms.hosting.view_webprovisioningplan %} diff --git a/src/coulobre/hosting/templates/hosting/account_list.html b/src/coulobre/hosting/templates/hosting/account_list.html index 51936b4..4601504 100644 --- a/src/coulobre/hosting/templates/hosting/account_list.html +++ b/src/coulobre/hosting/templates/hosting/account_list.html @@ -21,13 +21,21 @@
- + + + + {% if perms.hosting.view_hostingplan %}{% endif %} + + {% for account in accounts %} + {% if perms.hosting.view_hostingplan %} + + {% endif %} {% endfor %} diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html index 7e39373..de25d10 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -23,6 +23,12 @@ Hosting accounts {% endif %} + {% if perms.hosting.view_hostingplan %} + + + Hosting plans + + {% endif %} {% if perms.hosting.view_webprovisioningplan %} diff --git a/src/coulobre/hosting/templates/hosting/hosting_plan_form.html b/src/coulobre/hosting/templates/hosting/hosting_plan_form.html new file mode 100644 index 0000000..139a181 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/hosting_plan_form.html @@ -0,0 +1,16 @@ +{% extends "hosting/base.html" %} +{% block title %}{{ page_title }} · Coulobre Panel{% endblock %} +{% block content %} + ← Hosting plans +

{{ page_title }}

+
+
+ {% csrf_token %} + {{ form.as_div }} +
+ + Cancel +
+ +
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/hosting_plan_list.html b/src/coulobre/hosting/templates/hosting/hosting_plan_list.html new file mode 100644 index 0000000..da1a082 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/hosting_plan_list.html @@ -0,0 +1,36 @@ +{% extends "hosting/base.html" %} +{% block title %}Hosting plans · Coulobre Panel{% endblock %} +{% block content %} +
+

Hosting plans

+ {% if perms.hosting.add_hostingplan %} + Add hosting plan + {% endif %} +
+
+ {% if hosting_plans %} +
+
AccountPrimary domainAdded
AccountPrimary domainHosting planAdded
{{ account.name }} {{ account.primary_domain }}{% if account.hosting_plan %}{{ account.hosting_plan.name }}{% else %}Unassigned{% endif %}{{ account.created_at|date:"j M Y" }}
+ + + {% for hosting_plan in hosting_plans %} + + + + + {% endfor %} + +
PlanDescription
+ {% if perms.hosting.change_hostingplan %} + {{ hosting_plan.name }} + {% else %} + {{ hosting_plan.name }} + {% endif %} + {{ hosting_plan.description|default:"—" }}
+
+ {% else %} +

No hosting plans yet.

+ {% endif %} + {% include "hosting/pagination.html" %} + +{% endblock %} diff --git a/src/coulobre/hosting/test_hosting_plans.py b/src/coulobre/hosting/test_hosting_plans.py new file mode 100644 index 0000000..7a3e745 --- /dev/null +++ b/src/coulobre/hosting/test_hosting_plans.py @@ -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() diff --git a/src/coulobre/hosting/urls.py b/src/coulobre/hosting/urls.py index 71f798b..dc62262 100644 --- a/src/coulobre/hosting/urls.py +++ b/src/coulobre/hosting/urls.py @@ -22,6 +22,17 @@ urlpatterns = [ path( "accounts//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//edit/", + views.HostingPlanEdit.as_view(), + name="hosting_plan_edit", + ), path("plans/", views.PlanList.as_view(), name="plans"), path("plans//", views.PlanDetail.as_view(), name="plan"), path( diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index 8337bb0..21058f5 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -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) -- 2.43.0 From 39243a8ab3ba4abb27c883656a2ba4639dbc438e Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Thu, 19 Nov 2026 22:16:29 +0100 Subject: [PATCH 2/3] Scope reseller access to assigned hosting accounts Allow operators to assign accounts to resellers and give each reseller read-only access to their own accounts and Apache previews. --- docs/development.md | 11 ++ src/coulobre/admin.py | 15 ++ src/coulobre/hosting/access.py | 31 ++++ src/coulobre/hosting/admin.py | 16 ++- .../0004_reseller_hostingaccount_reseller.py | 48 +++++++ src/coulobre/hosting/models.py | 16 +++ .../templates/hosting/account_detail.html | 11 +- .../templates/hosting/account_list.html | 6 +- .../hosting/templates/hosting/base.html | 6 +- .../templates/hosting/plan_detail.html | 4 +- src/coulobre/hosting/test_resellers.py | 132 ++++++++++++++++++ src/coulobre/hosting/views.py | 59 +++++--- src/coulobre/settings.py | 3 +- 13 files changed, 325 insertions(+), 33 deletions(-) create mode 100644 src/coulobre/admin.py create mode 100644 src/coulobre/hosting/access.py create mode 100644 src/coulobre/hosting/migrations/0004_reseller_hostingaccount_reseller.py create mode 100644 src/coulobre/hosting/test_resellers.py diff --git a/docs/development.md b/docs/development.md index 3f1c3f5..95d7833 100644 --- a/docs/development.md +++ b/docs/development.md @@ -23,6 +23,17 @@ Use **Hosting plans** to add named plans with an optional description. Choose a plan when creating or editing an account. Existing accounts start unassigned. Operators need permission to view hosting plans before they can assign one. +## Give a reseller access to assigned accounts + +In `/admin/`, create a normal user and add a **Reseller** record for that user. +Then open a hosting account in Coulobre Panel and select the reseller when +editing the account. Existing accounts start with no reseller assigned. + +The reseller can sign in to Coulobre Panel to see their assigned accounts, +each account's hosting plan, and its web provisioning previews. Platform +operators manage assignments and node deployment files. Reseller access in +this release is read-only. + The local database defaults to `db.sqlite3`. Set `COULOBRE_SQLITE_PATH` to use a different file. Keep `COULOBRE_DEBUG` for local development. diff --git a/src/coulobre/admin.py b/src/coulobre/admin.py new file mode 100644 index 0000000..59e9741 --- /dev/null +++ b/src/coulobre/admin.py @@ -0,0 +1,15 @@ +from django.contrib.admin import AdminSite +from django.contrib.admin.apps import AdminConfig + +from .hosting.access import reseller_for_user + + +class CoulobreAdminSite(AdminSite): + def has_permission(self, request): + return ( + super().has_permission(request) and reseller_for_user(request.user) is None + ) + + +class CoulobreAdminConfig(AdminConfig): + default_site = "coulobre.admin.CoulobreAdminSite" diff --git a/src/coulobre/hosting/access.py b/src/coulobre/hosting/access.py new file mode 100644 index 0000000..05b7d51 --- /dev/null +++ b/src/coulobre/hosting/access.py @@ -0,0 +1,31 @@ +def reseller_for_user(user): + if not user.is_authenticated or user.is_superuser: + return None + return getattr(user, "hosting_reseller", None) + + +def panel_access(request): + return {"is_reseller": reseller_for_user(request.user) is not None} + + +class OperatorOnlyMixin: + def dispatch(self, request, *args, **kwargs): + if reseller_for_user(request.user) is not None: + return self.handle_no_permission() + return super().dispatch(request, *args, **kwargs) + + +class ResellerScopeMixin: + reseller_relation = "reseller" + + def has_permission(self): + return ( + reseller_for_user(self.request.user) is not None or super().has_permission() + ) + + def get_queryset(self): + queryset = super().get_queryset() + reseller = reseller_for_user(self.request.user) + if reseller is not None: + return queryset.filter(**{f"{self.reseller_relation}_id": reseller.pk}) + return queryset diff --git a/src/coulobre/hosting/admin.py b/src/coulobre/hosting/admin.py index 6a0e11d..24ad0b5 100644 --- a/src/coulobre/hosting/admin.py +++ b/src/coulobre/hosting/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from django.utils.html import format_html -from .models import HostingAccount, HostingPlan, WebProvisioningPlan +from .models import HostingAccount, HostingPlan, Reseller, WebProvisioningPlan @admin.register(HostingAccount) @@ -12,9 +12,12 @@ class HostingAccountAdmin(admin.ModelAdmin): actions = ("request_web_plans",) def get_exclude(self, request, obj=None): + excluded = list(super().get_exclude(request, obj) or ()) if not request.user.has_perm("hosting.view_hostingplan"): - return ("hosting_plan",) - return super().get_exclude(request, obj) + excluded.append("hosting_plan") + if not request.user.has_perm("hosting.view_reseller"): + excluded.append("reseller") + return excluded def save_model(self, request, obj, form, change): if change: @@ -37,6 +40,13 @@ class HostingPlanAdmin(admin.ModelAdmin): search_fields = ("name",) +@admin.register(Reseller) +class ResellerAdmin(admin.ModelAdmin): + list_display = ("user",) + list_select_related = ("user",) + search_fields = ("user__username",) + + @admin.register(WebProvisioningPlan) class WebProvisioningPlanAdmin(admin.ModelAdmin): list_display = ("primary_domain", "account", "requested_at", "completed_at") diff --git a/src/coulobre/hosting/migrations/0004_reseller_hostingaccount_reseller.py b/src/coulobre/hosting/migrations/0004_reseller_hostingaccount_reseller.py new file mode 100644 index 0000000..385b22a --- /dev/null +++ b/src/coulobre/hosting/migrations/0004_reseller_hostingaccount_reseller.py @@ -0,0 +1,48 @@ +# Generated by Django 5.2.19 on 2026-11-19 21:13 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0003_hostingplan_hostingaccount_hosting_plan"), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name="Reseller", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "user", + models.OneToOneField( + limit_choices_to={"is_superuser": False}, + on_delete=django.db.models.deletion.PROTECT, + related_name="hosting_reseller", + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + migrations.AddField( + model_name="hostingaccount", + name="reseller", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="hosting.reseller", + ), + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index a6e9643..a33b4d6 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -1,9 +1,22 @@ +from django.conf import settings from django.db import models from django.urls import reverse from .fields import DomainNameField +class Reseller(models.Model): + user = models.OneToOneField( + settings.AUTH_USER_MODEL, + on_delete=models.PROTECT, + related_name="hosting_reseller", + limit_choices_to={"is_superuser": False}, + ) + + def __str__(self): + return self.user.get_username() + + class HostingPlan(models.Model): name = models.CharField(max_length=120, unique=True) description = models.CharField(max_length=240, blank=True) @@ -21,6 +34,9 @@ class HostingAccount(models.Model): hosting_plan = models.ForeignKey( HostingPlan, on_delete=models.PROTECT, null=True, blank=True ) + reseller = models.ForeignKey( + Reseller, on_delete=models.PROTECT, null=True, blank=True + ) created_at = models.DateTimeField(auto_now_add=True) def get_absolute_url(self): diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 103b90e..248e494 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -7,22 +7,25 @@

{{ account.name }}

{{ account.primary_domain }}

- {% if perms.hosting.change_hostingaccount %} + {% if not is_reseller and perms.hosting.change_hostingaccount %} Edit account {% endif %}
Primary domain
{{ account.primary_domain }}
- {% if perms.hosting.view_hostingplan %} + {% if is_reseller or perms.hosting.view_hostingplan %}
Hosting plan
{% if account.hosting_plan %}{{ account.hosting_plan.name }}{% else %}Unassigned{% endif %}
{% endif %} + {% if not is_reseller and perms.hosting.view_reseller %} +
Reseller
{{ account.reseller|default_if_none:"Unassigned" }}
+ {% endif %}
Added
{{ account.created_at|date:"j M Y, H:i" }} UTC
- {% if perms.hosting.view_webprovisioningplan %} + {% if is_reseller or perms.hosting.view_webprovisioningplan %}

Recent Apache previews

- {% if perms.hosting.change_hostingaccount %} + {% if not is_reseller and perms.hosting.change_hostingaccount %}
{% csrf_token %} diff --git a/src/coulobre/hosting/templates/hosting/account_list.html b/src/coulobre/hosting/templates/hosting/account_list.html index 4601504..748ca07 100644 --- a/src/coulobre/hosting/templates/hosting/account_list.html +++ b/src/coulobre/hosting/templates/hosting/account_list.html @@ -6,7 +6,7 @@

Hosting accounts

{{ paginator.count }} account{{ paginator.count|pluralize }}{% if search %} matching your search{% endif %}

- {% if perms.hosting.add_hostingaccount %} + {% if not is_reseller and perms.hosting.add_hostingaccount %} Add hosting account {% endif %}
@@ -24,7 +24,7 @@ Account Primary domain - {% if perms.hosting.view_hostingplan %}Hosting plan{% endif %} + {% if is_reseller or perms.hosting.view_hostingplan %}Hosting plan{% endif %} Added @@ -33,7 +33,7 @@ {{ account.name }} {{ account.primary_domain }} - {% if perms.hosting.view_hostingplan %} + {% if is_reseller or perms.hosting.view_hostingplan %} {% if account.hosting_plan %}{{ account.hosting_plan.name }}{% else %}Unassigned{% endif %} {% endif %} {{ account.created_at|date:"j M Y" }} diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html index de25d10..4b8b73a 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -17,19 +17,19 @@