Skip to content

Remove collapsed state setting for filters #9

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

Merged
merged 1 commit into from
Oct 10, 2022
Merged
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ from django_admin_filters import MultiChoice
class MyChoicesFilter(MultiChoice):
FILTER_LABEL = "Select options"
BUTTON_LABEL = "Apply"
is_collapsed = False
```

- FILTER_LABEL: Filter title
- BUTTON_LABEL: Title for filter apply button
- is_collapsed: Filter state (collapsed/expanded) on first page load

## MultiChoice filter

Expand Down
2 changes: 0 additions & 2 deletions READMEru.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ from django_admin_filters import MultiChoice
class MyChoicesFilter(MultiChoice):
FILTER_LABEL = "Выберите опции"
BUTTON_LABEL = "Применить"
is_collapsed = False
```

- FILTER_LABEL: Заголовок фильтра
- BUTTON_LABEL: Заголовок кнопки применения фильтра
- is_collapsed: Состояние фильтра (свернутый/распахнутый) при первой загрузке страницы

## Фильтр MultiChoice

Expand Down
13 changes: 3 additions & 10 deletions django_admin_filters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
from django.contrib import admin


class Collapsed:
class Base:
"""Mixin class for filters with title, apply button and collapsed state."""

is_collapsed = False
parameter_name = 'filter'
title = None

Expand All @@ -18,16 +17,10 @@ def set_title(self):
'parameter_name': self.parameter_name,
'filter_name': self.FILTER_LABEL,
'button_label': self.BUTTON_LABEL,
'collapsed': self.collapsed_state,
}

@property
def collapsed_state(self):
"""Return string for CSS stype."""
return '' if self.is_collapsed else 'open'


class Filter(admin.FieldListFilter, Collapsed):
class Filter(admin.FieldListFilter, Base):
"""Base class for filters applied to field with title, apply button and collapsed state."""

parameter_name_mask = 'adminfilter_'
Expand All @@ -54,7 +47,7 @@ def choices(self, changelist):
raise NotImplementedError('Method choices')


class FilterSimple(admin.SimpleListFilter, Collapsed):
class FilterSimple(admin.SimpleListFilter, Base):
"""Base class for filters without field with title, apply button and collapsed state."""

parameter_name = 'adminfilter'
Expand Down
2 changes: 1 addition & 1 deletion django_admin_filters/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<details data-filter-title="{{ title.filter_name }}" {{ title.collapsed }}>
<details data-filter-title="{{ title.filter_name }}" open >
<summary>{{ title.filter_name }}</summary>
<ul>
{% block choices_list %}
Expand Down
5 changes: 0 additions & 5 deletions example/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@ class StatusFilter(MultiChoice):
"""Field status filter."""

FILTER_LABEL = "By status"
is_collapsed = True


class NumberFilter(MultiChoice):
"""Field number filter."""

FILTER_LABEL = "By number"
is_collapsed = True


class ColorFilter(MultiChoiceExt):
"""Property color filter."""

parameter_name = "color"
FILTER_LABEL = "By color"
is_collapsed = True

# https://docs.djangoproject.com/en/4.1/topics/db/queries/#complex-lookups-with-q-objects
options = [
Expand All @@ -38,14 +35,12 @@ class Timestamp1Filter(DateRange):
"""Field timestamp1 filter."""

FILTER_LABEL = "By timestamp1"
is_collapsed = True


class Timestamp2Filter(DateRangePicker):
"""Field timestamp2 filter."""

FILTER_LABEL = "By timestamp2"
is_collapsed = True


class Admin(admin.ModelAdmin):
Expand Down