]> git.openstreetmap.org Git - osqa.git/blob - forum/feed.py
OSQA-776, limit the cache key length in the infer_cache_key method, adding a setting...
[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.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
16
17 @decorate(add_domain, needs_origin=False)
18 def add_domain(domain, url, *args, **kwargs):
19     return "%s%s" % (settings.APP_BASE_URL, url)
20
21 class BaseNodeFeed(Feed):
22     if old_version:
23         title_template = "feeds/rss_title.html"
24         description_template = "feeds/rss_description.html"
25
26     def __init__(self, request, title, description, url):
27         self._title = title
28         self._description = mark_safe(unicode(description))
29         self._url = url
30
31         if old_version:
32             super(BaseNodeFeed, self).__init__('', request)
33
34     def title(self):
35         return self._title
36
37     def link(self):
38         return self._url
39
40     def description(self):
41         return self._description
42
43     def item_title(self, item):
44         return item.title
45
46     def item_description(self, item):
47         return item.html
48
49     def item_link(self, item):
50         return item.leaf.get_absolute_url()
51
52     def item_author_name(self, item):
53         return item.author.username
54
55     def item_author_link(self, item):
56         return item.author.get_profile_url()
57
58     def item_pubdate(self, item):
59         return item.added_at
60
61     if old_version:
62         def __call__(self, request):
63             feedgen = self.get_feed('')
64             response = HttpResponse(mimetype=feedgen.mime_type)
65             feedgen.write(response, 'utf-8')
66             return response
67
68
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)
73
74         self._question_list = question_list
75
76     def item_categories(self, item):
77         return item.tagname_list()  
78
79     def items(self):
80        return self._question_list[:30]
81
82 class RssAnswerFeed(BaseNodeFeed):
83     if old_version:
84         title_template = "feeds/rss_answer_title.html"
85
86     def __init__(self, request, question, include_comments=False):
87         super(RssAnswerFeed, self).__init__(
88             request, _("Answers to: %s") % smart_unicode(question.title),
89             question.html,
90             question.get_absolute_url()
91         )
92         self._question = question
93         self._include_comments = include_comments
94
95     def items(self):
96         if self._include_comments:
97             qs = self._question.all_children
98         else:
99             qs = self._question.answers
100
101         return qs.filter_state(deleted=False).order_by('-added_at')[:30]
102
103     def item_title(self, item):
104         if item.node_type == "answer":
105             return _("Answer by %s") % smart_unicode(item.author.username)
106         else:
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"))
111             )