Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a button in admin settings to enable/disable validation of organizer billing #504

Open
wants to merge 9 commits into
base: development
Choose a base branch
from
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 @@ -1196,26 +1197,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 @@ -21,6 +21,7 @@
from pretix.control.permissions import (
AdministratorPermissionRequiredMixin, StaffMemberRequiredMixin,
)
from pretix.common.enums import ValidStates

logger = logging.getLogger(__name__)

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')
Loading