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

✨ [#1821] Add visibility flags for Category model #835

Merged
merged 3 commits into from
Nov 9, 2023
Merged
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
5 changes: 4 additions & 1 deletion src/open_inwoner/cms/products/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def render(self, context, instance, placeholder):

# Highlighted categories
highlighted_categories = (
Category.objects.published().filter(highlighted=True).order_by("path")
Category.objects.published()
.visible_for_user(request.user)
.filter(highlighted=True)
.order_by("path")
)
if request.user.is_authenticated and request.user.selected_categories.exists():
categories = request.user.selected_categories.order_by("name")[: self.limit]
Expand Down
54 changes: 48 additions & 6 deletions src/open_inwoner/cms/products/tests/test_plugin_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,35 @@ def setUp(self):

def test_only_highlighted_categories_exist_in_context_when_they_exist(self):
CategoryFactory(name="Should be first")
highlighted_category = CategoryFactory(
name="This should be second", highlighted=True
highlighted_category1 = CategoryFactory(
name="This should be second",
highlighted=True,
visible_for_anonymous=True,
visible_for_authenticated=True,
)
highlighted_category2 = CategoryFactory(
path="0002",
highlighted=True,
visible_for_anonymous=True,
visible_for_authenticated=False,
)
highlighted_category3 = CategoryFactory(
path="0003",
highlighted=True,
visible_for_anonymous=False,
visible_for_authenticated=True,
)
highlighted_category4 = CategoryFactory(
path="0004",
highlighted=True,
visible_for_anonymous=False,
visible_for_authenticated=False,
)

html, context = cms_tools.render_plugin(CategoriesPlugin)
self.assertEqual(
list(context["categories"]),
[highlighted_category],
[highlighted_category1, highlighted_category2],
)

def test_highlighted_categories_are_ordered_by_alphabetically(self):
Expand All @@ -49,15 +70,36 @@ def test_highlighted_categories_are_ordered_by_alphabetically(self):
def test_only_highlighted_categories_are_shown_when_they_exist(self):
user = UserFactory()
category = CategoryFactory(name="Should be first")
highlighted_category = CategoryFactory(
name="This should be second", highlighted=True
highlighted_category1 = CategoryFactory(
name="This should be second",
highlighted=True,
visible_for_anonymous=True,
visible_for_authenticated=True,
)
highlighted_category2 = CategoryFactory(
path="0002",
highlighted=True,
visible_for_anonymous=True,
visible_for_authenticated=False,
)
highlighted_category3 = CategoryFactory(
path="0003",
highlighted=True,
visible_for_anonymous=False,
visible_for_authenticated=True,
)
highlighted_category4 = CategoryFactory(
path="0004",
highlighted=True,
visible_for_anonymous=False,
visible_for_authenticated=False,
)

html, context = cms_tools.render_plugin(CategoriesPlugin, user=user)

self.assertEqual(
list(context["categories"]),
[highlighted_category],
[highlighted_category1, highlighted_category3],
)

def test_category_selected(self):
Expand Down
57 changes: 55 additions & 2 deletions src/open_inwoner/components/tests/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,32 @@ def setUpTestData(cls):
# PrimaryNavigation.html requires apphook + categories
create_apphook_page(ProductsApphook)
cls.published1 = CategoryFactory(
path="0001", name="First one", slug="first-one"
path="0001",
name="First one",
slug="first-one",
visible_for_anonymous=True,
visible_for_authenticated=True,
)
cls.published2 = CategoryFactory(
path="0002", name="Second one", slug="second-one"
path="0002",
name="Second one",
slug="second-one",
visible_for_anonymous=True,
visible_for_authenticated=False,
)
cls.published3 = CategoryFactory(
path="0003",
name="Third one",
slug="third-one",
visible_for_anonymous=False,
visible_for_authenticated=True,
)
cls.published4 = CategoryFactory(
path="0004",
name="Fourth one",
slug="fourth-one",
visible_for_anonymous=False,
visible_for_authenticated=False,
)

def test_categories_hidden_from_anonymous_users(self):
Expand Down Expand Up @@ -55,6 +77,37 @@ def test_categories_not_hidden_from_anonymous_users(self):
self.assertEqual(categories[0].tag, "a")
self.assertEqual(categories[1].tag, "button")

links = [x for x in doc.find("[title='Onderwerpen'] + ul li a").items()]
self.assertEqual(len(links), 4)
self.assertEqual(links[0].attr("href"), self.published1.get_absolute_url())
self.assertEqual(links[1].attr("href"), self.published2.get_absolute_url())
self.assertEqual(links[2].attr("href"), self.published1.get_absolute_url())
self.assertEqual(links[3].attr("href"), self.published2.get_absolute_url())

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

self.client.force_login(self.user)

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

doc = PyQuery(response.content)

categories = doc.find("[title='Onderwerpen']")

self.assertEqual(len(categories), 2)
self.assertEqual(categories[0].tag, "a")
self.assertEqual(categories[1].tag, "button")

links = [x for x in doc.find("[title='Onderwerpen'] + ul li a").items()]
self.assertEqual(len(links), 4)
self.assertEqual(links[0].attr("href"), self.published1.get_absolute_url())
self.assertEqual(links[1].attr("href"), self.published3.get_absolute_url())
self.assertEqual(links[2].attr("href"), self.published1.get_absolute_url())
self.assertEqual(links[3].attr("href"), self.published3.get_absolute_url())

def test_search_bar_hidden_from_anonymous_users(self):
config = SiteConfiguration.get_solo()
config.hide_search_from_anonymous_users = True
Expand Down
27 changes: 23 additions & 4 deletions src/open_inwoner/pdc/admin/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.forms import BaseModelFormSet
from django.utils.translation import gettext as _

from django_better_admin_arrayfield.admin.mixins import DynamicArrayMixin
from django_better_admin_arrayfield.forms.widgets import DynamicArrayWidget
from import_export.admin import ImportExportMixin
from import_export.formats import base_formats
from ordered_model.admin import OrderedInlineModelAdminMixin, OrderedTabularInline
Expand Down Expand Up @@ -32,7 +34,7 @@ class CategoryAdminForm(movenodeform_factory(Category)):
class Meta:
model = Category
fields = "__all__"
widgets = {"description": CKEditorWidget}
widgets = {"description": CKEditorWidget, "zaaktypen": DynamicArrayWidget}

def clean(self, *args, **kwargs):
cleaned_data = super().clean(*args, **kwargs)
Expand Down Expand Up @@ -71,7 +73,9 @@ def clean(self):


@admin.register(Category)
class CategoryAdmin(OrderedInlineModelAdminMixin, ImportExportMixin, TreeAdmin):
class CategoryAdmin(
DynamicArrayMixin, OrderedInlineModelAdminMixin, ImportExportMixin, TreeAdmin
):
change_list_template = "admin/category_change_list.html"
form = CategoryAdminForm
inlines = (
Expand All @@ -81,8 +85,23 @@ class CategoryAdmin(OrderedInlineModelAdminMixin, ImportExportMixin, TreeAdmin):
prepopulated_fields = {"slug": ("name",)}
search_fields = ("name",)
ordering = ("path",)
list_display = ("name", "highlighted", "published")
list_editable = ("highlighted", "published")
list_display = (
"name",
"highlighted",
"published",
"visible_for_anonymous",
"visible_for_authenticated",
"visible_for_companies",
"visible_for_citizens",
)
list_editable = (
"highlighted",
"published",
"visible_for_anonymous",
"visible_for_authenticated",
"visible_for_companies",
"visible_for_citizens",
)
exclude = ("path", "depth", "numchild")

# import-export
Expand Down
7 changes: 7 additions & 0 deletions src/open_inwoner/pdc/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from ordered_model.models import OrderedModelQuerySet
from treebeard.mp_tree import MP_NodeQuerySet

from open_inwoner.accounts.models import User


class ProductQueryset(models.QuerySet):
def published(self):
Expand All @@ -22,6 +24,11 @@ def published(self):
def draft(self):
return self.filter(published=False)

def visible_for_user(self, user: User):
if user.is_authenticated:
return self.filter(visible_for_authenticated=True)
return self.filter(visible_for_anonymous=True)


class QuestionQueryset(OrderedModelQuerySet):
def general(self):
Expand Down
72 changes: 72 additions & 0 deletions src/open_inwoner/pdc/migrations/0061_auto_20231106_1207.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Generated by Django 3.2.20 on 2023-11-06 11:07

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("pdc", "0060_content_collapsable"),
]

operations = [
migrations.AddField(
model_name="category",
name="relevante_zaakperiode",
field=models.PositiveIntegerField(
blank=True,
help_text="Aantal maanden dat teruggekeken moet worden naar Zaken van deze zaaktypes.",
null=True,
verbose_name="Relevante zaakperiode",
),
),
migrations.AddField(
model_name="category",
name="visible_for_anonymous",
field=models.BooleanField(
default=True,
help_text="Of het onderwerp zichtbaar moet zijn op het anonieme deel (zonder inloggen).",
verbose_name="Anonieme deel",
),
),
migrations.AddField(
model_name="category",
name="visible_for_authenticated",
field=models.BooleanField(
default=True,
help_text="Of het onderwerp zichtbaar moet zijn op het beveiligde deel (achter inloggen).",
verbose_name="Beveiligde deel",
),
),
migrations.AddField(
model_name="category",
name="visible_for_citizens",
field=models.BooleanField(
default=True,
help_text="Of het onderwerp zichtbaar moet zijn wanneer iemand aangeeft een inwoner te zijn (of is ingelogd met BSN).",
verbose_name="Inwoner content",
),
),
migrations.AddField(
model_name="category",
name="visible_for_companies",
field=models.BooleanField(
default=True,
help_text="Of het onderwerp zichtbaar moet zijn wanneer iemand aangeeft een bedrijf te zijn (of is ingelogd met KvK).",
verbose_name="Bedrijven content",
),
),
migrations.AddField(
model_name="category",
name="zaaktypen",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(blank=True, max_length=1000),
blank=True,
default=list,
help_text="Zaaktypen waarvoor bij aanwezigheid dit onderwerp getoond moet worden.",
size=None,
verbose_name="Zaaktypen",
),
),
]
46 changes: 46 additions & 0 deletions src/open_inwoner/pdc/models/category.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -34,6 +35,51 @@ class Category(MP_Node):
default=False,
help_text=_("Whether the category should be published or not."),
)
visible_for_anonymous = models.BooleanField(
verbose_name=_("Anonieme deel"),
default=True,
help_text=_(
"Of het onderwerp zichtbaar moet zijn op het anonieme deel (zonder inloggen)."
),
)
visible_for_authenticated = models.BooleanField(
verbose_name=_("Beveiligde deel"),
default=True,
help_text=_(
"Of het onderwerp zichtbaar moet zijn op het beveiligde deel (achter inloggen)."
),
)
visible_for_companies = models.BooleanField(
verbose_name=_("Bedrijven content"),
default=True,
help_text=_(
"Of het onderwerp zichtbaar moet zijn wanneer iemand aangeeft een bedrijf te zijn (of is ingelogd met KvK)."
),
)
visible_for_citizens = models.BooleanField(
verbose_name=_("Inwoner content"),
default=True,
help_text=_(
"Of het onderwerp zichtbaar moet zijn wanneer iemand aangeeft een inwoner te zijn (of is ingelogd met BSN)."
),
)
zaaktypen = ArrayField(
models.CharField(max_length=1000, blank=True),
verbose_name=_("Zaaktypen"),
default=list,
blank=True,
help_text=_(
"Zaaktypen waarvoor bij aanwezigheid dit onderwerp getoond moet worden."
),
)
relevante_zaakperiode = models.PositiveIntegerField(
verbose_name=_("Relevante zaakperiode"),
blank=True,
null=True,
help_text=_(
"Aantal maanden dat teruggekeken moet worden naar Zaken van deze zaaktypes."
),
)
highlighted = models.BooleanField(
verbose_name=_("Highlighted"),
default=False,
Expand Down
Loading
Loading