]> git.openstreetmap.org Git - osqa.git/blob - forum/feed.py
Fixes OSQA 446 "Security - Multiple cross site scripting (XSS) vulnerabilities".
[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 from forum.utils.pagination import generate_uri
15
16 @decorate(add_domain, needs_origin=False)
17 def add_domain(domain, url):
18     return "%s%s" % (settings.APP_URL, url)
19
20 class BaseNodeFeed(Feed):
21     if old_version:
22         title_template = "feeds/rss_title.html"
23         description_template = "feeds/rss_description.html"
24
25     def __init__(self, request, title, description, url):
26         self._title = title
27         self._description = mark_safe(unicode(description))
28         self._url = url
29
30         if old_version:
31             super(BaseNodeFeed, self).__init__('', request)
32
33     def title(self):
34         return self._title
35
36     def link(self):
37         return self._url
38
39     def description(self):
40         return self._description
41
42     def item_title(self, item):
43         return item.title
44
45     def item_description(self, item):
46         return item.html
47
48     def item_link(self, item):
49         return item.leaf.get_absolute_url()
50
51     def item_author_name(self, item):
52         return item.author.username
53
54     def item_author_link(self, item):
55         return item.author.get_profile_url()
56
57     def item_pubdate(self, item):
58         return item.added_at
59
60     if old_version:
61         def __call__(self, request):
62             feedgen = self.get_feed('')
63             response = HttpResponse(mimetype=feedgen.mime_type)
64             feedgen.write(response, 'utf-8')
65             return response
66
67
68 class RssQuestionFeed(BaseNodeFeed):
69     def __init__(self, request, question_list, title, description):
70         url = request.path + "&" + generate_uri(request.GET, (_('page'), _('pagesize'), _('sort')))
71         super(RssQuestionFeed, self).__init__(request, title, description, url)
72
73         self._question_list = question_list
74
75     def item_categories(self, item):
76         return item.tagname_list()  
77
78     def items(self):
79        return self._question_list[:30]
80
81 class RssAnswerFeed(BaseNodeFeed):
82     if old_version:
83         title_template = "feeds/rss_answer_title.html"
84
85     def __init__(self, request, question, include_comments=False):
86         super(RssAnswerFeed, self).__init__(request, _("Answers to: %s") % question.title, question.html, question.get_absolute_url())
87         self._question = question
88         self._include_comments = include_comments
89
90     def items(self):
91         if self._include_comments:
92             qs = self._question.all_children
93         else:
94             qs = self._question.answers
95
96         return qs.filter_state(deleted=False).order_by('-added_at')[:30]
97
98     def item_title(self, item):
99         if item.node_type == "answer":
100             return _("Answer by %s") % item.author.username
101         else:
102             return _("Comment by %(cauthor)s on %(pauthor)s's %(qora)s") % dict(
103                 cauthor=item.author.username, pauthor=item.parent.author.username, qora=(item.parent.node_type == "answer" and _("answer") or _("question"))
104             )
105
106
107
108