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

Category functions with fix #313

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions fala/apps/adviser/templatetags/adviser_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import urlparse, parse_qs, quote
from django_jinja import library
from jinja2 import pass_context
from laalaa.api import PROVIDER_CATEGORIES


@library.filter
Expand Down Expand Up @@ -53,3 +54,27 @@ def google_map_params(item):
return {"api": 1, "query": address}
else:
return {"api": 1, "query": postcode}


@library.filter
def category_tuples(form):
# create list of tuples which can be passed to urlencode for pagination links
return [("categories", c) for c in form.cleaned_data["categories"]]


@library.filter
def category_selection(form):
if "categories" in form.cleaned_data:
categories = [PROVIDER_CATEGORIES[cat] for cat in form.cleaned_data["categories"]]
formatted_categories = ", ".join(map(str, categories))

return formatted_categories
return None


@library.filter
def category_selection_list(form):
if "categories" in form.cleaned_data:
categories = [PROVIDER_CATEGORIES[cat] for cat in form.cleaned_data["categories"]]
return map(str, categories)
return []
27 changes: 27 additions & 0 deletions fala/apps/adviser/tests/test_search_view_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ def test_category_search_has_no_previous_button(self):
response = self.client.get(self.url, self.data)
self.assertNotContains(response, '<div class="govuk-pagination__previous">')

@override_settings(FEATURE_FLAG_NO_MAP=True)
class PaginationTest(SimpleTestCase):
client = Client()
url = reverse("search")

data = {"postcode": "PE30", "categories": "deb", "page" : "2"}

def test_prev_pagination_link_contains_category(self):
response = self.client.get(self.url, self.data)
soup = bs4.BeautifulSoup(response.content, "html.parser")
next = soup.find("div", class_="govuk-pagination__prev")
link = next.find("a").get('href')
self.assertIn("deb", link)

def test_next_pagination_link_contains_category(self):
response = self.client.get(self.url, self.data)
soup = bs4.BeautifulSoup(response.content, "html.parser")
next = soup.find("div", class_="govuk-pagination__next")
link = next.find("a").get('href')
self.assertIn("deb", link)

def test_2nd_pagination_link_contains_category(self):
response = self.client.get(self.url, self.data)
soup = bs4.BeautifulSoup(response.content, "html.parser")
next = soup.find("li", class_="govuk-pagination__item")
link = next.find("a").get('href')
self.assertIn("deb", link)

@override_settings(FEATURE_FLAG_NO_MAP=True)
class ResultsPageWithJustPostcodeTest(SimpleTestCase):
Expand Down
13 changes: 0 additions & 13 deletions fala/apps/adviser/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from .forms import AdviserSearchForm
from .laa_laa_paginator import LaaLaaPaginator
from laalaa.api import PROVIDER_CATEGORIES
from .regions import Region


Expand Down Expand Up @@ -83,26 +82,14 @@ def get_context_data(self):
"postcode": self._form.cleaned_data["postcode"],
"name": self._form.cleaned_data["name"],
}
# create list of tuples which can be passed to urlencode for pagination links
categories = [("categories", c) for c in self._form.cleaned_data["categories"]]
return {
"form": self._form,
"data": self._data,
"pages": pages,
"params": params,
"FEATURE_FLAG_SURVEY_MONKEY": settings.FEATURE_FLAG_SURVEY_MONKEY,
"categories": categories,
"category_selection": self._display_category(),
}

def _display_category(self):
if "categories" in self._form.cleaned_data:
categories = [PROVIDER_CATEGORIES[cat] for cat in self._form.cleaned_data["categories"]]
formatted_categories = ", ".join(map(str, categories))

return formatted_categories
return []

class OldMapState(object):
def __init__(self, form, current_url):
self.current_url = current_url
Expand Down
18 changes: 9 additions & 9 deletions fala/templates/adviser/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ <h1 class="govuk-heading-xl">Search results</h1>
{% if form.name.value() %}
<li class="govuk-body notranslate" role="listitem" translate="no">Organisation: {{ form.name.value() }}</li>
{% endif %}
{% if category_selection %}
<li class="govuk-body notranslate" role="listitem" translate="no">Categories: {{ category_selection }} </li>
{% if form|category_selection %}
<li class="govuk-body notranslate" role="listitem" translate="no">Categories: {{ form|category_selection }} </li>
{% endif %}
</ul>

<form action="/" method="GET" novalidate>
<input type="hidden" name="postcode" value="{{ form.postcode.value() }}">
<input type="hidden" name="name" value="{{ form.name.value() }}">
{% for value, label_text in form.categories.field.choices %}
{% if label_text in category_selection.split(", ") %}
{% if label_text in form|category_selection_list %}
<input type="hidden" name="categories" value="{{ value }}">
{% endif %}
{% endfor %}
Expand Down Expand Up @@ -111,8 +111,8 @@ <h3 class="govuk-heading-s govuk-!-margin-top-2">{{ _('Categories') }}</h3>
{% set current_page = pages.current_page() %}
{% if current_page.has_previous() %}
<div class="govuk-pagination__prev">
{% if categories|length %}
<a class="govuk-link govuk-pagination__link" href="{{ url('search') }}?page={{ current_page.previous_page_number() }}&{{ params|urlencode }}&{{ categories|urlencode }}" rel="prev">
{% if form|category_tuples|length %}
<a class="govuk-link govuk-pagination__link" href="{{ url('search') }}?page={{ current_page.previous_page_number() }}&{{ params|urlencode }}&{{ form|category_tuples|urlencode }}" rel="prev">
{% else %}
<a class="govuk-link govuk-pagination__link" href="{{ url('search') }}?page={{ current_page.previous_page_number() }}&{{ params|urlencode }}" rel="prev">
{% endif %}
Expand All @@ -132,8 +132,8 @@ <h3 class="govuk-heading-s govuk-!-margin-top-2">{{ _('Categories') }}</h3>
{% else %}
<li class="govuk-pagination__item">
{% endif %}
{% if categories|length %}
<a class="govuk-link govuk-pagination__link" href="{{ url('search') }}?page={{ page_num }}&{{ params|urlencode }}&{{ categories|urlencode }}" aria-label="Page {{ page_num }}" aria-current="page">
{% if form|category_tuples|length %}
<a class="govuk-link govuk-pagination__link" href="{{ url('search') }}?page={{ page_num }}&{{ params|urlencode }}&{{ form|category_tuples|urlencode }}" aria-label="Page {{ page_num }}" aria-current="page">
{% else %}
<a class="govuk-link govuk-pagination__link" href="{{ url('search') }}?page={{ page_num }}&{{ params|urlencode }}" aria-label="Page {{ page_num }}" aria-current="page">
{% endif %}
Expand All @@ -144,8 +144,8 @@ <h3 class="govuk-heading-s govuk-!-margin-top-2">{{ _('Categories') }}</h3>
</ul>
{% if current_page.has_next() %}
<div class="govuk-pagination__next">
{% if categories|length %}
<a class="govuk-link govuk-pagination__link" href="{{ url('search') }}?page={{ current_page.next_page_number() }}&{{ params|urlencode }}&{{ categories|urlencode }}" rel="next">
{% if form|category_tuples|length %}
<a class="govuk-link govuk-pagination__link" href="{{ url('search') }}?page={{ current_page.next_page_number() }}&{{ params|urlencode }}&{{ form|category_tuples|urlencode }}" rel="next">
{% else %}
<a class="govuk-link govuk-pagination__link" href="{{ url('search') }}?page={{ current_page.next_page_number() }}&{{ params|urlencode }}" rel="next">
{% endif %}
Expand Down