Skip to content

Commit

Permalink
Add a button in admin settings to enable/disable validation of organi…
Browse files Browse the repository at this point in the history
…zer billing (#504)

* Add button to toggle billing validation for organizers

* Edit active url

* Fix isort flake8 warning

* Change url display

* Resolve conversations

* Move enums to module pretix/common

* Fix isort
  • Loading branch information
HungNgien authored Jan 20, 2025
1 parent c361ed5 commit accad6e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 20 deletions.
33 changes: 14 additions & 19 deletions src/pretix/base/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.utils.crypto import get_random_string
from django.utils.formats import date_format
from django.utils.functional import cached_property
from django.utils.html import format_html
from django.utils.timezone import make_aware, now
from django.utils.translation import gettext, gettext_lazy as _
from django_scopes import ScopedManager, scopes_disabled
Expand Down Expand Up @@ -1205,26 +1206,20 @@ def live_issues(self):
)
)

billing_obj = OrganizerBillingModel.objects.filter(organizer=self.organizer).first()

if not billing_obj or not billing_obj.stripe_payment_method_id:
issues.append(
(
"<a {a_attr}>"
+ gettext('You need to fill the billing information.')
+ "</a>"
).format(
a_attr='href="%s#tab-0-1-open"'
% (
reverse(
"control:organizer.settings.billing",
kwargs={
"organizer": self.organizer.slug,
},
),
),
gs = GlobalSettingsObject()
if gs.settings.get("billing_validation", True) is True:
billing_obj = OrganizerBillingModel.objects.filter(organizer=self.organizer).first()
if not billing_obj or not billing_obj.stripe_payment_method_id:
url = reverse(
"control:organizer.settings.billing",
kwargs={"organizer": self.organizer.slug}
)
)
issue = format_html(
'<a href="{}#tab-0-1-open">{}</a>',
url,
gettext("You need to fill the billing information.")
)
issues.append(issue)

responses = event_live_issues.send(self)
for receiver, response in sorted(responses, key=lambda r: str(r[0])):
Expand Down
6 changes: 6 additions & 0 deletions src/pretix/common/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import StrEnum


class ValidStates(StrEnum):
DISABLED = 'disabled'
ENABLED = 'enabled'
7 changes: 6 additions & 1 deletion src/pretix/control/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,12 @@ def get_admin_navigation(request):
'label': _('Social login settings'),
'url': reverse('plugins:socialauth:admin.global.social.auth.settings'),
'active': (url.url_name == 'admin.global.social.auth.settings')
}
},
{
'label': _('Billing Validation'),
'url': reverse('control:admin.toggle.billing.validation'),
'active': (url.url_name == 'admin.toggle.billing.validation'),
},
]
},
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{% extends "pretixcontrol/admin/base.html" %}
{% load i18n %}
{% load bootstrap3 %}
{% load rich_text %}

{% block title %}{% trans "Enable or disable billing validation" %}{% endblock %}
{% block content %}
<nav id="event-nav" class="header-nav">
<div class="navigation">
<div class="navigation-title">
<h1>{% trans "Enable or disable billing validation" %}</h1>
</div>
{% include "pretixcontrol/event/component_link.html" %}
</div>
</nav>
<form action="" method="post" class="form-horizontal form-plugins">
{% csrf_token %}
{% if "success" in request.GET %}
<div class="alert alert-success">
{% trans "Your changes have been saved." %}
</div>
{% endif %}
<div class="tabbed-form">
<fieldset>
<legend>{% trans "Enable or disable billing validation" %}</legend>
<div class="table-responsive">
<table class="table">
<tr class="{% if billing_validation %}success{% else %}default{% endif %}">
<td>
<strong>{% trans "Validate billing information" %}</strong>
</td>
<td class="text-right flip" width="20%">
{% if billing_validation %}
<button class="btn btn-default btn-block" name="billing_validation" value="disabled">
{% trans "Disabled" %}
</button>
{% else %}
<button class="btn btn-default btn-block" name="billing_validation" value="enabled">
{% trans "Enabled" %}
</button>
{% endif %}
</td>
</tr>
</table>
</div>
</fieldset>
</div>
</form>
{% endblock %}
1 change: 1 addition & 0 deletions src/pretix/control/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
url(r'^global/settings/$', global_settings.GlobalSettingsView.as_view(), name='admin.global.settings'),
url(r'^global/update/$', global_settings.UpdateCheckView.as_view(), name='admin.global.update'),
url(r'^global/message/$', global_settings.MessageView.as_view(), name='admin.global.message'),
url(r'^global/billing_validation/$', global_settings.ToggleBillingValidationView.as_view(), name="admin.toggle.billing.validation"),
url(r'^vouchers/$', admin.VoucherList.as_view(), name='admin.vouchers'),
url(r'^vouchers/add$', admin.VoucherCreate.as_view(), name='admin.vouchers.add'),
url(r'^vouchers/(?P<voucher>\d+)/$', admin.VoucherUpdate.as_view(), name='admin.voucher'),
Expand Down
34 changes: 34 additions & 0 deletions src/pretix/control/views/global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pretix.base.models import LogEntry, OrderPayment, OrderRefund
from pretix.base.services.update_check import check_result_table, update_check
from pretix.base.settings import GlobalSettingsObject
from pretix.common.enums import ValidStates
from pretix.control.forms.global_settings import (
GlobalSettingsForm, SSOConfigForm, UpdateSettingsForm,
)
Expand Down Expand Up @@ -158,3 +159,36 @@ class RefundDetailView(AdministratorPermissionRequiredMixin, View):
def get(self, request, *args, **kwargs):
p = get_object_or_404(OrderRefund, pk=request.GET.get('pk'))
return JsonResponse({'data': p.info_data})


class ToggleBillingValidationView(AdministratorPermissionRequiredMixin, TemplateView):
template_name = 'pretixcontrol/toggle_billing_validation.html'

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.gs = GlobalSettingsObject()

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.gs.settings.get('billing_validation') is None:
self.gs.settings.set('billing_validation', True)
context['billing_validation'] = self.gs.settings.get('billing_validation')
return context

def post(self, request, *args, **kwargs):
value = request.POST.get('billing_validation', '').lower()

if value == ValidStates.DISABLED:
billing_validation = False
elif value == ValidStates.ENABLED:
billing_validation = True
else:
logger.error('Invalid value for billing validation: %s', value)
messages.error(request, _('Invalid value for billing validation!'))
return redirect(self.get_success_url())

self.gs.settings.set('billing_validation', billing_validation)
return redirect(self.get_success_url())

def get_success_url(self) -> str:
return reverse('control:admin.toggle.billing.validation')

0 comments on commit accad6e

Please sign in to comment.