Skip to content

Commit

Permalink
[#1657] add option to hide search bar and page from anonymous users
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-sigma committed Aug 16, 2023
1 parent 7a7eb6f commit 02fed6d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
<div class="header__submenu">

{% if cms_apps.products %}
{% if request.user.is_authenticated or not config.hide_search_from_anonymous_users %}
<nav class="header__actions" aria-label="Zoek navigatie mobiel">
{% url 'search:search' as search_url %}
{% render_form form=search_form method="GET" form_action=search_url inline=True spaceless=False %}
{% input search_form.query no_label=True %}
{% form_actions primary_icon="search" primary_text=_("Zoeken") hide_primary_text=True %}
{% endrender_form %}
</nav>
{% endif %}
{% endif %}

<nav class="primary-navigation" aria-label="Hoofd navigatie">
Expand Down Expand Up @@ -109,7 +111,7 @@
{% include "components/Header/PrimaryNavigation.html" %}

{% if cms_apps.products %}
{% if request.user.is_authenticated or not config.hide_categories_from_anonymous_users %}
{% if request.user.is_authenticated or not config.hide_search_from_anonymous_users %}
<nav class="header__actions" aria-label="Zoek navigatie desktop">
{% url 'search:search' as search_url %}
{% render_form form=search_form method="GET" form_action=search_url inline=True spaceless=True %}
Expand All @@ -125,7 +127,7 @@
</header>

{% if cms_apps.products %}
{% if request.user.is_authenticated or not config.hide_categories_from_anonymous_users %}
{% if request.user.is_authenticated or not config.hide_search_from_anonymous_users %}
<section class="search search__mobile">
<nav class="search__actions " aria-label="Zoek navigatie mobiel">
{% url 'search:search' as search_url %}
Expand Down
27 changes: 27 additions & 0 deletions src/open_inwoner/components/tests/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,30 @@ def test_categories_not_hidden_from_anonymous_users(self):
self.assertEqual(len(categories), 2)
self.assertEqual(categories[0].tag, "a")
self.assertEqual(categories[1].tag, "button")

def test_search_bar_hidden_from_anonymous_users(self):
config = SiteConfiguration.get_solo()
config.hide_search_from_anonymous_users = True
config.save()

response = self.client.get("/")

doc = PyQuery(response.content)

search_buttons = doc.find("[title='Zoeken']")

self.assertEqual(len(search_buttons), 0)

def test_search_bar_not_hidden_from_anonymous_users(self):
config = SiteConfiguration.get_solo()
config.hide_search_from_anonymous_users = False
config.save()

response = self.client.get("/")

doc = PyQuery(response.content)

search_buttons = doc.find("[title='Zoeken']")

for button in search_buttons:
self.assertEqual(button.tag, "button")
7 changes: 6 additions & 1 deletion src/open_inwoner/configurations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ class SiteConfigurarionAdmin(OrderedInlineModelAdminMixin, SingletonModelAdmin):
),
(
_("Display options for anonymous users"),
{"fields": ("hide_categories_from_anonymous_users",)},
{
"fields": (
"hide_categories_from_anonymous_users",
"hide_search_from_anonymous_users",
)
},
),
)
inlines = [SiteConfigurationPageInline]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.2.20 on 2023-08-16 10:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
(
"configurations",
"0047_siteconfiguration_hide_categories_from_anonymous_users",
),
]

operations = [
migrations.AddField(
model_name="siteconfiguration",
name="hide_search_from_anonymous_users",
field=models.BooleanField(
default=False,
help_text="If checked, only authenticated users will be able to search the page.",
verbose_name="Hide search from anonymouns users",
),
),
]
7 changes: 7 additions & 0 deletions src/open_inwoner/configurations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ class SiteConfiguration(SingletonModel):
"If checked, categories will be hidden from users who are not logged in."
),
)
hide_search_from_anonymous_users = models.BooleanField(
verbose_name=_("Hide search from anonymouns users"),
default=False,
help_text=_(
"If checked, only authenticated users will be able to search the page."
),
)

class Meta:
verbose_name = _("Site Configuration")
Expand Down
25 changes: 25 additions & 0 deletions src/open_inwoner/search/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.test import TestCase
from django.urls import reverse

from open_inwoner.configurations.models import SiteConfiguration


class TestSearchView(TestCase):
def test_search_hidden_from_anonymous_users(self):
config = SiteConfiguration.get_solo()
config.hide_categories_from_anonymous_users = True
config.save()

response = self.client.get(reverse("search:search"))

self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/accounts/login/?next=/search/")

def test_search_not_hidden_from_anonymous_users(self):
config = SiteConfiguration.get_solo()
config.hide_categories_from_anonymous_users = False
config.save()

response = self.client.get(reverse("search:search"))

self.assertEqual(response.status_code, 200)
12 changes: 10 additions & 2 deletions src/open_inwoner/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
from django.utils.translation import gettext as _
from django.views.generic import FormView

from open_inwoner.configurations.models import SiteConfiguration
from open_inwoner.utils.mixins import PaginationMixin
from open_inwoner.utils.views import CommonPageMixin, LogMixin
from open_inwoner.utils.views import CommonPageMixin, LoginMaybeRequiredMixin, LogMixin

from .forms import FeedbackForm, SearchForm
from .searches import search_products


class SearchView(LogMixin, CommonPageMixin, PaginationMixin, FormView):
class SearchView(
LoginMaybeRequiredMixin, LogMixin, CommonPageMixin, PaginationMixin, FormView
):
form_class = SearchForm
template_name = "pages/search.html"
paginate_by = 20
Expand Down Expand Up @@ -135,3 +138,8 @@ def form_valid(self, form):
),
)
return HttpResponseRedirect(http_referer)

@property
def display_restricted(self):
config = SiteConfiguration.get_solo()
return config.hide_categories_from_anonymous_users is True

0 comments on commit 02fed6d

Please sign in to comment.