]> git.openstreetmap.org Git - osqa.git/blob - forum/feed.py
use latest jQuery version
[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[:30]
83
84 class RssAnswerFeed(BaseNodeFeed):
85     if old_version:
86         title_template = "feeds/rss_answer_title.html"
87
88     def __init__(self, request, question, include_comments=False):
89         super(RssAnswerFeed, self).__init__(
90             request, _("Answers to: %s") % smart_unicode(question.title),
91             question.html,
92             question.get_absolute_url()
93         )
94         self._question = question
95         self._include_comments = include_comments
96
97     def items(self):
98         if self._include_comments:
99             qs = self._question.all_children
100         else:
101             qs = self._question.answers
102
103         return qs.filter_state(deleted=False).order_by('-added_at')[:30]
104
105     def item_title(self, item):
106         if item.node_type == "answer":
107             return _("Answer by %s") % smart_unicode(item.author.username)
108         else:
109             return _("Comment by %(cauthor)s on %(pauthor)s's %(qora)s") % dict(
110                 cauthor=smart_unicode(item.author.username),
111                 pauthor=smart_unicode(item.parent.author.username),
112                 qora=(item.parent.node_type == "answer" and _("answer") or _("question"))
113             )