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