From 22d2782941b214970d1f4226fc2a18597268b859 Mon Sep 17 00:00:00 2001 From: Nicolas Joyard <joyard.nicolas@gmail.com> Date: Fri, 17 Jun 2016 23:23:53 +0200 Subject: [PATCH] Group autocomplete: abide by active_only session parameter --- core/views.py | 5 ++++- .../GroupListTest.test_autocomplete.content | 2 +- .../GroupListTest.test_autocomplete_query.content | 2 +- memopol/tests/test_group_list.py | 4 ++-- memopol/views/group_ac.py | 15 +++++++++++++-- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/core/views.py b/core/views.py index 5a0a8605..051c31f4 100644 --- a/core/views.py +++ b/core/views.py @@ -36,7 +36,10 @@ class ActiveLegislatureMixin(object): def get_active_only(self): overriden = self.override_active_only() if overriden is None: - return self.request.session['active_only'] + if 'active_only' in self.request.session: + return self.request.session['active_only'] + else: + return self.default_active_only else: return overriden diff --git a/memopol/tests/response_fixtures/GroupListTest.test_autocomplete.content b/memopol/tests/response_fixtures/GroupListTest.test_autocomplete.content index f9d90437..84f648bd 100644 --- a/memopol/tests/response_fixtures/GroupListTest.test_autocomplete.content +++ b/memopol/tests/response_fixtures/GroupListTest.test_autocomplete.content @@ -1 +1 @@ -{"pagination": {"more": true}, "results": [{"text": "Committee on Agriculture and Rural Development", "id": 47}, {"text": "Committee on Budgetary Control", "id": 89}, {"text": "Committee on Budgets", "id": 34}, {"text": "Committee on Civil Liberties, Justice and Home Affairs", "id": 7}, {"text": "Committee on Constitutional Affairs", "id": 37}, {"text": "Committee on Culture and Education", "id": 20}, {"text": "Committee on Development", "id": 8}, {"text": "Committee on Economic and Monetary Affairs", "id": 30}, {"text": "Committee on Employment and Social Affairs", "id": 3}, {"text": "Committee on Fisheries", "id": 55}]} \ No newline at end of file +{"pagination": {"more": true}, "results": [{"text": "Committee on Agriculture and Rural Development", "id": 47}, {"text": "Committee on Budgetary Control", "id": 89}, {"text": "Committee on Budgets", "id": 34}, {"text": "Committee on Civil Liberties, Justice and Home Affairs", "id": 7}, {"text": "Committee on Constitutional Affairs", "id": 37}, {"text": "Committee on Culture and Education", "id": 20}, {"text": "Committee on Economic and Monetary Affairs", "id": 30}, {"text": "Committee on Employment and Social Affairs", "id": 3}, {"text": "Committee on Fisheries", "id": 55}, {"text": "Committee on Foreign Affairs", "id": 5}]} \ No newline at end of file diff --git a/memopol/tests/response_fixtures/GroupListTest.test_autocomplete_query.content b/memopol/tests/response_fixtures/GroupListTest.test_autocomplete_query.content index 60cbd8c4..ecdf253b 100644 --- a/memopol/tests/response_fixtures/GroupListTest.test_autocomplete_query.content +++ b/memopol/tests/response_fixtures/GroupListTest.test_autocomplete_query.content @@ -1 +1 @@ -{"pagination": {"more": false}, "results": [{"text": "Committee on Agriculture and Rural Development", "id": 47}, {"text": "Committee on Development", "id": 8}, {"text": "Committee on Regional Development", "id": 24}]} \ No newline at end of file +{"pagination": {"more": false}, "results": [{"text": "Committee on Agriculture and Rural Development", "id": 47}, {"text": "Committee on Regional Development", "id": 24}]} \ No newline at end of file diff --git a/memopol/tests/test_group_list.py b/memopol/tests/test_group_list.py index 4372cb70..cb1d686b 100644 --- a/memopol/tests/test_group_list.py +++ b/memopol/tests/test_group_list.py @@ -60,10 +60,10 @@ class GroupListTest(ResponseDiffMixin, TestCase): if q: url = '/legislature/autocomplete/group/?q=%s' % q else: - url = '/legislature/autocomplete/group/' + url = '/legislature/autocomplete/group/?' # setup session variables - self.client.get(url) + self.client.get('%s&active_only=0' % url) self.responsediff_test(url, numQueries) diff --git a/memopol/views/group_ac.py b/memopol/views/group_ac.py index 9e231b21..9d7abc58 100644 --- a/memopol/views/group_ac.py +++ b/memopol/views/group_ac.py @@ -1,15 +1,26 @@ # coding: utf-8 +from core.views import ActiveLegislatureMixin + from dal.autocomplete import Select2QuerySetView +import datetime + from django.db.models import Q from representatives.models import Group -class GroupAutocomplete(Select2QuerySetView): +class GroupAutocomplete(Select2QuerySetView, ActiveLegislatureMixin): + def get_queryset(self): - qs = Group.objects.exclude(kind__in=['chamber', 'country']) + qs = Group.objects.distinct().exclude(kind__in=['chamber', 'country']) + + if self.get_active_only(): + qs = qs.filter( + Q(mandates__end_date__gte=datetime.date.today()) | + Q(mandates__end_date__isnull=True) + ) if self.q: qs = qs.filter( -- GitLab