From 684b8e4102612055e7ef11211bd235fc5f3efbad Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Mon, 21 Mar 2022 12:36:40 +0100 Subject: [PATCH] Accept arabic numbers from date picker (#2547) --- src/pretix/base/forms/widgets.py | 30 +++++++++++++++++++ src/pretix/helpers/formats/ar/__init__.py | 21 ++++++++++++++ src/pretix/helpers/formats/ar/formats.py | 35 +++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/pretix/helpers/formats/ar/__init__.py create mode 100644 src/pretix/helpers/formats/ar/formats.py diff --git a/src/pretix/base/forms/widgets.py b/src/pretix/base/forms/widgets.py index 7b570eeb19..3903b6e954 100644 --- a/src/pretix/base/forms/widgets.py +++ b/src/pretix/base/forms/widgets.py @@ -42,6 +42,24 @@ from django.utils.timezone import get_current_timezone, now from django.utils.translation import gettext_lazy as _ +def replace_arabic_numbers(inp): + if not isinstance(inp, str): + return inp + table = { + 1632: 48, # 0 + 1633: 49, # 1 + 1634: 50, # 2 + 1635: 51, # 3 + 1636: 52, # 4 + 1637: 53, # 5 + 1638: 54, # 6 + 1639: 55, # 7 + 1640: 56, # 8 + 1641: 57, # 9 + } + return inp.translate(table) + + class DatePickerWidget(forms.DateInput): def __init__(self, attrs=None, date_format=None): attrs = attrs or {} @@ -62,6 +80,10 @@ class DatePickerWidget(forms.DateInput): forms.DateInput.__init__(self, date_attrs, date_format) + def value_from_datadict(self, data, files, name): + v = super().value_from_datadict(data, files, name) + return replace_arabic_numbers(v) + class TimePickerWidget(forms.TimeInput): def __init__(self, attrs=None, time_format=None): @@ -83,6 +105,10 @@ class TimePickerWidget(forms.TimeInput): forms.TimeInput.__init__(self, time_attrs, time_format) + def value_from_datadict(self, data, files, name): + v = super().value_from_datadict(data, files, name) + return replace_arabic_numbers(v) + class UploadedFileWidget(forms.ClearableFileInput): def __init__(self, *args, **kwargs): @@ -179,6 +205,10 @@ class SplitDateTimePickerWidget(forms.SplitDateTimeWidget): # Skip one hierarchy level forms.MultiWidget.__init__(self, widgets, attrs) + def value_from_datadict(self, data, files, name): + v = super().value_from_datadict(data, files, name) + return [replace_arabic_numbers(i) for i in v] + class BusinessBooleanRadio(forms.RadioSelect): def __init__(self, require_business=False, attrs=None): diff --git a/src/pretix/helpers/formats/ar/__init__.py b/src/pretix/helpers/formats/ar/__init__.py new file mode 100644 index 0000000000..9fd5bdc500 --- /dev/null +++ b/src/pretix/helpers/formats/ar/__init__.py @@ -0,0 +1,21 @@ +# +# This file is part of pretix (Community Edition). +# +# Copyright (C) 2014-2020 Raphael Michel and contributors +# Copyright (C) 2020-2021 rami.io GmbH and contributors +# +# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General +# Public License as published by the Free Software Foundation in version 3 of the License. +# +# ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are +# applicable granting you additional permissions and placing additional restrictions on your usage of this software. +# Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive +# this file, see . +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. +# +# You should have received a copy of the GNU Affero General Public License along with this program. If not, see +# . +# diff --git a/src/pretix/helpers/formats/ar/formats.py b/src/pretix/helpers/formats/ar/formats.py new file mode 100644 index 0000000000..01b2c05afd --- /dev/null +++ b/src/pretix/helpers/formats/ar/formats.py @@ -0,0 +1,35 @@ +# +# This file is part of pretix (Community Edition). +# +# Copyright (C) 2014-2020 Raphael Michel and contributors +# Copyright (C) 2020-2021 rami.io GmbH and contributors +# +# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General +# Public License as published by the Free Software Foundation in version 3 of the License. +# +# ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are +# applicable granting you additional permissions and placing additional restrictions on your usage of this software. +# Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive +# this file, see . +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +# details. +# +# You should have received a copy of the GNU Affero General Public License along with this program. If not, see +# . +# +DATE_INPUT_FORMATS = [ + "%d-%m-%Y", + "%Y-%m-%d", + "%m/%d/%Y", + "%m/%d/%y", + "%b %d %Y", + "%b %d, %Y", + "%d %b %Y", + "%d %b, %Y", + "%B %d %Y", + "%B %d, %Y", + "%d %B %Y", + "%d %B, %Y" +]