Skip to content

Commit

Permalink
Merge pull request #437 from Domejko/invoice-filter-update
Browse files Browse the repository at this point in the history
Filtering logic moved to a separate function
  • Loading branch information
TreyWW authored Jul 9, 2024
2 parents 5f3807d + 439d1b5 commit f116000
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions backend/service/invoices/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ def should_add_condition(was_previous_selection, has_just_been_selected):
return (was_previous_selection and not has_just_been_selected) or (not was_previous_selection and has_just_been_selected)


def filter_conditions(or_conditions, previous_filters, action_filter_by, action_filter_type, context):
for filter_type, filter_by_list in previous_filters.items():
or_conditions_filter = Q() # Initialize OR conditions for each filter type
for filter_by, status in filter_by_list.items():
# Determine if the filter was selected in the previous request
was_previous_selection = bool(status)
# Determine if the filter is selected in the current request
has_just_been_selected = action_filter_by == filter_by and action_filter_type == filter_type

# Check if the filter status has changed
if should_add_condition(was_previous_selection, has_just_been_selected):
# Construct filter condition dynamically based on filter_type
filter_condition = build_filter_condition(filter_type, filter_by)
or_conditions_filter |= Q(**filter_condition)
context["selected_filters"].append(filter_by)

# Combine OR conditions for each filter type with AND
or_conditions &= or_conditions_filter

return or_conditions, context


def get_context(
invoices: QuerySet,
sort_by: str | None,
Expand Down Expand Up @@ -59,24 +81,8 @@ def get_context(
# Initialize OR conditions for filters using Q objects
or_conditions = Q()

# Iterate through previous filters to build OR conditions
for filter_type, filter_by_list in previous_filters.items():
or_conditions_filter = Q() # Initialize OR conditions for each filter type
for filter_by, status in filter_by_list.items():
# Determine if the filter was selected in the previous request
was_previous_selection = bool(status)
# Determine if the filter is selected in the current request
has_just_been_selected = action_filter_by == filter_by and action_filter_type == filter_type

# Check if the filter status has changed
if should_add_condition(was_previous_selection, has_just_been_selected):
# Construct filter condition dynamically based on filter_type
filter_condition = build_filter_condition(filter_type, filter_by)
or_conditions_filter |= Q(**filter_condition)
context["selected_filters"].append(filter_by)

# Combine OR conditions for each filter type with AND
or_conditions &= or_conditions_filter
if action_filter_by or action_filter_type:
or_conditions, context = filter_conditions(or_conditions, previous_filters, action_filter_by, action_filter_type, context)

# check/update payment status to make sure it is correct before invoices are filtered and displayed
invoices.update(
Expand All @@ -96,7 +102,6 @@ def get_context(
)
)

# Apply OR conditions to the invoices queryset
invoices = invoices.filter(or_conditions)

# Validate and sanitize the sort_by parameter
Expand All @@ -119,7 +124,6 @@ def get_context(
context["sort_direction"] = True
invoices = invoices.order_by(sort_by)

# Add invoices to the context
context["invoices"] = invoices

return context, invoices

0 comments on commit f116000

Please sign in to comment.