Skip to content

Commit

Permalink
✨ [#1821] Category visibility based on user login status
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Nov 6, 2023
1 parent da3bd42 commit 2fc7686
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
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
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
2 changes: 1 addition & 1 deletion src/open_inwoner/pdc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class CategoryListView(
model = Category

def get_queryset(self):
return Category.get_root_nodes().published()
return Category.get_root_nodes().published().visible_for_user(self.request.user)

@cached_property
def crumbs(self):
Expand Down
4 changes: 3 additions & 1 deletion src/open_inwoner/utils/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def settings(request):
"cookie_link_text": config.cookie_link_text,
"cookie_link_url": config.cookie_link_url,
"extra_css": config.extra_css,
"menu_categories": Category.get_root_nodes().published(),
"menu_categories": (
Category.get_root_nodes().published().visible_for_user(request.user)
),
# default SearchForm, might be overwritten by actual SearchView
"search_form": SearchForm(auto_id=False),
"search_filter_categories": config.search_filter_categories,
Expand Down

0 comments on commit 2fc7686

Please sign in to comment.