diff --git a/froide_govplan/cms_plugins.py b/froide_govplan/cms_plugins.py index 83661b2..d6cb553 100644 --- a/froide_govplan/cms_plugins.py +++ b/froide_govplan/cms_plugins.py @@ -3,7 +3,12 @@ from django.utils.translation import gettext_lazy as _ from cms.plugin_base import CMSPluginBase from cms.plugin_pool import plugin_pool -from .models import PLUGIN_TEMPLATES, GovernmentPlansCMSPlugin +from .models import ( + PLUGIN_TEMPLATES, + GovernmentPlanSection, + GovernmentPlansCMSPlugin, + GovernmentPlanSectionsCMSPlugin, +) @plugin_pool.register_plugin @@ -22,3 +27,24 @@ class GovernmentPlansPlugin(CMSPluginBase): context["request"], published_only=False ) return context + + +@plugin_pool.register_plugin +class GovernmentPlanSectionsPlugin(CMSPluginBase): + name = _("Government plan sections") + model = GovernmentPlanSectionsCMSPlugin + render_template = "froide_govplan/plugins/sections.html" + + def render(self, context, instance, placeholder): + context = super().render(context, instance, placeholder) + + if instance.government: + sections = GovernmentPlanSection.objects.filter( + government=instance.government + ) + else: + sections = GovernmentPlanSection.objects.all() + + context["sections"] = sections + + return context diff --git a/froide_govplan/migrations/0006_sections_cms_plugin.py b/froide_govplan/migrations/0006_sections_cms_plugin.py new file mode 100644 index 0000000..46228b6 --- /dev/null +++ b/froide_govplan/migrations/0006_sections_cms_plugin.py @@ -0,0 +1,45 @@ +# Generated by Django 3.2.12 on 2022-03-14 12:10 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("cms", "0022_auto_20180620_1551"), + ("froide_govplan", "0005_governmentplansection"), + ] + + operations = [ + migrations.CreateModel( + name="GovernmentPlanSectionsCMSPlugin", + fields=[ + ( + "cmsplugin_ptr", + models.OneToOneField( + auto_created=True, + on_delete=django.db.models.deletion.CASCADE, + parent_link=True, + primary_key=True, + related_name="froide_govplan_governmentplansectionscmsplugin", + serialize=False, + to="cms.cmsplugin", + ), + ), + ( + "government", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="froide_govplan.government", + ), + ), + ], + options={ + "abstract": False, + }, + bases=("cms.cmsplugin",), + ), + ] diff --git a/froide_govplan/models.py b/froide_govplan/models.py index e0851f5..1a12070 100644 --- a/froide_govplan/models.py +++ b/froide_govplan/models.py @@ -435,3 +435,12 @@ if CMSPlugin: if self.count == 0: return plans[self.offset :] return plans[self.offset : self.offset + self.count] + + class GovernmentPlanSectionsCMSPlugin(CMSPlugin): + """ + CMS Plugin for displaying plan sections + """ + + government = models.ForeignKey( + Government, null=True, blank=True, on_delete=models.SET_NULL + ) diff --git a/froide_govplan/templates/froide_govplan/plugins/sections.html b/froide_govplan/templates/froide_govplan/plugins/sections.html new file mode 100644 index 0000000..5afd809 --- /dev/null +++ b/froide_govplan/templates/froide_govplan/plugins/sections.html @@ -0,0 +1,33 @@ +{% load thumbnail %} + + + +