2 from django.contrib.syndication.views import Feed, FeedDoesNotExist, add_domain
5 from django.contrib.syndication.feeds import Feed, FeedDoesNotExist, add_domain
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
13 from forum.modules import decorate
15 @decorate(add_domain, needs_origin=False)
16 def add_domain(domain, url):
17 return "%s%s" % (settings.APP_URL, url)
19 class BaseNodeFeed(Feed):
21 title_template = "feeds/rss_title.html"
22 description_template = "feeds/rss_description.html"
24 def __init__(self, request, title, description, url):
26 self._description = mark_safe(unicode(description))
30 super(BaseNodeFeed, self).__init__('', request)
38 def description(self):
39 return self._description
41 def item_title(self, item):
44 def item_description(self, item):
47 def item_link(self, item):
48 return item.leaf.get_absolute_url()
50 def item_author_name(self, item):
51 return item.author.username
53 def item_author_link(self, item):
54 return item.author.get_profile_url()
56 def item_pubdate(self, item):
60 def __call__(self, request):
61 feedgen = self.get_feed('')
62 response = HttpResponse(mimetype=feedgen.mime_type)
63 feedgen.write(response, 'utf-8')
67 class RssQuestionFeed(BaseNodeFeed):
68 def __init__(self, request, question_list, title, description):
69 url = request.path + "&" + "&".join(["%s=%s" % (k, v) for k, v in request.GET.items() if not k in (_('page'), _('pagesize'), _('sort'))])
70 super(RssQuestionFeed, self).__init__(request, title, description, url)
72 self._question_list = question_list
74 def item_categories(self, item):
75 return item.tagname_list()
78 return self._question_list[:30]
80 class RssAnswerFeed(BaseNodeFeed):
82 title_template = "feeds/rss_answer_title.html"
84 def __init__(self, request, question, include_comments=False):
85 super(RssAnswerFeed, self).__init__(request, _("Answers to: %s") % question.title, question.html, question.get_absolute_url())
86 self._question = question
87 self._include_comments = include_comments
90 if self._include_comments:
91 qs = self._question.all_children
93 qs = self._question.answers
95 return qs.filter_state(deleted=False).order_by('-added_at')[:30]
97 def item_title(self, item):
98 if item.node_type == "answer":
99 return _("Answer by %s") % item.author.username
101 return _("Comment by %(cauthor)s on %(pauthor)s's %(qora)s") % dict(
102 cauthor=item.author.username, pauthor=item.parent.author.username, qora=(item.parent.node_type == "answer" and _("answer") or _("question"))