##// END OF EJS Templates
feed: fixed problem with 500 errors on rendering atom feed with diffs.
marcink -
r2475:9b419253 default
parent child Browse files
Show More
@@ -1,204 +1,205 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2017-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import pytz
22 22 import logging
23 23
24 24 from beaker.cache import cache_region
25 25 from pyramid.view import view_config
26 26 from pyramid.response import Response
27 27 from webhelpers.feedgenerator import Rss201rev2Feed, Atom1Feed
28 28
29 29 from rhodecode.apps._base import RepoAppView
30 30 from rhodecode.lib import audit_logger
31 31 from rhodecode.lib import helpers as h
32 32 from rhodecode.lib.auth import (
33 33 LoginRequired, HasRepoPermissionAnyDecorator)
34 34 from rhodecode.lib.diffs import DiffProcessor, LimitedDiffContainer
35 35 from rhodecode.lib.utils2 import str2bool, safe_int, md5_safe
36 36 from rhodecode.model.db import UserApiKeys, CacheKey
37 37
38 38 log = logging.getLogger(__name__)
39 39
40 40
41 41 class RepoFeedView(RepoAppView):
42 42 def load_default_context(self):
43 43 c = self._get_local_tmpl_context()
44 44
45 45
46 46 self._load_defaults()
47 47 return c
48 48
49 49 def _get_config(self):
50 50 import rhodecode
51 51 config = rhodecode.CONFIG
52 52
53 53 return {
54 54 'language': 'en-us',
55 55 'feed_ttl': '5', # TTL of feed,
56 56 'feed_include_diff':
57 57 str2bool(config.get('rss_include_diff', False)),
58 58 'feed_items_per_page':
59 59 safe_int(config.get('rss_items_per_page', 20)),
60 60 'feed_diff_limit':
61 61 # we need to protect from parsing huge diffs here other way
62 62 # we can kill the server
63 63 safe_int(config.get('rss_cut_off_limit', 32 * 1024)),
64 64 }
65 65
66 66 def _load_defaults(self):
67 67 _ = self.request.translate
68 68 config = self._get_config()
69 69 # common values for feeds
70 70 self.description = _('Changes on %s repository')
71 71 self.title = self.title = _('%s %s feed') % (self.db_repo_name, '%s')
72 72 self.language = config["language"]
73 73 self.ttl = config["feed_ttl"]
74 74 self.feed_include_diff = config['feed_include_diff']
75 75 self.feed_diff_limit = config['feed_diff_limit']
76 76 self.feed_items_per_page = config['feed_items_per_page']
77 77
78 78 def _changes(self, commit):
79 79 diff_processor = DiffProcessor(
80 80 commit.diff(), diff_limit=self.feed_diff_limit)
81 81 _parsed = diff_processor.prepare(inline_diff=False)
82 82 limited_diff = isinstance(_parsed, LimitedDiffContainer)
83 83
84 return _parsed, limited_diff
84 return diff_processor, _parsed, limited_diff
85 85
86 86 def _get_title(self, commit):
87 87 return h.shorter(commit.message, 160)
88 88
89 89 def _get_description(self, commit):
90 90 _renderer = self.request.get_partial_renderer(
91 91 'rhodecode:templates/feed/atom_feed_entry.mako')
92 parsed_diff, limited_diff = self._changes(commit)
92 diff_processor, parsed_diff, limited_diff = self._changes(commit)
93 93 return _renderer(
94 94 'body',
95 95 commit=commit,
96 96 parsed_diff=parsed_diff,
97 97 limited_diff=limited_diff,
98 98 feed_include_diff=self.feed_include_diff,
99 diff_processor=diff_processor,
99 100 )
100 101
101 102 def _set_timezone(self, date, tzinfo=pytz.utc):
102 103 if not getattr(date, "tzinfo", None):
103 104 date.replace(tzinfo=tzinfo)
104 105 return date
105 106
106 107 def _get_commits(self):
107 108 return list(self.rhodecode_vcs_repo[-self.feed_items_per_page:])
108 109
109 110 def uid(self, repo_id, commit_id):
110 111 return '{}:{}'.format(md5_safe(repo_id), md5_safe(commit_id))
111 112
112 113 @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED])
113 114 @HasRepoPermissionAnyDecorator(
114 115 'repository.read', 'repository.write', 'repository.admin')
115 116 @view_config(
116 117 route_name='atom_feed_home', request_method='GET',
117 118 renderer=None)
118 119 def atom(self):
119 120 """
120 121 Produce an atom-1.0 feed via feedgenerator module
121 122 """
122 123 self.load_default_context()
123 124
124 125 @cache_region('long_term')
125 126 def _generate_feed(cache_key):
126 127 feed = Atom1Feed(
127 128 title=self.title % self.db_repo_name,
128 129 link=h.route_url('repo_summary', repo_name=self.db_repo_name),
129 130 description=self.description % self.db_repo_name,
130 131 language=self.language,
131 132 ttl=self.ttl
132 133 )
133 134
134 135 for commit in reversed(self._get_commits()):
135 136 date = self._set_timezone(commit.date)
136 137 feed.add_item(
137 138 unique_id=self.uid(self.db_repo.repo_id, commit.raw_id),
138 139 title=self._get_title(commit),
139 140 author_name=commit.author,
140 141 description=self._get_description(commit),
141 142 link=h.route_url(
142 143 'repo_commit', repo_name=self.db_repo_name,
143 144 commit_id=commit.raw_id),
144 145 pubdate=date,)
145 146
146 147 return feed.mime_type, feed.writeString('utf-8')
147 148
148 149 invalidator_context = CacheKey.repo_context_cache(
149 150 _generate_feed, self.db_repo_name, CacheKey.CACHE_TYPE_ATOM)
150 151
151 152 with invalidator_context as context:
152 153 context.invalidate()
153 154 mime_type, feed = context.compute()
154 155
155 156 response = Response(feed)
156 157 response.content_type = mime_type
157 158 return response
158 159
159 160 @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED])
160 161 @HasRepoPermissionAnyDecorator(
161 162 'repository.read', 'repository.write', 'repository.admin')
162 163 @view_config(
163 164 route_name='rss_feed_home', request_method='GET',
164 165 renderer=None)
165 166 def rss(self):
166 167 """
167 168 Produce an rss2 feed via feedgenerator module
168 169 """
169 170 self.load_default_context()
170 171
171 172 @cache_region('long_term')
172 173 def _generate_feed(cache_key):
173 174 feed = Rss201rev2Feed(
174 175 title=self.title % self.db_repo_name,
175 176 link=h.route_url('repo_summary', repo_name=self.db_repo_name),
176 177 description=self.description % self.db_repo_name,
177 178 language=self.language,
178 179 ttl=self.ttl
179 180 )
180 181
181 182 for commit in reversed(self._get_commits()):
182 183 date = self._set_timezone(commit.date)
183 184 feed.add_item(
184 185 unique_id=self.uid(self.db_repo.repo_id, commit.raw_id),
185 186 title=self._get_title(commit),
186 187 author_name=commit.author,
187 188 description=self._get_description(commit),
188 189 link=h.route_url(
189 190 'repo_commit', repo_name=self.db_repo_name,
190 191 commit_id=commit.raw_id),
191 192 pubdate=date,)
192 193
193 194 return feed.mime_type, feed.writeString('utf-8')
194 195
195 196 invalidator_context = CacheKey.repo_context_cache(
196 197 _generate_feed, self.db_repo_name, CacheKey.CACHE_TYPE_RSS)
197 198
198 199 with invalidator_context as context:
199 200 context.invalidate()
200 201 mime_type, feed = context.compute()
201 202
202 203 response = Response(feed)
203 204 response.content_type = mime_type
204 205 return response
General Comments 0
You need to be logged in to leave comments. Login now