From 3928cc4ace8bf29e9f61446206eb0b410ae4571d Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Wed, 6 Sep 2017 15:12:15 +0200 Subject: [PATCH] Payment/Manual: Improve placeholders --- .../indico_payment_manual/placeholders.py | 69 ++++++++++++++++++- .../indico_payment_manual/plugin.py | 13 ++-- payment_manual/setup.py | 6 +- 3 files changed, 78 insertions(+), 10 deletions(-) diff --git a/payment_manual/indico_payment_manual/placeholders.py b/payment_manual/indico_payment_manual/placeholders.py index b31061c..50dea37 100644 --- a/payment_manual/indico_payment_manual/placeholders.py +++ b/payment_manual/indico_payment_manual/placeholders.py @@ -16,9 +16,44 @@ from __future__ import unicode_literals -from indico_payment_manual import _ -from indico.util.placeholders import Placeholder +from decimal import Decimal +from urllib import quote_plus + from indico.modules.events.registration.placeholders.registrations import IDPlaceholder +from indico.util.placeholders import ParametrizedPlaceholder, Placeholder + +from indico_payment_manual import _ + + +class EscapablePlaceholder(ParametrizedPlaceholder): + param_required = False + param_restricted = True + field = None + basic_description = None + + @classmethod + def render(cls, param, regform, registration): + rv = getattr(registration, cls.field) + if param == 'url': + rv = quote_plus(rv.encode('utf-8')).decode('utf-8') + return rv + + @classmethod + def iter_param_info(cls, regform, registration): + yield None, cls.basic_description + yield 'url', '{} ({})'.format(cls.basic_description, _('escaped for URLs')) + + +class FirstNamePlaceholder(EscapablePlaceholder): + name = 'first_name' + basic_description = _("First name of the registrant") + field = 'first_name' + + +class LastNamePlaceholder(EscapablePlaceholder): + name = 'last_name' + basic_description = _("Last name of the registrant") + field = 'last_name' class RegistrationIDPlaceholder(IDPlaceholder): @@ -32,3 +67,33 @@ class EventIDPlaceholder(Placeholder): @classmethod def render(cls, regform, registration): return registration.registration_form.event_new.id + + +class PricePlaceholder(ParametrizedPlaceholder): + param_required = False + param_restricted = True + name = 'price' + + @classmethod + def render(cls, param, regform, registration): + if param == 'int': + return unicode(int(registration.price * 100)) + elif param == 'short' and int(registration.price) == registration.price: + return unicode(int(registration.price)) + else: + return unicode(registration.price.quantize(Decimal('.01'))) + + @classmethod + def iter_param_info(cls, regform, registration): + yield None, _("The price the registrant needs to pay (e.g. 100.00 or 100.25)") + yield 'short', _("The price without cents if possible (e.g. 100 or 100.25)") + yield 'int', _("The price formatted as an integer (e.g. 10000 or 10025)") + + +class CurrencyPlaceholder(Placeholder): + name = 'currency' + description = _("The currency used in the registration") + + @classmethod + def render(cls, regform, registration): + return registration.currency diff --git a/payment_manual/indico_payment_manual/plugin.py b/payment_manual/indico_payment_manual/plugin.py index a09a6fa..1d96c1d 100644 --- a/payment_manual/indico_payment_manual/plugin.py +++ b/payment_manual/indico_payment_manual/plugin.py @@ -29,8 +29,9 @@ from indico.web.forms.validators import UsedIf from indico_payment_manual import _ -DETAILS_DESC = _('The details the user needs to make their payment. This usually includes the bank account details ' - 'the IBAN and payment reference.') +DETAILS_DESC = _('The details the user needs to make their payment. This usually includes the bank account details, ' + 'the IBAN and payment reference. You can also link to a custom payment site. In this case make sure ' + 'to use the URL-escaped versions of the placeholders where available.') class PluginSettingsForm(PaymentPluginSettingsFormBase): @@ -68,13 +69,15 @@ class ManualPaymentPlugin(PaymentPluginMixin, IndicoPlugin): self.connect(signals.get_placeholders, self._get_details_placeholders, sender='manual-payment-details') def _get_details_placeholders(self, sender, regform, registration, **kwargs): - from indico.modules.events.registration.placeholders.registrations import (FirstNamePlaceholder, - LastNamePlaceholder) - from indico_payment_manual.placeholders import RegistrationIDPlaceholder, EventIDPlaceholder + from indico_payment_manual.placeholders import (FirstNamePlaceholder, LastNamePlaceholder, + RegistrationIDPlaceholder, EventIDPlaceholder, PricePlaceholder, + CurrencyPlaceholder) yield FirstNamePlaceholder yield LastNamePlaceholder yield RegistrationIDPlaceholder yield EventIDPlaceholder + yield PricePlaceholder + yield CurrencyPlaceholder @property def logo_url(self): diff --git a/payment_manual/setup.py b/payment_manual/setup.py index 7f0784c..ff7cdb9 100644 --- a/payment_manual/setup.py +++ b/payment_manual/setup.py @@ -16,12 +16,12 @@ from __future__ import unicode_literals -from setuptools import setup, find_packages +from setuptools import find_packages, setup setup( name='indico_payment_manual', - version='0.2.0', + version='0.3.0', url='https://github.com/indico/indico-plugins', license='https://www.gnu.org/licenses/gpl-3.0.txt', author='Indico Team', @@ -30,7 +30,7 @@ setup( zip_safe=False, include_package_data=True, install_requires=[ - 'indico>=1.9.10' + 'indico>=1.9.11.dev17' ], classifiers=[ 'Environment :: Plugins',