Prepare to block billing entity editing

ref #198
This commit is contained in:
Tobias Kunze 2025-10-07 16:39:04 +02:00
parent 6443582c0e
commit 842b66a84d
4 changed files with 30 additions and 3 deletions

View file

@ -66,8 +66,22 @@ class OrganizationAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
readonly_fields = list(super().get_readonly_fields(request, obj) or [])
readonly_fields.append("namespace") # Always read-only
if obj and obj.has_inherited_billing_entity:
readonly_fields.append("billing_entity")
return readonly_fields
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
if obj and obj.has_inherited_billing_entity:
form.base_fields["billing_entity"].help_text = _(
"This billing entity is inherited from the organization's origin and cannot be modified."
)
return form
@admin.register(BillingEntity)
class BillingEntityAdmin(admin.ModelAdmin):
@ -77,8 +91,9 @@ class BillingEntityAdmin(admin.ModelAdmin):
@admin.register(OrganizationOrigin)
class OrganizationOriginAdmin(admin.ModelAdmin):
list_display = ("name",)
list_display = ("name", "billing_entity")
search_fields = ("name",)
autocomplete_fields = ("billing_entity",)
@admin.register(OrganizationMembership)

View file

@ -89,6 +89,10 @@ class Organization(ServalaModelMixin, models.Model):
def get_absolute_url(self):
return self.urls.base
@property
def has_inherited_billing_entity(self):
return self.origin and self.billing_entity == self.origin.billing_entity
def set_owner(self, user):
with scopes_disabled():
OrganizationMembership.objects.filter(user=user, organization=self).delete()

View file

@ -8,6 +8,10 @@ from servala.frontend.forms.mixins import HtmxMixin
class OrganizationForm(HtmxMixin, ModelForm):
# def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
# if self.instance and self.instance.has_inherited_billing_entity:
# TODO disable billing entity editing
class Meta:
model = Organization
fields = ("name",)
@ -46,7 +50,7 @@ class OrganizationCreateForm(OrganizationForm):
def __init__(self, *args, user=None, **kwargs):
super().__init__(*args, **kwargs)
self.user = user
if not self.initial.get("invoice_country"):
default_country_name = "Switzerland"
country_choices = self.fields["invoice_country"].choices
@ -55,7 +59,6 @@ class OrganizationCreateForm(OrganizationForm):
self.initial["invoice_country"] = country_id
break
self.user = user
self.odoo_addresses = get_invoice_addresses(self.user)
if self.odoo_addresses:

View file

@ -69,6 +69,11 @@
<div class="card">
<div class="card-header">
<h4 class="card-title">{% translate "Billing Address" %}</h4>
{% if form.instance.has_inherited_billing_entity %}
<p class="text-muted">
<small>{% translate "This billing address cannot be modified." %}</small>
</p>
{% endif %}
</div>
<div class="card-content">
<div class="card-body">