Skip to content

Commit

Permalink
Improve code quality.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo committed Mar 7, 2024
1 parent b1bb5d0 commit ce37e11
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions admin_interface/templatetags/admin_interface_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,26 @@ def admin_interface_date_hierarchy_removal_link(changelist, date_field_name):
date_label = " ".join(date_labels).lower()

params = changelist.get_filters_params()
date_params = [p for p in params if p.startswith(date_field_name)]
date_params = [param for param in params if param.startswith(date_field_name)]

date_args = [datetime.datetime.now().year, 1, 1]
year = params.get(f"{date_field_name}__year")
if year:
date_args[0] = int(year[0]) if isinstance(year, list) else int(year)
date_format = "Y"

if f"{date_field_name}__month" in params:
month = params[f"{date_field_name}__month"]
date_args[1] = int(month[0]) if isinstance(month, list) else int(month)
date_format = "YEAR_MONTH_FORMAT"

if f"{date_field_name}__day" in params:
day = params[f"{date_field_name}__day"]
date_args[2] = int(day[0]) if isinstance(day, list) else int(day)
date_format = "DATE_FORMAT"

date_filters = (
("year", "Y"),
("month", "YEAR_MONTH_FORMAT"),
("day", "DATE_FORMAT"),
)
for index, date_filter in enumerate(date_filters):
date_filter_key, date_filter_format = date_filter
date_filter_param = f"{date_field_name}__{date_filter_key}"
if date_filter_param in params:
param = params[date_filter_param]
date_args[index] = (
int(param[0]) if isinstance(param, (list, tuple)) else int(param)
)
date_format = date_filter_format
continue
break
date_value = datetime.date(*date_args)

removal_link = changelist.get_query_string(remove=date_params)
Expand Down

0 comments on commit ce37e11

Please sign in to comment.