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
14 from forum.utils.pagination import generate_uri
16 @decorate(add_domain, needs_origin=False)
17 def add_domain(domain, url):
18 return "%s%s" % (settings.APP_URL, url)
20 class BaseNodeFeed(Feed):
22 title_template = "feeds/rss_title.html"
23 description_template = "feeds/rss_description.html"
25 def __init__(self, request, title, description, url):
27 self._description = mark_safe(unicode(description))
31 super(BaseNodeFeed, self).__init__('', request)
39 def description(self):
40 return self._description
42 def item_title(self, item):
45 def item_description(self, item):
48 def item_link(self, item):
49 return item.leaf.get_absolute_url()
51 def item_author_name(self, item):
52 return item.author.username
54 def item_author_link(self, item):
55 return item.author.get_profile_url()
57 def item_pubdate(self, item):
61 def __call__(self, request):
62 feedgen = self.get_feed('')
63 response = HttpResponse(mimetype=feedgen.mime_type)
64 feedgen.write(response, 'utf-8')
68 class RssQuestionFeed(BaseNodeFeed):
69 def __init__(self, request, question_list, title, description):
70 url = request.path + "&" + generate_uri(request.GET, (_('page'), _('pagesize'), _('sort')))
71 super(RssQuestionFeed, self).__init__(request, title, description, url)
73 self._question_list = question_list
75 def item_categories(self, item):
76 return item.tagname_list()
79 return self._question_list[:30]
81 class RssAnswerFeed(BaseNodeFeed):
83 title_template = "feeds/rss_answer_title.html"
85 def __init__(self, request, question, include_comments=False):
86 super(RssAnswerFeed, self).__init__(request, _("Answers to: %s") % question.title, question.html, question.get_absolute_url())
87 self._question = question
88 self._include_comments = include_comments
91 if self._include_comments:
92 qs = self._question.all_children
94 qs = self._question.answers
96 return qs.filter_state(deleted=False).order_by('-added_at')[:30]
98 def item_title(self, item):
99 if item.node_type == "answer":
100 return _("Answer by %s") % item.author.username
102 return _("Comment by %(cauthor)s on %(pauthor)s's %(qora)s") % dict(
103 cauthor=item.author.username, pauthor=item.parent.author.username, qora=(item.parent.node_type == "answer" and _("answer") or _("question"))