From b1b5f6c90816d1c43daffd7aca687ba920dfb856 Mon Sep 17 00:00:00 2001 From: vasileios Date: Thu, 2 Jun 2022 08:38:11 +0200 Subject: [PATCH 1/2] [#669] Show root and children themas on home page --- src/open_inwoner/pdc/tests/test_category.py | 18 ++++++------------ src/open_inwoner/pdc/views.py | 14 ++++++-------- src/open_inwoner/templates/pages/home.html | 2 +- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/open_inwoner/pdc/tests/test_category.py b/src/open_inwoner/pdc/tests/test_category.py index 4d6a5cae15..50af5dc939 100644 --- a/src/open_inwoner/pdc/tests/test_category.py +++ b/src/open_inwoner/pdc/tests/test_category.py @@ -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): @@ -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], diff --git a/src/open_inwoner/pdc/views.py b/src/open_inwoner/pdc/views.py index 30c4091c2b..947126ea78 100644 --- a/src/open_inwoner/pdc/views.py +++ b/src/open_inwoner/pdc/views.py @@ -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 @@ -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: diff --git a/src/open_inwoner/templates/pages/home.html b/src/open_inwoner/templates/pages/home.html index 618bc951f3..f798aa3f28 100644 --- a/src/open_inwoner/templates/pages/home.html +++ b/src/open_inwoner/templates/pages/home.html @@ -24,7 +24,7 @@

{% 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 %} From 6c32255193765e35bc70623c430f6bf7d4ba3bd4 Mon Sep 17 00:00:00 2001 From: vasileios Date: Thu, 2 Jun 2022 08:51:45 +0200 Subject: [PATCH 2/2] [#669] Fixed black formatting --- .../pdc/migrations/0038_auto_20220601_1435.py | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/open_inwoner/pdc/migrations/0038_auto_20220601_1435.py b/src/open_inwoner/pdc/migrations/0038_auto_20220601_1435.py index 99fc9bef07..6d867f047f 100644 --- a/src/open_inwoner/pdc/migrations/0038_auto_20220601_1435.py +++ b/src/open_inwoner/pdc/migrations/0038_auto_20220601_1435.py @@ -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", + ), ), ]