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.encoding import smart_unicode
10 from django.utils.translation import ugettext as _
11 from django.utils.safestring import mark_safe
12 from models import Question
13 from forum import settings
14 from forum.modules import decorate
15 from forum.utils.pagination import generate_uri
17 @decorate(add_domain, needs_origin=False)
18 def add_domain(domain, url, *args, **kwargs):
19 return "%s%s" % (settings.APP_BASE_URL, url)
21 class BaseNodeFeed(Feed):
23 title_template = "feeds/rss_title.html"
24 description_template = "feeds/rss_description.html"
26 def __init__(self, request, title, description, url):
28 self._description = mark_safe(description.encode("utf-8"))
32 super(BaseNodeFeed, self).__init__('', request)
40 def description(self):
41 return self._description
43 def item_title(self, item):
46 def item_description(self, item):
49 def item_link(self, item):
50 return item.leaf.get_absolute_url()
52 def item_author_name(self, item):
53 return item.author.username
55 def item_author_link(self, item):
56 return item.author.get_profile_url()
58 def item_pubdate(self, item):
62 def __call__(self, request):
63 feedgen = self.get_feed('')
64 response = HttpResponse(mimetype=feedgen.mime_type)
65 feedgen.write(response, 'utf-8')
69 class RssQuestionFeed(BaseNodeFeed):
70 def __init__(self, request, question_list, title, description):
71 url = request.path + "?" + generate_uri(request.GET, (_('page'), _('pagesize'), _('sort')))
72 super(RssQuestionFeed, self).__init__(request, title, description, url)
74 self._question_list = question_list
76 def item_categories(self, item):
77 return item.tagname_list()
80 return self._question_list[:30]
82 class RssAnswerFeed(BaseNodeFeed):
84 title_template = "feeds/rss_answer_title.html"
86 def __init__(self, request, question, include_comments=False):
87 super(RssAnswerFeed, self).__init__(
88 request, _("Answers to: %s") % smart_unicode(question.title),
90 question.get_absolute_url()
92 self._question = question
93 self._include_comments = include_comments
96 if self._include_comments:
97 qs = self._question.all_children
99 qs = self._question.answers
101 return qs.filter_state(deleted=False).order_by('-added_at')[:30]
103 def item_title(self, item):
104 if item.node_type == "answer":
105 return _("Answer by %s") % smart_unicode(item.author.username)
107 return _("Comment by %(cauthor)s on %(pauthor)s's %(qora)s") % dict(
108 cauthor=smart_unicode(item.author.username),
109 pauthor=smart_unicode(item.parent.author.username),
110 qora=(item.parent.node_type == "answer" and _("answer") or _("question"))