Skip to content

Commit

Permalink
fix: date hierarchy is reset on filter (#873) (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelPancocha authored Nov 25, 2024
1 parent cd0dc18 commit b342733
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
3 changes: 0 additions & 3 deletions src/unfold/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.contrib.admin import StackedInline as BaseStackedInline
from django.contrib.admin import TabularInline as BaseTabularInline
from django.contrib.admin import display, helpers
from django.contrib.admin.options import IS_POPUP_VAR, TO_FIELD_VAR
from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
from django.db import models
from django.db.models import BLANK_CHOICE_DASH, Model
Expand Down Expand Up @@ -477,8 +476,6 @@ def changelist_view(
{
"actions_list": actions,
"actions_row": actions_row,
"to_field_var": TO_FIELD_VAR,
"is_popup_var": IS_POPUP_VAR,
}
)

Expand Down
19 changes: 2 additions & 17 deletions src/unfold/templates/unfold/change_list_filter.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load i18n admin_list %}
{% load i18n admin_list unfold %}

<div id="changelist-filter" class="backdrop-blur-sm bg-opacity-80 bg-gray-900 flex inset-0 z-50 fixed {% if not cl.model_admin.list_filter_sheet %}2xl:bg-transparent 2xl:relative 2xl:!block 2xl:z-10{% endif %}" x-show="filterOpen">
<label for="show-filters" id="changelist-filter-close" class="flex-grow {% if not cl.model_admin.list_filter_sheet %}2xl:hidden{% endif %}" x-on:click="filterOpen = false"></label>
Expand Down Expand Up @@ -38,22 +38,7 @@ <h3 class="border-b flex font-semibold mb-4 px-4 py-4 text-font-important-light
<div class="px-4{% if cl.model_admin.list_filter_submit %} {% endif %}">
{% if cl.model_admin.list_filter_submit %}
<form id="filter-form" method="get">
{% if request.GET.q %}
<input type="hidden" name="q" value="{{ request.GET.q }}">
{% endif %}

{% if request.GET.o %}
<input type="hidden" name="o" value="{{ request.GET.o }}">
{% endif %}

{% if is_popup %}
<input type="hidden" name="{{ is_popup_var }}" value="1">
{% endif %}

{% if to_field %}
<input type="hidden" name="{{ to_field_var }}" value="{{ to_field }}">
{% endif %}

{% preserve_filters %}
{% endif %}

{% for spec in cl.filter_specs %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for key, value in params.items %}
<input type="hidden" name="{{ key }}" value="{{ value }}">
{% endfor %}
31 changes: 29 additions & 2 deletions src/unfold/templatetags/unfold.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Any, Dict, List, Mapping, Optional, Union
from typing import Any, Dict, List, Mapping, Optional, Set, Union

from django import template
from django.contrib.admin.helpers import AdminForm, Fieldset
from django.contrib.admin.views.main import ChangeList
from django.forms import Field
from django.template import Library, Node, RequestContext, TemplateSyntaxError
from django.http import HttpRequest
from django.template import Context, Library, Node, RequestContext, TemplateSyntaxError
from django.template.base import NodeList, Parser, Token, token_kwargs
from django.template.loader import render_to_string
from django.utils.safestring import SafeText
Expand Down Expand Up @@ -224,3 +226,28 @@ def add_css_class(field: Field, classes: Union[list, tuple]) -> Field:
field.field.widget.attrs["class"] = classes

return field


@register.inclusion_tag(
"unfold/templatetags/preserve_changelist_filters.html",
takes_context=True,
name="preserve_filters",
)
def preserve_changelist_filters(context: Context) -> Dict[str, Dict[str, str]]:
"""
Generate hidden input fields to preserve filters for POST forms.
"""
request: Optional[HttpRequest] = context.get("request")
changelist: Optional[ChangeList] = context.get("cl")

if not request or not changelist:
return {"params": {}}

used_params: Set[str] = {
param for spec in changelist.filter_specs for param in spec.used_parameters
}
preserved_params: Dict[str, str] = {
param: value for param, value in request.GET.items() if param not in used_params
}

return {"params": preserved_params}

0 comments on commit b342733

Please sign in to comment.