diff --git a/src/coulobre/hosting/migrations/0035_reseller_audit_event.py b/src/coulobre/hosting/migrations/0035_reseller_audit_event.py new file mode 100644 index 0000000..b88f3f4 --- /dev/null +++ b/src/coulobre/hosting/migrations/0035_reseller_audit_event.py @@ -0,0 +1,64 @@ +# Generated by Django 5.2.19 on 2026-11-21 18:05 + +import django.db.models.deletion +from django.db import migrations, models + +import coulobre.hosting.fields + + +class Migration(migrations.Migration): + dependencies = [ + ("hosting", "0034_account_service_changes"), + ] + + operations = [ + migrations.CreateModel( + name="ResellerAuditEvent", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("reseller_username", models.CharField(max_length=150)), + ("actor_username", models.CharField(max_length=150)), + ( + "action", + models.CharField( + choices=[ + ("create", "Created reseller"), + ("update", "Updated reseller"), + ("assign", "Assigned hosting account"), + ("unassign", "Unassigned hosting account"), + ("plan", "Changed hosting account plan"), + ], + max_length=8, + ), + ), + ( + "account_domain", + coulobre.hosting.fields.DomainNameField( + blank=True, max_length=253 + ), + ), + ("changes", models.JSONField()), + ("created_at", models.DateTimeField(auto_now_add=True)), + ( + "reseller", + models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="audit_events", + to="hosting.reseller", + ), + ), + ], + options={ + "ordering": ("-pk",), + }, + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 9083c31..dd36efc 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -57,10 +57,38 @@ class Reseller(models.Model): def __str__(self): return self.user.get_username() + def get_absolute_url(self): + return reverse("panel:reseller", kwargs={"pk": self.pk}) + def may_create_accounts(self): return self.account_limit > 0 and self.hosting_plans.exists() +class ResellerAuditEvent(models.Model): + class Action(models.TextChoices): + CREATE = "create", "Created reseller" + UPDATE = "update", "Updated reseller" + ASSIGN = "assign", "Assigned hosting account" + UNASSIGN = "unassign", "Unassigned hosting account" + PLAN = "plan", "Changed hosting account plan" + + reseller = models.ForeignKey( + Reseller, + on_delete=models.SET_NULL, + null=True, + related_name="audit_events", + ) + reseller_username = models.CharField(max_length=150) + actor_username = models.CharField(max_length=150) + action = models.CharField(max_length=8, choices=Action) + account_domain = DomainNameField(max_length=253, blank=True) + changes = models.JSONField() + created_at = models.DateTimeField(auto_now_add=True) + + class Meta: + ordering = ("-pk",) + + class Customer(models.Model): user = models.OneToOneField( settings.AUTH_USER_MODEL, diff --git a/src/coulobre/hosting/reseller_accounts.py b/src/coulobre/hosting/reseller_accounts.py index 9669012..ec28f2c 100644 --- a/src/coulobre/hosting/reseller_accounts.py +++ b/src/coulobre/hosting/reseller_accounts.py @@ -1,18 +1,115 @@ +from django.contrib.auth import get_user_model from django.core.exceptions import NON_FIELD_ERRORS, ValidationError from django.db import connection, transaction from django.db.models import F -from .models import Reseller +from .models import Customer, Reseller, ResellerAuditEvent +from .resource_requests import lock_hosting_account, require_active_services + +Action = ResellerAuditEvent.Action + + +def lock_resellers(candidates): + if connection.vendor == "sqlite": + # Acquire the write lock before reading the reseller's policy. + candidates.update(account_limit=F("account_limit")) + return candidates.order_by("pk").select_for_update() + + +def plan_reference(plan): + if plan is None: + return None + return {"id": plan.pk, "name": plan.name} + + +def policy_snapshot(reseller): + return { + "account_limit": reseller.account_limit, + "hosting_plans": [ + plan_reference(plan) + for plan in reseller.hosting_plans.order_by("pk") + ], + "is_active": reseller.user.is_active, + } + + +def record_event(reseller, actor, action, changes, *, account_domain=""): + ResellerAuditEvent.objects.create( + reseller=reseller, + reseller_username=reseller.user.get_username(), + actor_username=actor, + action=action, + account_domain=account_domain, + changes=changes, + ) + + +def policy_changes(before, after): + return { + key: {"before": before.get(key), "after": value} + for key, value in after.items() + if before.get(key) != value + } + + +def may_manage_sign_in(user): + return ( + not user.is_staff + and not user.is_superuser + and not Customer.objects.filter(user=user).exists() + ) + + +@transaction.atomic +def create_reseller(login_form, policy, actor): + user = login_form.save(commit=False) + user.is_active = policy["is_active"] + user.save() + reseller = Reseller.objects.create( + user=user, account_limit=policy["account_limit"] + ) + if "hosting_plans" in policy: + reseller.hosting_plans.set(policy["hosting_plans"]) + record_event( + reseller, + actor, + Action.CREATE, + policy_changes({}, policy_snapshot(reseller)), + ) + return reseller + + +@transaction.atomic +def update_reseller(reseller_id, policy, actor): + reseller = lock_resellers(Reseller.objects.filter(pk=reseller_id)).get() + user = ( + get_user_model().objects.select_for_update().get(pk=reseller.user_id) + ) + reseller.user = user + before = policy_snapshot(reseller) + + if "is_active" in policy and user.is_active != policy["is_active"]: + if not may_manage_sign_in(user): + raise ValidationError( + "This login can no longer be managed as a reseller." + ) + user.is_active = policy["is_active"] + user.save(update_fields=["is_active"]) + if reseller.account_limit != policy["account_limit"]: + reseller.account_limit = policy["account_limit"] + reseller.save(update_fields=["account_limit"]) + if "hosting_plans" in policy: + reseller.hosting_plans.set(policy["hosting_plans"]) + changes = policy_changes(before, policy_snapshot(reseller)) + if changes: + record_event(reseller, actor, Action.UPDATE, changes) @transaction.atomic def create_reseller_account(access, account): candidates = Reseller.objects.filter(pk=access.pk, user_id=access.user_id) - if connection.vendor == "sqlite": - # Write first so SQLite locks writers before the quota check. - candidates.update(account_limit=F("account_limit")) - reseller = candidates.select_for_update().first() - if reseller is None: + reseller = lock_resellers(candidates).first() + if reseller is None or not reseller.user.is_active: raise ValidationError( {NON_FIELD_ERRORS: "Your reseller access has changed."} ) @@ -31,3 +128,79 @@ def create_reseller_account(access, account): account.reseller = reseller account.save(force_insert=True) + record_account_event( + reseller, reseller.user.get_username(), Action.ASSIGN, account + ) + + +def record_account_event(reseller, actor, action, account): + plan = plan_reference(account.hosting_plan) + record_event( + reseller, + actor, + action, + {"plan": plan}, + account_domain=account.primary_domain, + ) + + +@transaction.atomic +def save_operator_account(account, actor, *, changed_fields=None): + if changed_fields is None: + previous = None + previous_reseller_id = None + reseller_id = account.reseller_id + else: + previous = lock_hosting_account(account.pk, customer_id=None) + if "primary_domain" in changed_fields: + require_active_services(previous) + previous_reseller_id = previous.reseller_id + reseller_id = ( + account.reseller_id + if "reseller" in changed_fields + else previous_reseller_id + ) + + locked = { + reseller.pk: reseller + for reseller in lock_resellers( + Reseller.objects.filter(pk__in=[previous_reseller_id, reseller_id]) + ) + } + if reseller_id is not None and reseller_id not in locked: + raise ValidationError( + {"reseller": "This reseller is no longer available."} + ) + account.save(update_fields=changed_fields) + if previous_reseller_id != reseller_id: + if previous_reseller_id is not None: + record_account_event( + locked[previous_reseller_id], actor, Action.UNASSIGN, previous + ) + if reseller_id is not None: + account.refresh_from_db(fields=["primary_domain", "hosting_plan"]) + record_account_event( + locked[reseller_id], actor, Action.ASSIGN, account + ) + elif ( + reseller_id is not None + and changed_fields is not None + and "hosting_plan" in changed_fields + and previous.hosting_plan_id != account.hosting_plan_id + ): + record_event( + locked[reseller_id], + actor, + Action.PLAN, + { + "account_plan": { + "before": plan_reference(previous.hosting_plan), + "after": plan_reference(account.hosting_plan), + } + }, + account_domain=( + account.primary_domain + if "primary_domain" in changed_fields + else previous.primary_domain + ), + ) diff --git a/src/coulobre/hosting/reseller_views.py b/src/coulobre/hosting/reseller_views.py new file mode 100644 index 0000000..826a10b --- /dev/null +++ b/src/coulobre/hosting/reseller_views.py @@ -0,0 +1,192 @@ +from django import forms +from django.contrib.auth import get_user_model +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.mixins import PermissionRequiredMixin +from django.core.exceptions import ValidationError +from django.core.paginator import Paginator +from django.db import IntegrityError +from django.db.models import Count +from django.http import Http404 +from django.shortcuts import get_object_or_404, redirect +from django.utils.decorators import method_decorator +from django.utils.functional import cached_property +from django.views.decorators.debug import sensitive_post_parameters +from django.views.generic import DetailView, FormView, ListView + +from .access import OperatorOnlyMixin +from .models import Reseller +from .reseller_accounts import ( + create_reseller, + may_manage_sign_in, + update_reseller, +) + +VIEW_RESELLERS = "hosting.view_reseller" +VIEW_PLANS = "hosting.view_hostingplan" +VIEW_ACCOUNTS = "hosting.view_hostingaccount" + + +class ResellerPolicyForm(forms.ModelForm): + is_active = forms.BooleanField( + label="Allow panel sign-in", required=False, initial=True + ) + + class Meta: + model = Reseller + fields = ("is_active", "account_limit", "hosting_plans") + labels = {"hosting_plans": "Allowed hosting plans"} + help_texts = { + "account_limit": "Enter 0 to stop self-service account creation.", + "hosting_plans": ( + "Plans available when a reseller creates an account." + ), + } + + def __init__(self, *args, show_plans, **kwargs): + super().__init__(*args, **kwargs) + if not show_plans: + self.fields.pop("hosting_plans") + if self.instance.pk: + user = self.instance.user + if may_manage_sign_in(user): + self.initial["is_active"] = user.is_active + else: + self.fields.pop("is_active") + + +class ResellerList(OperatorOnlyMixin, PermissionRequiredMixin, ListView): + permission_required = VIEW_RESELLERS + template_name = "hosting/reseller_list.html" + context_object_name = "resellers" + paginate_by = 20 + + def get_queryset(self): + self.search = self.request.GET.get("q", "").strip()[:150] + resellers = ( + Reseller.objects.select_related("user") + .annotate(account_count=Count("hostingaccount")) + .order_by("user__username", "pk") + ) + if self.search: + resellers = resellers.filter(user__username__icontains=self.search) + return resellers + + def get_context_data(self, **kwargs): + return super().get_context_data(search=self.search, **kwargs) + + +class ResellerDetail(OperatorOnlyMixin, PermissionRequiredMixin, DetailView): + permission_required = VIEW_RESELLERS + template_name = "hosting/reseller_detail.html" + queryset = Reseller.objects.select_related("user") + context_object_name = "reseller" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + user = self.request.user + accounts = self.object.hostingaccount_set.select_related( + "hosting_plan" + ).order_by("primary_domain", "pk") + events = self.object.audit_events.all() + context["account_count"] = accounts.count() + if user.has_perm(VIEW_ACCOUNTS): + context["account_page"] = Paginator(accounts, 20).get_page( + self.request.GET.get("account_page") + ) + else: + events = events.filter(account_domain="") + context["history_page"] = Paginator(events, 20).get_page( + self.request.GET.get("history_page") + ) + if user.has_perm(VIEW_PLANS): + context["allowed_plans"] = list(self.object.hosting_plans.all()) + return context + + +@method_decorator( + sensitive_post_parameters("password1", "password2"), name="dispatch" +) +class ResellerCreate(OperatorOnlyMixin, PermissionRequiredMixin, FormView): + permission_required = (VIEW_RESELLERS, "hosting.add_reseller") + form_class = UserCreationForm + template_name = "hosting/reseller_form.html" + extra_context = { + "page_title": "Add reseller", + "submit_label": "Add reseller", + } + + @cached_property + def policy_form(self): + return ResellerPolicyForm( + self.request.POST if self.request.method == "POST" else None, + prefix="policy", + show_plans=self.request.user.has_perm(VIEW_PLANS), + ) + + def get_context_data(self, **kwargs): + return super().get_context_data( + policy_form=self.policy_form, login_form=True, **kwargs + ) + + def form_valid(self, form): + if not self.policy_form.is_valid(): + return self.form_invalid(form) + try: + reseller = create_reseller( + form, + self.policy_form.cleaned_data, + self.request.user.get_username(), + ) + except IntegrityError: + user = form.instance + if ( + not get_user_model() + .objects.filter(username__iexact=user.username) + .exists() + ): + raise + form.add_error( + "username", user.unique_error_message(type(user), ["username"]) + ) + return self.form_invalid(form) + return redirect(reseller) + + +class ResellerEdit(OperatorOnlyMixin, PermissionRequiredMixin, FormView): + permission_required = (VIEW_RESELLERS, "hosting.change_reseller") + form_class = ResellerPolicyForm + template_name = "hosting/reseller_form.html" + extra_context = { + "page_title": "Edit reseller", + "submit_label": "Save changes", + } + + @cached_property + def reseller(self): + return get_object_or_404( + Reseller.objects.select_related("user"), pk=self.kwargs["pk"] + ) + + def get_form_kwargs(self): + return { + **super().get_form_kwargs(), + "instance": self.reseller, + "show_plans": self.request.user.has_perm(VIEW_PLANS), + } + + def get_context_data(self, **kwargs): + return super().get_context_data(reseller=self.reseller, **kwargs) + + def form_valid(self, form): + try: + update_reseller( + self.reseller.pk, + form.cleaned_data, + self.request.user.get_username(), + ) + except Reseller.DoesNotExist as error: + raise Http404 from error + except ValidationError as error: + form.add_error(None, error) + return self.form_invalid(form) + return redirect(self.reseller) diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 39981e9..9e29f9c 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -159,6 +159,12 @@ button, input, select, textarea { background: linear-gradient(currentColor, currentColor) center 35% / 40% 1px no-repeat; } +.nav-icon-resellers { + background: + linear-gradient(currentColor, currentColor) center 35% / 55% 1px no-repeat, + linear-gradient(currentColor, currentColor) center 65% / 75% 1px no-repeat; +} + .nav-icon-health { background: linear-gradient(currentColor, currentColor) 25% bottom / 1px 40% no-repeat, @@ -527,6 +533,22 @@ dd { width: 100%; } +.reseller-editor fieldset { + min-width: 0; + padding: 0; + border: 0; + margin: 0; +} + +.reseller-editor legend { + margin-bottom: 0.65rem; + font-weight: 680; +} + +.reseller-editor input[type="checkbox"] { + width: auto; +} + .editor .errorlist { margin: 0; padding: 0 0 0 1.1rem; diff --git a/src/coulobre/hosting/templates/hosting/account_list.html b/src/coulobre/hosting/templates/hosting/account_list.html index 669bcc0..99a634c 100644 --- a/src/coulobre/hosting/templates/hosting/account_list.html +++ b/src/coulobre/hosting/templates/hosting/account_list.html @@ -7,7 +7,7 @@

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

{% if account_allowance %}

{{ account_allowance.used }} of {{ account_allowance.limit }} accounts allocated

{% endif %} - {% if account_allowance and account_allowance.used < account_allowance.limit or is_operator and perms.hosting.add_hostingaccount %} + {% if account_allowance.may_create and account_allowance.used < account_allowance.limit or is_operator and perms.hosting.add_hostingaccount %} Add hosting account {% endif %} diff --git a/src/coulobre/hosting/templates/hosting/base.html b/src/coulobre/hosting/templates/hosting/base.html index cd111dc..e030a8d 100644 --- a/src/coulobre/hosting/templates/hosting/base.html +++ b/src/coulobre/hosting/templates/hosting/base.html @@ -29,6 +29,12 @@ Customers {% endif %} + {% if is_operator and perms.hosting.view_reseller %} + + + Resellers + + {% endif %} {% if is_operator and perms.hosting.view_hostingplan %} diff --git a/src/coulobre/hosting/templates/hosting/reseller_detail.html b/src/coulobre/hosting/templates/hosting/reseller_detail.html new file mode 100644 index 0000000..e906357 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/reseller_detail.html @@ -0,0 +1,101 @@ +{% extends "hosting/base.html" %} +{% block title %}{{ reseller }} · Coulobre Panel{% endblock %} +{% block content %} + ← Resellers +
+

{{ reseller }}

+ {% if perms.hosting.change_reseller %} + Edit reseller + {% endif %} +
+
+
Panel sign-in
{{ reseller.user.is_active|yesno:"Active,Suspended" }}
+
Accounts allocated
{{ account_count }} of {{ reseller.account_limit }}
+ {% if perms.hosting.view_hostingplan %} +
+
Allowed hosting plans
+
{% for plan in allowed_plans %}{{ plan.name }}{% if not forloop.last %}, {% endif %}{% empty %}None{% endfor %}
+
+ {% endif %} +
+ {% if perms.hosting.view_hostingaccount %} +
+

Assigned hosting accounts

+ {% if account_page.object_list %} +
+ + + {% if perms.hosting.view_hostingplan %}{% endif %} + + + {% for account in account_page %} + + + + {% if perms.hosting.view_hostingplan %}{% endif %} + + {% endfor %} + +
AccountPrimary domainHosting plan
{{ account.name }}{{ account.primary_domain }}{{ account.hosting_plan.name|default:"Unassigned" }}
+
+ {% if account_page.has_other_pages %} + + {% endif %} + {% else %} +

No hosting accounts are assigned.

+ {% endif %} +
+ {% endif %} +
+

Audit history

+ {% if history_page.object_list %} +
+ + + + + + {% for event in history_page %} + + + + + + {% endfor %} + +
ChangeChanged byTime (UTC)
+ {{ event.get_action_display }} + {% if event.account_domain %}
{{ event.account_domain }}
{% endif %} + {% if event.changes.account_limit %} +
Account limit {{ event.changes.account_limit.before|default_if_none:"—" }} → {{ event.changes.account_limit.after }}
+ {% endif %} + {% if event.changes.is_active %} +
Panel sign-in {{ event.changes.is_active.before|yesno:"active,suspended,—" }} → {{ event.changes.is_active.after|yesno:"active,suspended" }}
+ {% endif %} + {% if perms.hosting.view_hostingplan %} + {% if event.changes.hosting_plans %} +
Allowed plans {% for plan in event.changes.hosting_plans.before %}{{ plan.name }}{% if not forloop.last %}, {% endif %}{% empty %}None{% endfor %} → {% for plan in event.changes.hosting_plans.after %}{{ plan.name }}{% if not forloop.last %}, {% endif %}{% empty %}None{% endfor %}
+ {% endif %} + {% if event.changes.plan %}
Hosting plan {{ event.changes.plan.name }}
{% endif %} + {% if event.changes.account_plan %} +
Hosting plan {{ event.changes.account_plan.before.name|default:"Unassigned" }} → {{ event.changes.account_plan.after.name|default:"Unassigned" }}
+ {% endif %} + {% endif %} +
{{ event.actor_username }}{{ event.created_at|date:"j M Y, H:i" }}
+
+ {% if history_page.has_other_pages %} + + {% endif %} + {% else %} +

No changes recorded by the panel.

+ {% endif %} +
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/reseller_form.html b/src/coulobre/hosting/templates/hosting/reseller_form.html new file mode 100644 index 0000000..4cf977e --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/reseller_form.html @@ -0,0 +1,27 @@ +{% extends "hosting/base.html" %} +{% block title %}{{ page_title }} · Coulobre Panel{% endblock %} +{% block content %} + ← {% if reseller %}{{ reseller }}{% else %}Resellers{% endif %} +

{{ page_title }}

+
+
+ {% csrf_token %} + {% if login_form %} +
+ Login + {{ form.as_div }} +
+
+ Access and plans + {{ policy_form.as_div }} +
+ {% else %} + {{ form.as_div }} + {% endif %} +
+ + Cancel +
+
+
+{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/reseller_list.html b/src/coulobre/hosting/templates/hosting/reseller_list.html new file mode 100644 index 0000000..a892ec7 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/reseller_list.html @@ -0,0 +1,41 @@ +{% extends "hosting/base.html" %} +{% block title %}Resellers · Coulobre Panel{% endblock %} +{% block content %} +
+

Resellers

+ {% if perms.hosting.add_reseller %} + Add reseller + {% endif %} +
+
+ + {% if resellers %} +
+ + + + + + {% for reseller in resellers %} + + + + + + {% endfor %} + +
UsernamePanel sign-inAccounts allocated
{{ reseller }}{{ reseller.user.is_active|yesno:"Active,Suspended" }}{{ reseller.account_count }} of {{ reseller.account_limit }}
+
+ {% elif search %} +

No resellers match this search.

+ {% else %} +

No resellers yet.

+ {% endif %} + {% include "hosting/pagination.html" %} +
+{% endblock %} diff --git a/src/coulobre/hosting/urls.py b/src/coulobre/hosting/urls.py index eb8c064..563dcc3 100644 --- a/src/coulobre/hosting/urls.py +++ b/src/coulobre/hosting/urls.py @@ -6,6 +6,12 @@ from .backup_restores import RestoreKind from .credential_views import DatabaseCredentialView, MailboxCredentialView from .health_views import ServiceHealthView from .phpmyadmin import PhpMyAdminLaunch, redeem_phpmyadmin +from .reseller_views import ( + ResellerCreate, + ResellerDetail, + ResellerEdit, + ResellerList, +) app_name = "panel" @@ -112,6 +118,14 @@ urlpatterns = [ views.AccountEdit.as_view(), name="account_edit", ), + path("resellers/", ResellerList.as_view(), name="resellers"), + path("resellers/new/", ResellerCreate.as_view(), name="reseller_create"), + path("resellers//", ResellerDetail.as_view(), name="reseller"), + path( + "resellers//edit/", + ResellerEdit.as_view(), + name="reseller_edit", + ), path("customers/", views.CustomerList.as_view(), name="customers"), path( "customers/new/", diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index 7446166..8745830 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -96,18 +96,16 @@ from .php_settings import ( settings_from_form, ) from .phpmyadmin import phpmyadmin_available -from .reseller_accounts import create_reseller_account +from .reseller_accounts import create_reseller_account, save_operator_account from .resource_requests import ( change_mailbox_quota, create_database_request, create_mailbox_request, - lock_hosting_account, queue_database_provisioning, queue_mailbox_provisioning, queue_site_storage_limit, queue_site_storage_measurement, queue_web_https, - require_active_services, ) from .service_changes import request_service_change from .service_credentials import ( @@ -212,10 +210,11 @@ class AccountList(TenantScopeMixin, PermissionRequiredMixin, ListView): def get_context_data(self, **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(): + if reseller is not None: context["account_allowance"] = { "used": reseller.hostingaccount_set.count(), "limit": reseller.account_limit, + "may_create": reseller.may_create_accounts(), } return context @@ -278,7 +277,9 @@ class AccountCreate(PermissionRequiredMixin, AccountEditor, CreateView): self.object = form.save(commit=False) try: if reseller is None: - self.object.save() + save_operator_account( + self.object, self.request.user.get_username() + ) else: create_reseller_account(reseller, self.object) except ValidationError as error: @@ -307,13 +308,11 @@ class AccountEdit( def form_valid(self, form): self.object = form.save(commit=False) try: - with transaction.atomic(): - if "primary_domain" in form.changed_data: - current = lock_hosting_account( - self.object.pk, customer_id=None - ) - require_active_services(current) - self.object.save(update_fields=form.changed_data) + save_operator_account( + self.object, + self.request.user.get_username(), + changed_fields=form.changed_data, + ) except ValidationError as error: form.add_error(None, error) return self.form_invalid(form)