]> git.openstreetmap.org Git - osqa.git/blob - forum/feed.py
Adds feeds for answers or comments to questions.
[osqa.git] / forum / feed.py
1 try:
2     from django.contrib.syndication.views import Feed, FeedDoesNotExist
3     old_version = False
4 except:
5     from django.contrib.syndication.feeds import Feed, FeedDoesNotExist
6     old_version = True
7
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
14 class BaseNodeFeed(Feed):
15     if old_version:
16         title_template = "feeds/rss_title.html"
17         description_template = "feeds/rss_description.html"
18
19     def __init__(self, request, title, description, url):
20         self._title = title
21         self._description = mark_safe(description)
22         self._url = url
23
24         if old_version:
25             super(BaseNodeFeed, self).__init__('', request)
26
27     def title(self):
28         return self._title
29
30     def link(self):
31         return self._url
32
33     def description(self):
34         return self._description
35
36     def item_title(self, item):
37         return item.title
38
39     def item_description(self, item):
40         return item.html
41
42     def item_link(self, item):
43         return item.leaf.get_absolute_url()
44
45     def item_author_name(self, item):
46         return item.author.username
47
48     def item_author_link(self, item):
49         return item.author.get_profile_url()
50
51     def item_pubdate(self, item):
52         return item.added_at
53
54     if old_version:
55         def __call__(self, request):
56             feedgen = self.get_feed('')
57             response = HttpResponse(mimetype=feedgen.mime_type)
58             feedgen.write(response, 'utf-8')
59             return response
60
61
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)
66
67         self._question_list = question_list
68
69     def item_categories(self, item):
70         return item.tagname_list()  
71
72     def items(self):
73        return self._question_list[:30]
74
75 class RssAnswerFeed(BaseNodeFeed):
76     if old_version:
77         title_template = "feeds/rss_answer_title.html"
78
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
83
84     def items(self):
85         if self._include_comments:
86             qs = self._question.all_children
87         else:
88             qs = self._question.answers
89
90         return qs.filter_state(deleted=False).order_by('-added_at')[:30]
91
92     def item_title(self, item):
93         if item.node_type == "answer":
94             return _("Answer by %s") % item.author.username
95         else:
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"))
98             )
99
100
101
102