2 from django.contrib.syndication.views import Feed, FeedDoesNotExist
5 from django.contrib.syndication.feeds import Feed, FeedDoesNotExist
8 from django.http import HttpResponse
9 from django.utils.translation import ugettext as _
10 from django.utils.safestring import mark_safe
11 from models import Question
12 from forum import settings
14 class BaseNodeFeed(Feed):
16 title_template = "feeds/rss_title.html"
17 description_template = "feeds/rss_description.html"
19 def __init__(self, request, title, description, url):
21 self._description = mark_safe(description)
25 super(BaseNodeFeed, self).__init__('', request)
33 def description(self):
34 return self._description
36 def item_title(self, item):
39 def item_description(self, item):
42 def item_link(self, item):
43 return item.leaf.get_absolute_url()
45 def item_author_name(self, item):
46 return item.author.username
48 def item_author_link(self, item):
49 return item.author.get_profile_url()
51 def item_pubdate(self, item):
55 def __call__(self, request):
56 feedgen = self.get_feed('')
57 response = HttpResponse(mimetype=feedgen.mime_type)
58 feedgen.write(response, 'utf-8')
62 class RssQuestionFeed(BaseNodeFeed):
63 def __init__(self, request, question_list, title, description):
64 url = request.path + "&" + "&".join(["%s=%s" % (k, v) for k, v in request.GET.items() if not k in (_('page'), _('pagesize'), _('sort'))])
65 super(RssQuestionFeed, self).__init__(request, title, description, url)
67 self._question_list = question_list
69 def item_categories(self, item):
70 return item.tagname_list()
73 return self._question_list[:30]
75 class RssAnswerFeed(BaseNodeFeed):
77 title_template = "feeds/rss_answer_title.html"
79 def __init__(self, request, question, include_comments=False):
80 super(RssAnswerFeed, self).__init__(request, _("Answers to: %s") % question.title, question.html, question.get_absolute_url())
81 self._question = question
82 self._include_comments = include_comments
85 if self._include_comments:
86 qs = self._question.all_children
88 qs = self._question.answers
90 return qs.filter_state(deleted=False).order_by('-added_at')[:30]
92 def item_title(self, item):
93 if item.node_type == "answer":
94 return _("Answer by %s") % item.author.username
96 return _("Comment by %(cauthor)s on %(pauthor)s's %(qora)s") % dict(
97 cauthor=item.author.username, pauthor=item.parent.author.username, qora=(item.parent.node_type == "answer" and _("answer") or _("question"))