]> git.openstreetmap.org Git - osqa.git/blob - forum/feed.py
Merge commit '9bea91a' into merge_small_prs
[osqa.git] / forum / feed.py
1 # -*- coding: utf-8 -*-
2
3 try:
4     from django.contrib.syndication.views import Feed, FeedDoesNotExist, add_domain
5     old_version = False
6 except:
7     from django.contrib.syndication.feeds import Feed, FeedDoesNotExist, add_domain
8     old_version = True
9
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
18
19 @decorate(add_domain, needs_origin=False)
20 def add_domain(domain, url, *args, **kwargs):
21     return "%s%s" % (settings.APP_BASE_URL, url)
22
23 class BaseNodeFeed(Feed):
24     if old_version:
25         title_template = "feeds/rss_title.html"
26         description_template = "feeds/rss_description.html"
27
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))
31         self._url = url
32
33         if old_version:
34             super(BaseNodeFeed, self).__init__('', request)
35
36     def title(self):
37         return u"%s" % smart_unicode(self._title)
38
39     def link(self):
40         return self._url
41
42     def description(self):
43         return u"%s" % smart_unicode(self._description)
44
45     def item_title(self, item):
46         return u"%s" % smart_unicode(item.title)
47
48     def item_description(self, item):
49         return u"%s" % smart_unicode(item.html)
50
51     def item_link(self, item):
52         return item.leaf.get_absolute_url()
53
54     def item_author_name(self, item):
55         return u"%s" % smart_unicode(item.author.username)
56
57     def item_author_link(self, item):
58         return item.author.get_profile_url()
59
60     def item_pubdate(self, item):
61         return item.added_at
62
63     if old_version:
64         def __call__(self, request):
65             feedgen = self.get_feed('')
66             response = HttpResponse(mimetype=feedgen.mime_type)
67             feedgen.write(response, 'utf-8')
68             return response
69
70
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)
75
76         self._question_list = question_list
77
78     def item_categories(self, item):
79         return item.tagname_list()  
80
81     def _items(self):
82        return self._question_list
83
84     def items(self):
85         return self._items()[:30]
86
87 class RssAnswerFeed(BaseNodeFeed):
88     if old_version:
89         title_template = "feeds/rss_answer_title.html"
90
91     def __init__(self, request, question, include_comments=False):
92         super(RssAnswerFeed, self).__init__(
93             request, _("Answers to: %s") % smart_unicode(question.title),
94             question.html,
95             question.get_absolute_url()
96         )
97         self._question = question
98         self._include_comments = include_comments
99
100     def _items(self):
101         if self._include_comments:
102             qs = self._question.all_children
103         else:
104             qs = self._question.answers
105
106         return qs.filter_state(deleted=False).order_by('-added_at')
107
108     def items(self):
109         return self._items()[:30]
110
111     def item_title(self, item):
112         if item.node_type == "answer":
113             return _("Answer by %s") % smart_unicode(item.author.username)
114         else:
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"))
119             )