From 03342fde2dcd1db4c8de8b6b8276dda24193a46c Mon Sep 17 00:00:00 2001 From: krmax44 Date: Thu, 17 Mar 2022 16:38:47 +0100 Subject: [PATCH] add updates plugin --- froide_govplan/cms_plugins.py | 16 +++++ .../0009_governmentplanupdatescmsplugin.py | 30 ++++++++++ froide_govplan/models.py | 60 +++++++++++++++++++ .../templates/froide_govplan/detail.html | 36 +---------- .../froide_govplan/plugins/updates.html | 53 ++++++++++++++++ froide_govplan/views.py | 7 +-- 6 files changed, 162 insertions(+), 40 deletions(-) create mode 100644 froide_govplan/migrations/0009_governmentplanupdatescmsplugin.py create mode 100644 froide_govplan/templates/froide_govplan/plugins/updates.html 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 @@
- {% for update in updates %} -
-
-
- -
-

{{ update.title }}

-
- -

{% if update.user %}von {{ update.user.get_full_name }}{% endif %}{% if update.organization %}, {{ update.organization.name }}{% endif %}

-
-
-
- {% if update.content %} -
- {{ update.content|markdown }} -
- {% endif %} -
- {% if update.url or update.foirequest %} -
- {% if update.url %} - → Mehr auf {{ update.get_url_domain }} lesen… - {% endif %} + {% include "froide_govplan/plugins/updates.html" with wrapper_classes="col col-12 col-lg-6 d-flex mb-4" %} - {% if update.foirequest %} - → zur Anfrage - {% endif %} -
- {% endif %} -
-
- {% endfor %} - -
+
diff --git a/froide_govplan/templates/froide_govplan/plugins/updates.html b/froide_govplan/templates/froide_govplan/plugins/updates.html new file mode 100644 index 0000000..bb2e76a --- /dev/null +++ b/froide_govplan/templates/froide_govplan/plugins/updates.html @@ -0,0 +1,53 @@ +{% load markup %} + +{% for update in updates %} +
+ +
+{% endfor %} \ No newline at end of file diff --git a/froide_govplan/views.py b/froide_govplan/views.py index cd9d581..c0c28d8 100644 --- a/froide_govplan/views.py +++ b/froide_govplan/views.py @@ -53,17 +53,12 @@ class GovPlanDetailView(GovernmentMixin, DetailView): "responsible_publicbody", "organization" ) - def get_section(self): - return GovernmentPlanSection.objects.filter( - categories__in=self.object.categories.all() - ).first() - def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["updates"] = self.object.updates.filter(public=True).order_by( "-timestamp" ) - context["section"] = self.get_section() + context["section"] = self.object.get_section() if self.request.user.is_authenticated: context["update_proposal_form"] = GovernmentPlanUpdateProposalForm() # For CMS toolbar