From d46a3fd316a6ee6a13e594f5869c2619ca9f7d9f Mon Sep 17 00:00:00 2001 From: Igor Santos Date: Thu, 28 Mar 2024 13:58:08 -0300 Subject: [PATCH] feat(nossas): add plugin to list publications by category/page --- app/nossas/publications/cms_plugins.py | 36 +++++++++++++++++++ app/nossas/publications/forms.py | 19 +++++++++- .../migrations/0006_publicationlist.py | 26 ++++++++++++++ app/nossas/publications/models.py | 13 ++++++- .../publications/publication_list_plugin.html | 19 ++++++++++ app/nossas/settings.py | 1 + 6 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 app/nossas/publications/cms_plugins.py create mode 100644 app/nossas/publications/migrations/0006_publicationlist.py create mode 100644 app/nossas/publications/templates/nossas/publications/publication_list_plugin.html diff --git a/app/nossas/publications/cms_plugins.py b/app/nossas/publications/cms_plugins.py new file mode 100644 index 00000000..b9db800c --- /dev/null +++ b/app/nossas/publications/cms_plugins.py @@ -0,0 +1,36 @@ +from django.utils.translation import gettext_lazy as _ + +from cms.cms_plugins import CMSPluginBase, plugin_pool + +from .forms import PublicationListForm +from .models import Publication, PublicationList + + +@plugin_pool.register_plugin +class PublicationListPlugin(CMSPluginBase): + name = _("Listagem de Publicações") + module = "NOSSAS" + model = PublicationList + form = PublicationListForm + render_template = "nossas/publications/publication_list_plugin.html" + + # def get_form(self, request, obj=None, change=False, **kwargs): + # form = super().get_form(request, obj, change, **kwargs) + + # import ipdb;ipdb.set_trace() + # if not change and request.current_page.application_namespace: + # form.fields["category"].initial = request.current_page + + # return form + + + def render(self, context, instance, placeholder): + context = super().render(context, instance, placeholder) + qs = Publication.on_site.all() + + if instance.category: + qs = qs.filter(parent=instance.category) + + context["publication_list"] = qs + + return context \ No newline at end of file diff --git a/app/nossas/publications/forms.py b/app/nossas/publications/forms.py index 571ed30e..1a726d43 100644 --- a/app/nossas/publications/forms.py +++ b/app/nossas/publications/forms.py @@ -4,7 +4,7 @@ from cms.models import Page from nossas.design.fields import Select2PageSearchField -from .models import Publication +from .models import Publication, PublicationList class PublicationForm(forms.ModelForm): @@ -21,3 +21,20 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["parent"].queryset = Page.objects.drafts().on_site() + + + +class PublicationListForm(forms.ModelForm): + category = Select2PageSearchField( + label=_("Página Relacionada"), + required=False, + ) + + class Meta: + model = PublicationList + fields = "__all__" + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.fields["category"].queryset = Page.objects.drafts().on_site() \ No newline at end of file diff --git a/app/nossas/publications/migrations/0006_publicationlist.py b/app/nossas/publications/migrations/0006_publicationlist.py new file mode 100644 index 00000000..055a4e62 --- /dev/null +++ b/app/nossas/publications/migrations/0006_publicationlist.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2 on 2024-03-28 16:40 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('cms', '0022_auto_20180620_1551'), + ('publications', '0005_alter_publication_managers_publication_external_link'), + ] + + operations = [ + migrations.CreateModel( + name='PublicationList', + fields=[ + ('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='%(app_label)s_%(class)s', serialize=False, to='cms.cmsplugin')), + ('category', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='cms.page', verbose_name='Página Relacionada')), + ], + options={ + 'abstract': False, + }, + bases=('cms.cmsplugin',), + ), + ] diff --git a/app/nossas/publications/models.py b/app/nossas/publications/models.py index a5aab4ae..d37ca922 100644 --- a/app/nossas/publications/models.py +++ b/app/nossas/publications/models.py @@ -2,7 +2,7 @@ from django.utils.translation import gettext_lazy as _ from django.urls import reverse -from cms.models import Page +from cms.models import Page, CMSPlugin from cms.models.fields import PlaceholderField from filer.fields.image import FilerImageField from tag_fields.managers import ModelTagsManager @@ -70,3 +70,14 @@ def get_absolute_url(self): @property def get_pub_date(self): return self.created_at + + + +class PublicationList(CMSPlugin): + category = models.ForeignKey( + Page, + verbose_name=_("Página Relacionada"), + blank=True, + null=True, + on_delete=models.SET_NULL, + ) \ No newline at end of file diff --git a/app/nossas/publications/templates/nossas/publications/publication_list_plugin.html b/app/nossas/publications/templates/nossas/publications/publication_list_plugin.html new file mode 100644 index 00000000..0a551cd1 --- /dev/null +++ b/app/nossas/publications/templates/nossas/publications/publication_list_plugin.html @@ -0,0 +1,19 @@ +
+ {% for p in publication_list %} + + {% endfor %} +
\ No newline at end of file diff --git a/app/nossas/settings.py b/app/nossas/settings.py index 922e6c95..f217ed46 100644 --- a/app/nossas/settings.py +++ b/app/nossas/settings.py @@ -110,6 +110,7 @@ "TeamAccordionPlugin", "TextPlugin", "VideoPlayerPlugin", + "PublicationListPlugin" ] CMS_TEMPLATES = [