Skip to content

Commit

Permalink
feat(nossas): add plugin to list publications by category/page
Browse files Browse the repository at this point in the history
  • Loading branch information
igr-santos committed Mar 28, 2024
1 parent 0fac1b0 commit d46a3fd
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
36 changes: 36 additions & 0 deletions app/nossas/publications/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 18 additions & 1 deletion app/nossas/publications/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
26 changes: 26 additions & 0 deletions app/nossas/publications/migrations/0006_publicationlist.py
Original file line number Diff line number Diff line change
@@ -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',),
),
]
13 changes: 12 additions & 1 deletion app/nossas/publications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="grid">
{% for p in publication_list %}
<div class="g-col-12 g-col-md-6">
<a href="{{ p.get_absolute_url }}" class="text-reset text-decoration-none">
<div class="card">
{% if p.image_default %}
<img src="{{ p.image_default.url }}" class="card-img-top" alt="{{ p.name }}">
{% endif %}
<div class="card-body">
<div class="card-body-content">
<h3>{{ p.title }}</h3>
<p>{{ p.description }}</p>
</div>
</div>
</div>
</a>
</div>
{% endfor %}
</div>
1 change: 1 addition & 0 deletions app/nossas/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"TeamAccordionPlugin",
"TextPlugin",
"VideoPlayerPlugin",
"PublicationListPlugin"
]

CMS_TEMPLATES = [
Expand Down

0 comments on commit d46a3fd

Please sign in to comment.