/
feeds.py
74 lines (59 loc) · 2.36 KB
/
feeds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Forum feeds
===========
This module defines the feed abstractions provided by the forum application. It allows to
generate RSS feeds.
"""
from django.contrib.syndication.views import Feed
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from machina.conf import settings as machina_settings
from machina.core.db.models import get_model
Forum = get_model('forum', 'Forum')
Topic = get_model('forum_conversation', 'Topic')
class LastTopicsFeed(Feed):
""" Provides feed items for the latest forum topics. """
# Standard RSS elements
title = _('Latest topics')
description = _('Latest topics updated on the forums')
link = reverse_lazy('forum:index')
# Item elements
title_template = 'forum_feeds/topics_title.html'
description_template = 'forum_feeds/topics_description.html'
def get_object(self, request, *args, **kwargs):
""" Handles the considered object. """
forum_pk = kwargs.get('forum_pk', None)
descendants = kwargs.get('descendants', None)
self.user = request.user
if forum_pk:
forum = get_object_or_404(Forum, pk=forum_pk)
forums_qs = (
forum.get_descendants(include_self=True)
if descendants else Forum.objects.filter(pk=forum_pk)
)
self.forums = request.forum_permission_handler.get_readable_forums(
forums_qs, request.user,
)
else:
self.forums = request.forum_permission_handler.get_readable_forums(
Forum.objects.all(), request.user,
)
def items(self):
""" Returns the items to include into the feed. """
return Topic.objects.filter(forum__in=self.forums).filter(
machina_settings.APPROVED_FILTER).order_by('-last_post_on')
def item_link(self, item):
""" Generates a link for a specific item of the feed. """
return reverse_lazy(
'forum_conversation:topic',
kwargs={
'forum_slug': item.forum.slug,
'forum_pk': item.forum.pk,
'slug': item.slug,
'pk': item.id,
},
)
def item_pubdate(self, item):
""" Returns the item's creation date or update date. """
return item.created