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

[#669] Show root and children themas on home page #254

Merged
merged 2 commits into from
Jun 9, 2022
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
40 changes: 30 additions & 10 deletions src/open_inwoner/pdc/migrations/0038_auto_20220601_1435.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,43 @@ class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.FILER_IMAGE_MODEL),
('pdc', '0037_category_highlighted'),
("pdc", "0037_category_highlighted"),
]

operations = [
migrations.AddField(
model_name='product',
name='icon',
field=filer.fields.image.FilerImageField(blank=True, help_text='Icon of the product', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='product_icons', to=settings.FILER_IMAGE_MODEL, verbose_name='Icon'),
model_name="product",
name="icon",
field=filer.fields.image.FilerImageField(
blank=True,
help_text="Icon of the product",
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="product_icons",
to=settings.FILER_IMAGE_MODEL,
verbose_name="Icon",
),
),
migrations.AddField(
model_name='product',
name='image',
field=filer.fields.image.FilerImageField(blank=True, help_text='Image of the product', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='product_images', to=settings.FILER_IMAGE_MODEL, verbose_name='Image'),
model_name="product",
name="image",
field=filer.fields.image.FilerImageField(
blank=True,
help_text="Image of the product",
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="product_images",
to=settings.FILER_IMAGE_MODEL,
verbose_name="Image",
),
),
migrations.AlterField(
model_name='category',
name='highlighted',
field=models.BooleanField(default=False, help_text='Wether the category should be highlighted or not.', verbose_name='Highlighted'),
model_name="category",
name="highlighted",
field=models.BooleanField(
default=False,
help_text="Wether the category should be highlighted or not.",
verbose_name="Highlighted",
),
),
]
18 changes: 6 additions & 12 deletions src/open_inwoner/pdc/tests/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
from .factories import CategoryFactory


class TestHighlightedCategory(WebTest):
def test_highlighted_categories_exist_in_context_when_anonymous(self):
category = CategoryFactory(name="Should be first")
class TestCategoryContext(WebTest):
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
)
response = self.app.get(reverse("root"))
self.assertEqual(
response.context["highlighted_categories"].first(), highlighted_category
)
self.assertNotIn(category, response.context["highlighted_categories"])
self.assertEqual(
list(response.context["categories"]),
[category, highlighted_category],
[highlighted_category],
)

def test_highlighted_categories_are_ordered_by_alphabetically(self):
Expand All @@ -33,19 +29,17 @@ def test_highlighted_categories_are_ordered_by_alphabetically(self):
response = self.app.get(reverse("root"))

self.assertEqual(
list(response.context["highlighted_categories"]),
list(response.context["categories"]),
[highlighted_category1, highlighted_category2],
)

def test_highlighted_categories_do_not_exist_in_context_when_logged_in(self):
def test_all_categories_exist_in_context_when_logged_in(self):
user = UserFactory()
category = CategoryFactory(name="Should be first")
highlighted_category = CategoryFactory(
name="This should be second", highlighted=True
)
response = self.app.get(reverse("root"), user=user)
with self.assertRaises(KeyError):
response.context["highlighted_categories"]
self.assertEqual(
list(response.context["categories"]),
[category, highlighted_category],
Expand Down
14 changes: 6 additions & 8 deletions src/open_inwoner/pdc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from open_inwoner.pdc.models.product import ProductCondition
from open_inwoner.plans.models import Plan
from open_inwoner.questionnaire.models import QuestionnaireStep
from open_inwoner.utils.views import CustomDetailBreadcrumbMixin

from .choices import YesNo
from .forms import ProductFinderForm
Expand Down Expand Up @@ -59,19 +58,18 @@ def get_context_data(self, **kwargs):
config = SiteConfiguration.get_solo()

limit = 3 if self.request.user.is_authenticated else 4
kwargs.update(categories=Category.get_root_nodes()[:limit])
kwargs.update(categories=Category.objects.all().order_by("name")[:limit])
kwargs.update(product_locations=ProductLocation.objects.all()[:1000])
kwargs.update(questionnaire_roots=QuestionnaireStep.get_root_nodes())
if self.request.user.is_authenticated:
kwargs.update(plans=Plan.objects.connected(self.request.user)[:limit])

# Highlighted categories
if not self.request.user.is_authenticated:
kwargs.update(
highlighted_categories=Category.get_root_nodes()
.filter(highlighted=True)
.order_by("name")[:limit]
)
highlighted_categories = (
Category.objects.all().filter(highlighted=True).order_by("name")[:limit]
)
if not self.request.user.is_authenticated and highlighted_categories:
kwargs.update(categories=highlighted_categories)

# Product finder:
if config.show_product_finder:
Expand Down
2 changes: 1 addition & 1 deletion src/open_inwoner/templates/pages/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1 class="h1">
{% if request.user.is_authenticated %}
{% card_container categories=categories columns=3 %}
{% else %}
{% card_container categories=highlighted_categories %}
{% card_container categories=categories %}
{% endif %}

{% if questionnaire_roots.exists %}
Expand Down