From 9b52bc4b924402ae98d1ab744861524e7105d879 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Tue, 5 Oct 2021 13:31:09 +0200 Subject: [PATCH] Show warning when extra fees may be applied on mailbox creation --- musician/models.py | 4 ++++ musician/templates/musician/mailbox_form.html | 9 +++++++++ musician/views.py | 13 ++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/musician/models.py b/musician/models.py index 3c86283..ffd594e 100644 --- a/musician/models.py +++ b/musician/models.py @@ -129,6 +129,10 @@ class UserAccount(OrchestraModel): return super().new_from_json(data=data, billing=billing, language=language, last_login=last_login) + def allowed_resources(self, resource): + allowed_by_type = musician_settings.ALLOWED_RESOURCES[self.type] + return allowed_by_type[resource] + class DatabaseUser(OrchestraModel): api_name = 'databaseusers' diff --git a/musician/templates/musician/mailbox_form.html b/musician/templates/musician/mailbox_form.html index bafff86..b7484bc 100644 --- a/musician/templates/musician/mailbox_form.html +++ b/musician/templates/musician/mailbox_form.html @@ -4,6 +4,15 @@ {% block content %}

{{ service.verbose_name }}

+{% if extra_mailbox %} + +{% endif %} +
{% csrf_token %} {% bootstrap_form form %} diff --git a/musician/views.py b/musician/views.py index 487434f..a6e2542 100644 --- a/musician/views.py +++ b/musician/views.py @@ -325,7 +325,18 @@ class MailboxCreateView(CustomContextMixin, UserTokenRequiredMixin, FormView): template_name = "musician/mailbox_form.html" form_class = MailboxCreateForm success_url = reverse_lazy("musician:mailbox-list") - extra_context = {'service': service_class} + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'extra_mailbox': self.is_extra_mailbox(context['profile']), + 'service': self.service_class, + }) + return context + + def is_extra_mailbox(self, profile): + number_of_mailboxes = len(self.orchestra.retrieve_mailbox_list()) + return number_of_mailboxes >= profile.allowed_resources('mailbox') def form_valid(self, form): serialized_data = form.serialize()