Skip to content

Commit

Permalink
IA-3433 Allow override of date_format for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
bramj committed Jan 7, 2025
1 parent f865156 commit 0e402bc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions iaso/api/payments/filters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ def filter_by_forms(request, queryset, key=None):


def filter_by_dates(request, queryset, start_date=None, end_date=None, key=None):
date_format = "%Y-%m-%d"

if start_date:
try:
filter_key = f"{key}__created_at__gte" if key else "created_at__gte"
filter_params = {filter_key: date_string_to_start_of_day(start_date)}
filter_params = {filter_key: date_string_to_start_of_day(start_date, date_format)}
queryset = queryset.filter(**filter_params)
except ValueError:
pass

if end_date:
try:
filter_key = f"{key}__created_at__lte" if key else "created_at__lte"
filter_params = {filter_key: date_string_to_end_of_day(end_date)}
filter_params = {filter_key: date_string_to_end_of_day(end_date, date_format)}
queryset = queryset.filter(**filter_params)
except ValueError:
pass
Expand Down
8 changes: 4 additions & 4 deletions iaso/utils/date_and_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ def timestamp_to_datetime(timestamp):
return date.strftime("%Y-%m-%d %H:%M:%S")


def date_string_to_start_of_day(date_str):
date_dt = datetime.strptime(date_str, DATE_FORMAT)
def date_string_to_start_of_day(date_str, date_format=DATE_FORMAT):
date_dt = datetime.strptime(date_str, date_format)
date_dt = timezone.make_aware(date_dt, timezone.get_default_timezone())

return date_dt.replace(hour=0, minute=0, second=0)


def date_string_to_end_of_day(date_str):
date_dt = datetime.strptime(date_str, DATE_FORMAT)
def date_string_to_end_of_day(date_str, date_format=DATE_FORMAT):
date_dt = datetime.strptime(date_str, date_format)
date_dt = timezone.make_aware(date_dt, timezone.get_default_timezone())

return date_dt.replace(hour=23, minute=59, second=59)

0 comments on commit 0e402bc

Please sign in to comment.