Skip to content

Commit

Permalink
EREGCSC-2549 Enable populated filter on admin panel (#1194)
Browse files Browse the repository at this point in the history
* Remove filter by subpart, title etc

Add filter by index populated

Move filters to common directory "common/filters.py"

* Add tests
  • Loading branch information
cgodwin1 authored Mar 5, 2024
1 parent be86943 commit c377252
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ def queryset(self, request, queryset):
return queryset.filter(**filter).distinct()


class TitleFilter(ParameterFilter):
parameter_name = "locations__title"
title = "Title"


class PartFilter(ParameterFilter):
parameter_name = "locations__part"
title = "Part"


class SectionFilter(ParameterFilter):
parameter_name = "locations__section__section_id"
title = "Section"
class IndexPopulatedFilter(SimpleListFilter):
title = "index populated"
parameter_name = "populated"

def lookups(self, request, model_admin):
return [
("yes", "Yes"),
("no", "No"),
]

class SubpartFilter(ParameterFilter):
parameter_name = "locations__subpart__subpart_id"
title = "Subpart"
def queryset(self, request, queryset):
if self.value() == "yes":
return queryset.filter(contentindex__content__isnull=False)
if self.value() == "no":
return queryset.filter(contentindex__content__isnull=True)
return queryset
28 changes: 28 additions & 0 deletions solution/backend/common/tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest

from common.filters import IndexPopulatedFilter
from content_search.models import ContentIndex
from file_manager.models import UploadedFile


@pytest.mark.django_db
def test_index_populated_filter():
a = UploadedFile.objects.create(document_name="a")
b = UploadedFile.objects.create(document_name="b")
ContentIndex.objects.create(file=a, content="Hello world")
ContentIndex.objects.create(file=b)

# Test when populated filter is not set
results = IndexPopulatedFilter(None, {}, None, None).queryset(None, UploadedFile.objects.all())
assert a in results
assert b in results

# Test when populated filter is set to "yes"
results = IndexPopulatedFilter(None, {"populated": "yes"}, None, None).queryset(None, UploadedFile.objects.all())
assert a in results
assert b not in results

# Test when populated filter is set to "no"
results = IndexPopulatedFilter(None, {"populated": "no"}, None, None).queryset(None, UploadedFile.objects.all())
assert a not in results
assert b in results
2 changes: 2 additions & 0 deletions solution/backend/file_manager/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.urls import reverse
from django.utils.html import format_html

from common.filters import IndexPopulatedFilter
from common.functions import establish_client
from content_search.functions import add_to_index
from content_search.models import ContentIndex
Expand Down Expand Up @@ -69,6 +70,7 @@ class UploadedFileAdmin(BaseAdmin):
fields = ("file_name", "file_path", "document_name", "date", "summary",
"updated_at", "document_type", "subjects", "locations", "internal_notes",
"index_populated", "get_content", "download_file", "category",)
list_filter = [IndexPopulatedFilter]

manytomany_lookups = {
"locations": lambda: AbstractLocation.objects.all().select_subclasses(),
Expand Down
12 changes: 2 additions & 10 deletions solution/backend/resources/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,11 @@
from django.utils.safestring import mark_safe
from solo.admin import SingletonModelAdmin

from common.filters import IndexPopulatedFilter
from content_search.functions import add_to_index
from content_search.models import ContentIndex

from . import actions
from .filters import (
PartFilter,
SectionFilter,
SubpartFilter,
TitleFilter,
)
from .models import (
AbstractCategory,
AbstractLocation,
Expand Down Expand Up @@ -147,10 +142,7 @@ class AbstractResourceAdmin(BaseAdmin):

list_filter = [
"approved",
TitleFilter,
PartFilter,
SectionFilter,
SubpartFilter,
IndexPopulatedFilter,
]

foreignkey_lookups = {
Expand Down

0 comments on commit c377252

Please sign in to comment.