Skip to content

Commit

Permalink
fix(api.py): update filter logic to apply filters as OR operation
Browse files Browse the repository at this point in the history
Refactor filter application to allow combining multiple filters
using an OR operation. This change ensures that classes meeting
any of the filter criteria are included. Additionally, remove
duplicates and sort the resulting classes by start time and name
for consistent output.
  • Loading branch information
NodeJSmith committed Jan 3, 2025
1 parent bfb0913 commit f55889f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/otf_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ def get_classes(
exclude_cancelled (bool): Whether to exclude classes that were cancelled by the studio. Default is True.
exclude_unbookable (bool): Whether to exclude classes that are outside the scheduling window. Default is\
True.
filters (list[ClassFilter] | ClassFilter | None): A list of filters to apply to the classes. Filters are\
applied in the order they are provided. Default is None.
filters (list[ClassFilter] | ClassFilter | None): A list of filters to apply to the classes, or a single\
filter. Filters are applied as an OR operation. Default is None.
Returns:
OtfClassList: The classes for the user.
Expand Down Expand Up @@ -246,11 +246,17 @@ def get_classes(
classes_list.classes = classes_list.classes[:limit]

if filters:
filtered_classes: list[models.OtfClass] = []

if not isinstance(filters, list):
filters = [filters]

for f in filters:
classes_list.classes = f.filter_classes(classes_list.classes)
filtered_classes.extend(f.filter_classes(classes_list.classes))

# remove duplicates
classes_list.classes = list({c.class_uuid: c for c in filtered_classes}.values())
classes_list.classes = sorted(classes_list.classes, key=lambda x: (x.starts_at_local, x.name))

return classes_list

Expand Down

0 comments on commit f55889f

Please sign in to comment.