-
Notifications
You must be signed in to change notification settings - Fork 173
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
alonraiz
wants to merge
18
commits into
modlinltd:develop
Choose a base branch
from
alonraiz:just-filter-without-save
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
60edde3
filter without creating new object
alonraiz c4f539f
template string removed
alonraiz 31ab25e
Just filter without save (#2)
alonraiz 1541a35
Update CI (#1) (#3)
alonraiz 408cf88
FE bug fixes (#4)
alonraiz 1936671
select2 widgets not initializing (#6)
alonraiz 41d9633
save form data when filtering
alonraiz d4acf38
save form data when filtering
alonraiz 9309aa9
filter without creating new object
alonraiz 3a5feaa
template string removed
alonraiz e8a027f
save form data when filtering
alonraiz 8ba8315
Merge branch 'just-filter-without-save' of https://github.com/alonrai…
alonraiz eff7c15
Merge remote-tracking branch 'upstream/develop' into just-filter-with…
alonraiz b695456
template string removed
alonraiz fb38ffc
Modlinltd master (#8)
alonraiz ee868f5
Merge branch 'modlinltd:master' into master
alonraiz a337ca4
Merge branch 'master' into just-filter-without-save
alonraiz adb40aa
Update test_creation.py
alonraiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,9 @@ | |
from django.shortcuts import resolve_url | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from .forms import AdvancedFilterForm | ||
from .forms import AdvancedFilterForm, AdvancedFilterQueryForm | ||
from .models import AdvancedFilter | ||
from .q_serializer import QSerializer | ||
|
||
|
||
logger = logging.getLogger('advanced_filters.admin') | ||
|
@@ -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" | ||
|
@@ -56,15 +82,24 @@ 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) | ||
|
||
def save_advanced_filter(self, request, form): | ||
self.list_filter = (AdvancedListFilters, AdvancedQueryFilters,) + tuple(self.list_filter) | ||
|
||
def save_advanced_filter(self, request, form, save_instance): | ||
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) | ||
if form.is_valid(): | ||
afilter = form.save(commit=False) | ||
afilter.created_by = request.user | ||
afilter.query = form.generate_query() | ||
afilter.save() | ||
afilter.users.add(request.user) | ||
|
||
if save_instance: | ||
afilter.save() | ||
afilter.users.add(request.user) | ||
messages.add_message( | ||
request, messages.SUCCESS, | ||
_('Advanced filter added successfully.') | ||
|
@@ -77,9 +112,36 @@ def save_advanced_filter(self, request, form): | |
elif request.method == "POST": | ||
logger.info('Failed saving advanced filter, params: %s', form.data) | ||
|
||
def get_advanced_filter_data(self, request): | ||
if request.POST.get('action') == 'advanced_filters': | ||
return request.POST, True | ||
|
||
search_query = request.GET.get('_aquery') | ||
if search_query: | ||
query_serializer = QSerializer(base64=True) | ||
raw_query = query_serializer.loads(search_query, raw=True) | ||
query_list = query_serializer.get_field_values_list(raw_query) | ||
|
||
data = { | ||
'form-TOTAL_FORMS': 0, | ||
'form-INITIAL_FORMS': 0, | ||
'title': 'unsaved filter' | ||
} | ||
|
||
for idx, query in enumerate(query_list): | ||
query_dict = AdvancedFilterQueryForm._parse_query_dict(query, self.model) | ||
|
||
for key, value in query_dict.items(): | ||
data['form-{idx}-{key}'.format(idx=idx, key=key)] = value | ||
|
||
data['form-TOTAL_FORMS'] += 1 | ||
|
||
return data, False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above means to initialize the forms when querying without saving? If so, perhaps we can use |
||
|
||
return None, False | ||
|
||
def adv_filters_handle(self, request, extra_context={}): | ||
data = request.POST if request.POST.get( | ||
'action') == 'advanced_filters' else None | ||
data, save_instance = self.get_advanced_filter_data(request) | ||
adv_filters_form = self.advanced_filter_form( | ||
data=data, model_admin=self, extra_form=True) | ||
extra_context.update({ | ||
|
@@ -88,7 +150,7 @@ def adv_filters_handle(self, request, extra_context={}): | |
'current_afilter': request.GET.get('_afilter'), | ||
'app_label': self.opts.app_label, | ||
}) | ||
return self.save_advanced_filter(request, adv_filters_form) | ||
return self.save_advanced_filter(request, adv_filters_form, save_instance) | ||
|
||
def changelist_view(self, request, extra_context=None): | ||
"""Add advanced_filters form to changelist context""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{# for now - make filter hidden in sidebar #} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
orfilter_ad_hoc
or some other name, and allow it to be called fromadv_filters_handle
if not saving.