1 # -*- coding: utf-8 -*-
4 from django.contrib.syndication.views import Feed, FeedDoesNotExist, add_domain
7 from django.contrib.syndication.feeds import Feed, FeedDoesNotExist, add_domain
10 from django.http import HttpResponse
11 from django.utils.encoding import smart_unicode
12 from django.utils.translation import ugettext as _
13 from django.utils.safestring import mark_safe
14 from models import Question
15 from forum import settings
16 from forum.modules import decorate
17 from forum.utils.pagination import generate_uri
19 @decorate(add_domain, needs_origin=False)
20 def add_domain(domain, url, *args, **kwargs):
21 return "%s%s" % (settings.APP_BASE_URL, url)
23 class BaseNodeFeed(Feed):
25 title_template = "feeds/rss_title.html"
26 description_template = "feeds/rss_description.html"
28 def __init__(self, request, title, description, url):
29 self._title = u"%s" % smart_unicode(title)
30 self._description = mark_safe(u"%s" % smart_unicode(description))
34 super(BaseNodeFeed, self).__init__('', request)
37 return u"%s" % smart_unicode(self._title)
42 def description(self):
43 return u"%s" % smart_unicode(self._description)
45 def item_title(self, item):
46 return u"%s" % smart_unicode(item.title)
48 def item_description(self, item):
49 return u"%s" % smart_unicode(item.html)
51 def item_link(self, item):
52 return item.leaf.get_absolute_url()
54 def item_author_name(self, item):
55 return u"%s" % smart_unicode(item.author.username)
57 def item_author_link(self, item):
58 return item.author.get_profile_url()
60 def item_pubdate(self, item):
64 def __call__(self, request):
65 feedgen = self.get_feed('')
66 response = HttpResponse(content_type=feedgen.mime_type)
67 feedgen.write(response, 'utf-8')
71 class RssQuestionFeed(BaseNodeFeed):
72 def __init__(self, request, question_list, title, description):
73 url = request.path + "?" + generate_uri(request.GET, (_('page'), _('pagesize'), _('sort')))
74 super(RssQuestionFeed, self).__init__(request, title, description, url)
76 self._question_list = question_list
78 def item_categories(self, item):
79 return item.tagname_list()
82 return self._question_list
85 return self._items()[:30]
87 class RssAnswerFeed(BaseNodeFeed):
89 title_template = "feeds/rss_answer_title.html"
91 def __init__(self, request, question, include_comments=False):
92 super(RssAnswerFeed, self).__init__(
93 request, _("Answers to: %s") % smart_unicode(question.title),
95 question.get_absolute_url()
97 self._question = question
98 self._include_comments = include_comments
101 if self._include_comments:
102 qs = self._question.all_children
104 qs = self._question.answers
106 return qs.filter_state(deleted=False).order_by('-added_at')
109 return self._items()[:30]
111 def item_title(self, item):
112 if item.node_type == "answer":
113 return _("Answer by %s") % smart_unicode(item.author.username)
115 return _("Comment by %(cauthor)s on %(pauthor)s's %(qora)s") % dict(
116 cauthor=smart_unicode(item.author.username),
117 pauthor=smart_unicode(item.parent.author.username),
118 qora=(item.parent.node_type == "answer" and _("answer") or _("question"))