Fix broken pagination by remembering last search

This commit is contained in:
Antoine Bartuccio 2017-07-23 13:24:39 +02:00
parent 9ff8f02a45
commit 39616874a8
3 changed files with 25 additions and 11 deletions

View File

@ -20,7 +20,7 @@
border-color: black; border-color: black;
} }
</style> </style>
{% if object_list.exists() %} {% if result_exists %}
<h2>{% trans %}User found{% endtrans %}</h2> <h2>{% trans %}User found{% endtrans %}</h2>
{% for user in object_list %} {% for user in object_list %}

View File

@ -27,7 +27,7 @@ from django.conf.urls import url
from matmat.views import * from matmat.views import *
urlpatterns = [ urlpatterns = [
url(r'^$', SearchFormView.as_view(), name="search"), url(r'^search$', SearchFormView.as_view(), name="search"),
url(r'^reverse$', SearchReverseFormView.as_view(), name="search_reverse"), url(r'^search/reverse$', SearchReverseFormView.as_view(), name="search_reverse"),
url(r'^clear$', SearchClearFormView.as_view(), name="search_clear"), url(r'^clear$', SearchClearFormView.as_view(), name="search_clear"),
] ]

View File

@ -21,6 +21,7 @@
# Place - Suite 330, Boston, MA 02111-1307, USA. # Place - Suite 330, Boston, MA 02111-1307, USA.
# #
# #
from ast import literal_eval
from django.views.generic import ListView, View from django.views.generic import ListView, View
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
@ -87,6 +88,10 @@ class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.form_class = kwargs['form'] self.form_class = kwargs['form']
self.reverse = kwargs['reverse'] self.reverse = kwargs['reverse']
self.session = request.session
self.last_search = self.session.get('matmat_search_result', str([]))
self.last_search = literal_eval(self.last_search)
print(self.last_search)
if 'valid_form' in kwargs.keys(): if 'valid_form' in kwargs.keys():
self.valid_form = kwargs['valid_form'] self.valid_form = kwargs['valid_form']
else: else:
@ -105,18 +110,24 @@ class SearchFormListView(WasSuscribed, SingleObjectMixin, ListView):
self.object = None self.object = None
kwargs = super(SearchFormListView, self).get_context_data(**kwargs) kwargs = super(SearchFormListView, self).get_context_data(**kwargs)
kwargs['form'] = self.form_class kwargs['form'] = self.form_class
kwargs['result_exists'] = self.result_exists
return kwargs return kwargs
def get_queryset(self): def get_queryset(self):
q = self.init_query
if self.valid_form is not None: if self.valid_form is not None:
if self.reverse: if self.reverse:
return self.init_query.filter(phone=self.valid_form['phone']).all() q = q.filter(phone=self.valid_form['phone']).all()
else: else:
q = self.init_query q = q.all()
# f = self.valid_form
return q.all()
else: else:
return self.model.objects.none() q = q.filter(pk__in=self.last_search).all()
self.result_exists = q.exists()
self.last_search = []
for user in q:
self.last_search.append(user.id)
self.session['matmat_search_result'] = str(self.last_search)
return q
class SearchFormView(WasSuscribed, FormView): class SearchFormView(WasSuscribed, FormView):
@ -142,11 +153,11 @@ class SearchFormView(WasSuscribed, FormView):
view = SearchFormListView.as_view() view = SearchFormListView.as_view()
if form.is_valid(): if form.is_valid():
kwargs['valid_form'] = form.clean() kwargs['valid_form'] = form.clean()
request.session['matmat_search'] = form.cleaned_data_json request.session['matmat_search_form'] = form.cleaned_data_json
return view(request, *args, **kwargs) return view(request, *args, **kwargs)
def get_initial(self): def get_initial(self):
return self.session.get('matmat_search', {}) return self.session.get('matmat_search_form', {})
class SearchReverseFormView(SearchFormView): class SearchReverseFormView(SearchFormView):
@ -160,5 +171,8 @@ class SearchClearFormView(WasSuscribed, View):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
super(SearchClearFormView, self).dispatch(request, *args, **kwargs) super(SearchClearFormView, self).dispatch(request, *args, **kwargs)
request.session.pop('matmat_search') if 'matmat_search_form' in request.session.keys():
request.session.pop('matmat_search_form')
if 'matmat_search_result' in request.session.keys():
request.session.pop('matmat_search_result')
return HttpResponseRedirect(reverse('matmat:search')) return HttpResponseRedirect(reverse('matmat:search'))