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