Skip to content

Commit

Permalink
Add include_in_rss field to Post (#728)
Browse files Browse the repository at this point in the history
* Added include_in_rss field to Post

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Gerben <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 20, 2023
1 parent c6f85df commit a6eb725
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions changes/663.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add include in rss field to post, to be able to exclude blog items from rss feed.
2 changes: 1 addition & 1 deletion djangocms_blog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class PostAdmin(PlaceholderAdminMixin, FrontendEditableAdminMixin, ModelAppHookC
if apps.is_installed("djangocms_blog.liveblog"):
actions += ["enable_liveblog", "disable_liveblog"]
_fieldsets = [
(None, {"fields": ["title", "subtitle", "slug", "publish", ["categories", "app_config"]]}),
(None, {"fields": ["title", "subtitle", "slug", "publish", "include_in_rss", ["categories", "app_config"]]}),
# left empty for sites, author and related fields
(None, {"fields": [[]]}),
(
Expand Down
6 changes: 5 additions & 1 deletion djangocms_blog/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ def description(self):
return _("Blog articles on %(site_name)s") % {"site_name": Site.objects.get_current().name}

def items(self, obj=None):
return Post.objects.namespace(self.namespace).published().order_by("-date_published")[: self.feed_items_number]
return (
Post.objects.namespace(self.namespace)
.published_on_rss()
.order_by("-date_published")[: self.feed_items_number]
)

def item_title(self, item):
return mark_safe(item.safe_translation_getter("title"))
Expand Down
7 changes: 7 additions & 0 deletions djangocms_blog/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def published(self, current_site=True):
else:
return queryset

def published_on_rss(self, current_site=True):
queryset = self.published_future(current_site)
return queryset.exclude(include_in_rss=False)

def published_future(self, current_site=True):
if current_site:
queryset = self.on_site()
Expand Down Expand Up @@ -144,6 +148,9 @@ def get_queryset(self, *args, **kwargs):
def published(self, current_site=True):
return self.get_queryset().published(current_site)

def published_on_rss(self, current_site=True):
return self.get_queryset().published_on_rss(current_site)

def available(self, current_site=True):
return self.get_queryset().available(current_site)

Expand Down
17 changes: 17 additions & 0 deletions djangocms_blog/migrations/0040_post_include_in_rss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.2 on 2021-10-25 12:44

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("djangocms_blog", "0039_auto_20200331_2227"),
]

operations = [
migrations.AddField(
model_name="post",
name="include_in_rss",
field=models.BooleanField(default=True, verbose_name="include in RSS feed"),
),
]
1 change: 1 addition & 0 deletions djangocms_blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class Post(KnockerModel, BlogMetaMixin, TranslatableModel):
date_published_end = models.DateTimeField(_("published until"), null=True, blank=True)
date_featured = models.DateTimeField(_("featured date"), null=True, blank=True)
publish = models.BooleanField(_("publish"), default=False)
include_in_rss = models.BooleanField(_("include in RSS feed"), default=True)
categories = models.ManyToManyField(
"djangocms_blog.BlogCategory", verbose_name=_("category"), related_name="blog_posts", blank=True
)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,21 @@ def test_feed(self):
feed.config = self.app_config_1
self.assertEqual(list(feed.items("tag-2")), [posts[0]])

with smart_override("en"):
with switch_language(posts[0], "en"):
posts[0].include_in_rss = False
posts[0].save()

request = self.get_page_request(pages[1], self.user, path=posts[0].get_absolute_url())

feed = LatestEntriesFeed()
feed.namespace, feed.config = get_app_instance(request)
self.assertEqual(len(list(feed.items())), 0)
self.reload_urlconf()

posts[0].include_in_rss = True
posts[0].save()


class SitemapViewTest(BaseTest):
def test_sitemap(self):
Expand Down

0 comments on commit a6eb725

Please sign in to comment.