diff --git a/froide_govplan/cms_plugins.py b/froide_govplan/cms_plugins.py index 70f6335..04652a8 100644 --- a/froide_govplan/cms_plugins.py +++ b/froide_govplan/cms_plugins.py @@ -8,6 +8,7 @@ from .models import ( GovernmentPlansCMSPlugin, GovernmentPlanSection, GovernmentPlanSectionsCMSPlugin, + GovernmentPlanUpdatesCMSPlugin, ) @@ -51,3 +52,18 @@ class GovernmentPlanSectionsPlugin(CMSPluginBase): context["sections"] = sections return context + + +@plugin_pool.register_plugin +class GovernmentPlanUpdatesPlugin(CMSPluginBase): + name = _("Government plan updates") + model = GovernmentPlanUpdatesCMSPlugin + render_template = "froide_govplan/plugins/updates.html" + + def render(self, context, instance, placeholder): + context = super().render(context, instance, placeholder) + context["updates"] = instance.get_updates( + context["request"], published_only=False + ) + context["show_context"] = True + return context diff --git a/froide_govplan/migrations/0009_governmentplanupdatescmsplugin.py b/froide_govplan/migrations/0009_governmentplanupdatescmsplugin.py new file mode 100644 index 0000000..26532b0 --- /dev/null +++ b/froide_govplan/migrations/0009_governmentplanupdatescmsplugin.py @@ -0,0 +1,30 @@ +# Generated by Django 3.2.12 on 2022-03-17 14:54 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('cms', '0022_auto_20180620_1551'), + ('publicbody', '0039_publicbody_alternative_emails'), + ('froide_govplan', '0008_governmentplan_proposals'), + ] + + operations = [ + migrations.CreateModel( + name='GovernmentPlanUpdatesCMSPlugin', + 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_governmentplanupdatescmsplugin', serialize=False, to='cms.cmsplugin')), + ('count', models.PositiveIntegerField(default=1, help_text='0 means all the updates', verbose_name='number of updates')), + ('offset', models.PositiveIntegerField(default=0, help_text='number of updates to skip from top of list', verbose_name='offset')), + ('categories', models.ManyToManyField(blank=True, to='publicbody.Category', verbose_name='categories')), + ('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 9f4ba50..560485a 100644 --- a/froide_govplan/models.py +++ b/froide_govplan/models.py @@ -220,6 +220,11 @@ class GovernmentPlan(models.Model): "{}#p-{}".format(self.government.planning_document, ref) for ref in refs ] + def get_section(self): + return GovernmentPlanSection.objects.filter( + categories__in=self.categories.all() + ).first() + def update_from_updates(self): last_status_update = ( self.updates.all().filter(public=True).exclude(status="").first() @@ -507,3 +512,58 @@ if CMSPlugin: government = models.ForeignKey( Government, null=True, blank=True, on_delete=models.SET_NULL ) + + class GovernmentPlanUpdatesCMSPlugin(CMSPlugin): + """ + CMS Plugin for displaying plan updates + """ + + government = models.ForeignKey( + Government, null=True, blank=True, on_delete=models.SET_NULL + ) + categories = models.ManyToManyField( + Category, verbose_name=_("categories"), blank=True + ) + + count = models.PositiveIntegerField( + _("number of updates"), default=1, help_text=_("0 means all the updates") + ) + offset = models.PositiveIntegerField( + _("offset"), + default=0, + help_text=_("number of updates to skip from top of list"), + ) + + def get_updates(self, request, published_only=True): + # TODO: remove duplication with GovernmentPlansCMSPlugin.get_plans + if ( + published_only + or not request + or not getattr(request, "toolbar", False) + or not request.toolbar.edit_mode_active + ): + updates = GovernmentPlanUpdate.objects.filter(public=True) + else: + updates = GovernmentPlanUpdate.objects.all() + + updates = updates.order_by("-timestamp").prefetch_related( + "plan", "plan__categories" + ) + + filters = {} + if self.government_id: + filters["plan__government_id"] = self.government_id + + cat_list = self.categories.all().values_list("id", flat=True) + if cat_list: + filters["plan__categories__in"] = cat_list + + updates = updates.filter(**filters).distinct() + if self.count == 0: + return updates[self.offset :] + return updates[self.offset : self.offset + self.count] + + def __str__(self): + if self.count == 0: + return str(_("All matching updates")) + return _("%s matching updates") % self.count diff --git a/froide_govplan/templates/froide_govplan/detail.html b/froide_govplan/templates/froide_govplan/detail.html index f3e2b54..b8b31cd 100644 --- a/froide_govplan/templates/froide_govplan/detail.html +++ b/froide_govplan/templates/froide_govplan/detail.html @@ -159,41 +159,9 @@
{% if update.user %}von {{ update.user.get_full_name }}{% endif %}{% if update.organization %}, {{ update.organization.name }}{% endif %}
-