Coverage for jutil/decorators.py: 0%
14 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-07 16:40 -0500
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-07 16:40 -0500
1from django.utils.formats import date_format
4def formatted_date(description=None, ordering=None, fmt: str = "SHORT_DATE_FORMAT"):
5 """
6 Decorator for formatting date/datetime field in Django admin.
7 :param description: Short description of the function (default is "short date").
8 :param ordering: Admin order field (optional).
9 :param fmt: Format to pass to date_format (default "SHORT_DATE_FORMAT")
11 Usage example in Django Admin:
13 @formatted_date(_("timestamp"), "timestamp")
14 def timestamp_short(obj):
15 return obj.timestamp
16 """
18 def wrap_func(func):
19 def fmt_func(*args, **kwargs):
20 val = func(*args, **kwargs)
21 if not val:
22 return ""
23 return date_format(val, fmt)
25 if description is not None:
26 fmt_func.short_description = description
27 if ordering is not None:
28 fmt_func.admin_order_field = ordering
29 return fmt_func
31 return wrap_func