Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: filter files by category #659

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion alexandria/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,21 @@ class FileFilterSet(FilterSet):
document_metainfo = JSONValueFilter(field_name="document__metainfo")
active_group = ActiveGroupFilter()
files = BaseCSVFilter(field_name="pk", lookup_expr="in")
category = CategoriesFilter(field_name="document__category")
categories = CategoriesFilter(field_name="document__category")
Comment on lines +171 to +172
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need both? They're functionally equal. I'd rather only have the plural if possible. (I notice the DocumentFilterSet also has both though, so maybe for consistency... 🤔 )

# exclude_children is applied in CategoriesFilter, this is needed for DjangoFilterBackend
exclude_children = BooleanFilter(field_name="title", method=lambda qs, __, ___: qs)

class Meta:
model = models.File
fields = ["original", "renderings", "variant", "metainfo", "files"]
fields = [
"original",
"renderings",
"variant",
"metainfo",
"files",
"categories",
]


class FileSearchFilterSet(FileFilterSet):
Expand Down
15 changes: 15 additions & 0 deletions alexandria/core/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,18 @@ def test_document_category_filters(
data = response.json()["data"]
assert len(data) == expected_count
assert sorted([doc["attributes"]["title"] for doc in data]) == snapshot


def test_file_category_filter(db, admin_client, file_factory):
f1 = file_factory(name="1.pdf")
file_factory(name="2.pdf")

response = admin_client.get(
reverse("file-list"),
{"filter[categories]": f1.document.category_id, "filter[variant]": "original"},
)
Comment on lines +394 to +397
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a test for category as well if we're adding it to the filterset


assert response.status_code == HTTP_200_OK
data = response.json()["data"]
assert len(data) == 1
assert sorted([doc["attributes"]["name"] for doc in data]) == ["1.pdf"]
Loading