From da63414a934e4839c5b7fdeaf5905f7985ccd23a Mon Sep 17 00:00:00 2001 From: Amin Vassefpoor Date: Fri, 14 Jun 2024 22:38:31 +0330 Subject: [PATCH] Resolve reoccurrence of issue #78 for admin list display converts (#81) --- jalali_date/admin.py | 3 ++- jalali_date/templatetags/jalali_tags.py | 11 +---------- jalali_date/utils.py | 8 ++++++++ 3 files changed, 11 insertions(+), 11 deletions(-) create mode 100644 jalali_date/utils.py diff --git a/jalali_date/admin.py b/jalali_date/admin.py index 16d13fb..d96c6b8 100644 --- a/jalali_date/admin.py +++ b/jalali_date/admin.py @@ -4,6 +4,7 @@ from jalali_date import date2jalali, datetime2jalali from jalali_date.fields import JalaliDateField, SplitJalaliDateTimeField +from jalali_date.utils import normalize_strftime from jalali_date.widgets import AdminJalaliDateWidget, AdminSplitJalaliDateTime overrides = { @@ -62,6 +63,7 @@ def func(obj): if not g_date: return '' else: + strftime = normalize_strftime(strftime) return convert_func(g_date).strftime(strftime) func.short_description = field.verbose_name @@ -89,4 +91,3 @@ def __init__(self, *args, **kwargs): formfield_overrides.update(self.formfield_overrides) self.formfield_overrides = formfield_overrides super(TabularInlineJalaliMixin, self).__init__(*args, **kwargs) - diff --git a/jalali_date/templatetags/jalali_tags.py b/jalali_date/templatetags/jalali_tags.py index 4cac2f1..d714f61 100644 --- a/jalali_date/templatetags/jalali_tags.py +++ b/jalali_date/templatetags/jalali_tags.py @@ -5,6 +5,7 @@ from django.conf import settings from jalali_date import date2jalali, datetime2jalali +from jalali_date.utils import normalize_strftime django_version = get_version() if StrictVersion(django_version) >= StrictVersion('1.9'): @@ -27,16 +28,6 @@ def contents(self): return self.value -def normalize_strftime(strftime): - """ - Normalize strftime values to make sure their usable for datetime libraries. - """ - if not isinstance(strftime, str): - # Convert non-str values to str to support stuff like "lazy_translations". - strftime = str(strftime) - return strftime - - @register.filter def to_jalali(g_date, strftime=None): if g_date is None: diff --git a/jalali_date/utils.py b/jalali_date/utils.py new file mode 100644 index 0000000..2c0d2e4 --- /dev/null +++ b/jalali_date/utils.py @@ -0,0 +1,8 @@ +def normalize_strftime(strftime): + """ + Normalize strftime values to make sure their usable for datetime libraries. + """ + if not isinstance(strftime, str): + # Convert non-str values to str to support stuff like "lazy_translations". + strftime = str(strftime) + return strftime