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

Filter without saving #108

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
35 changes: 34 additions & 1 deletion advanced_filters/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .forms import AdvancedFilterForm
from .models import AdvancedFilter
from .q_serializer import QSerializer


logger = logging.getLogger('advanced_filters.admin')
Expand Down Expand Up @@ -43,6 +44,31 @@ def queryset(self, request, queryset):
return queryset


class AdvancedQueryFilters(admin.SimpleListFilter):
"""Allow filtering by advanced filters query"""
title = ' '
template = 'admin/query_filter.html'

parameter_name = '_aquery'

def lookups(self, request, model_admin):
return None

def queryset(self, request, queryset):
if self.value():
query_serializer = QSerializer(base64=True)
query = query_serializer.loads(self.value())
return queryset.filter(query).distinct()

return queryset

def choices(self, changelist):
return []

def has_output(self):
return True


class AdminAdvancedFiltersMixin(object):
""" Generic AdvancedFilters mixin """
advanced_change_list_template = "admin/advanced_filters.html"
Expand All @@ -56,9 +82,16 @@ def __init__(self, *args, **kwargs):
self.original_change_list_template = "admin/change_list.html"
self.change_list_template = self.advanced_change_list_template
# add list filters to filters
self.list_filter = (AdvancedListFilters,) + tuple(self.list_filter)
self.list_filter = (AdvancedListFilters, AdvancedQueryFilters,) + tuple(self.list_filter)

def save_advanced_filter(self, request, form):
if '_just_filter' in request.POST:
search_query = form.generate_query()
query_serializer = QSerializer(base64=True)
b64_query = query_serializer.dumps(search_query)
url = "{path}?_aquery={query}"\
.format(path=request.path, query=b64_query)
return HttpResponseRedirect(url)
Copy link
Member

Choose a reason for hiding this comment

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

How about we add this into it's own method filter_without_saving or filter_ad_hoc or some other name, and allow it to be called from adv_filters_handle if not saving.

if form.is_valid():
afilter = form.save(commit=False)
afilter.created_by = request.user
Expand Down
1 change: 1 addition & 0 deletions advanced_filters/templates/admin/advanced_filters.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ <h1>{% trans "Create advanced filter" %}:</h1>
<br />
<input method="POST" type="submit" value="{% trans "Save" %}">
<input method="POST" name="_save_goto" type="submit" value="{% trans "Save & Filter Now!" %}">
<input method="POST" name="_just_filter" type="submit" value="{% trans "Filter" %}">
<a href="#" class="grp-button" style="margin:auto" onclick="$.magnificPopup.close();">{% trans "Cancel" %}</a>
</form>

Expand Down
1 change: 1 addition & 0 deletions advanced_filters/templates/admin/query_filter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{# for now - make filter hidden in sidebar #}
11 changes: 11 additions & 0 deletions advanced_filters/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ def test_create_form_valid(self):

assert list(created_filter.query.children[0]) == self.query

# just filter
Copy link
Member

Choose a reason for hiding this comment

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

This should go in it's own test, e.g test_create_form_valid_just_filter and I would rename test_create_form_valid_save_goto

form_data.pop('_save_goto')
form_data['_just_filter'] = 1
url = reverse('admin:customers_client_changelist')
res = self.client.post(url, data=form_data)

assert res.status_code == 302
assert AdvancedFilter.objects.count() == 2
url = res['location']
assert '?_aquery=' in url


class AdvancedFilterUsageTest(TestCase):
""" Test filter visibility and actual filtering of a changelist """
Expand Down