##// END OF EJS Templates
repo-feed: fix path-based permissions condition on caching element....
marcink -
r2740:77867d16 stable
parent child Browse files
Show More
@@ -1,218 +1,220 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2017-2018 RhodeCode GmbH
3 # Copyright (C) 2017-2018 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 diff_processor, _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 diff_processor, parsed_diff, limited_diff = self._changes(commit)
92 diff_processor, parsed_diff, limited_diff = self._changes(commit)
93 filtered_parsed_diff, has_hidden_changes = self.path_filter.filter_patchset(parsed_diff)
93 filtered_parsed_diff, has_hidden_changes = self.path_filter.filter_patchset(parsed_diff)
94 return _renderer(
94 return _renderer(
95 'body',
95 'body',
96 commit=commit,
96 commit=commit,
97 parsed_diff=filtered_parsed_diff,
97 parsed_diff=filtered_parsed_diff,
98 limited_diff=limited_diff,
98 limited_diff=limited_diff,
99 feed_include_diff=self.feed_include_diff,
99 feed_include_diff=self.feed_include_diff,
100 diff_processor=diff_processor,
100 diff_processor=diff_processor,
101 has_hidden_changes=has_hidden_changes
101 has_hidden_changes=has_hidden_changes
102 )
102 )
103
103
104 def _set_timezone(self, date, tzinfo=pytz.utc):
104 def _set_timezone(self, date, tzinfo=pytz.utc):
105 if not getattr(date, "tzinfo", None):
105 if not getattr(date, "tzinfo", None):
106 date.replace(tzinfo=tzinfo)
106 date.replace(tzinfo=tzinfo)
107 return date
107 return date
108
108
109 def _get_commits(self):
109 def _get_commits(self):
110 return list(self.rhodecode_vcs_repo[-self.feed_items_per_page:])
110 return list(self.rhodecode_vcs_repo[-self.feed_items_per_page:])
111
111
112 def uid(self, repo_id, commit_id):
112 def uid(self, repo_id, commit_id):
113 return '{}:{}'.format(md5_safe(repo_id), md5_safe(commit_id))
113 return '{}:{}'.format(md5_safe(repo_id), md5_safe(commit_id))
114
114
115 @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED])
115 @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED])
116 @HasRepoPermissionAnyDecorator(
116 @HasRepoPermissionAnyDecorator(
117 'repository.read', 'repository.write', 'repository.admin')
117 'repository.read', 'repository.write', 'repository.admin')
118 @view_config(
118 @view_config(
119 route_name='atom_feed_home', request_method='GET',
119 route_name='atom_feed_home', request_method='GET',
120 renderer=None)
120 renderer=None)
121 def atom(self):
121 def atom(self):
122 """
122 """
123 Produce an atom-1.0 feed via feedgenerator module
123 Produce an atom-1.0 feed via feedgenerator module
124 """
124 """
125 self.load_default_context()
125 self.load_default_context()
126
126
127 def _generate_feed():
127 def _generate_feed():
128 feed = Atom1Feed(
128 feed = Atom1Feed(
129 title=self.title % self.db_repo_name,
129 title=self.title % self.db_repo_name,
130 link=h.route_url('repo_summary', repo_name=self.db_repo_name),
130 link=h.route_url('repo_summary', repo_name=self.db_repo_name),
131 description=self.description % self.db_repo_name,
131 description=self.description % self.db_repo_name,
132 language=self.language,
132 language=self.language,
133 ttl=self.ttl
133 ttl=self.ttl
134 )
134 )
135
135
136 for commit in reversed(self._get_commits()):
136 for commit in reversed(self._get_commits()):
137 date = self._set_timezone(commit.date)
137 date = self._set_timezone(commit.date)
138 feed.add_item(
138 feed.add_item(
139 unique_id=self.uid(self.db_repo.repo_id, commit.raw_id),
139 unique_id=self.uid(self.db_repo.repo_id, commit.raw_id),
140 title=self._get_title(commit),
140 title=self._get_title(commit),
141 author_name=commit.author,
141 author_name=commit.author,
142 description=self._get_description(commit),
142 description=self._get_description(commit),
143 link=h.route_url(
143 link=h.route_url(
144 'repo_commit', repo_name=self.db_repo_name,
144 'repo_commit', repo_name=self.db_repo_name,
145 commit_id=commit.raw_id),
145 commit_id=commit.raw_id),
146 pubdate=date,)
146 pubdate=date,)
147
147
148 return feed.mime_type, feed.writeString('utf-8')
148 return feed.mime_type, feed.writeString('utf-8')
149
149
150 @cache_region('long_term')
150 @cache_region('long_term')
151 def _generate_feed_and_cache(cache_key):
151 def _generate_feed_and_cache(cache_key):
152 return _generate_feed()
152 return _generate_feed()
153
153
154 if self.path_filter.is_enabled:
154 if self.path_filter.is_enabled:
155 mime_type, feed = _generate_feed()
156 else:
155 invalidator_context = CacheKey.repo_context_cache(
157 invalidator_context = CacheKey.repo_context_cache(
156 _generate_feed_and_cache, self.db_repo_name, CacheKey.CACHE_TYPE_ATOM)
158 _generate_feed_and_cache, self.db_repo_name,
159 CacheKey.CACHE_TYPE_ATOM)
157 with invalidator_context as context:
160 with invalidator_context as context:
158 context.invalidate()
161 context.invalidate()
159 mime_type, feed = context.compute()
162 mime_type, feed = context.compute()
160 else:
161 mime_type, feed = _generate_feed()
162
163
163 response = Response(feed)
164 response = Response(feed)
164 response.content_type = mime_type
165 response.content_type = mime_type
165 return response
166 return response
166
167
167 @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED])
168 @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED])
168 @HasRepoPermissionAnyDecorator(
169 @HasRepoPermissionAnyDecorator(
169 'repository.read', 'repository.write', 'repository.admin')
170 'repository.read', 'repository.write', 'repository.admin')
170 @view_config(
171 @view_config(
171 route_name='rss_feed_home', request_method='GET',
172 route_name='rss_feed_home', request_method='GET',
172 renderer=None)
173 renderer=None)
173 def rss(self):
174 def rss(self):
174 """
175 """
175 Produce an rss2 feed via feedgenerator module
176 Produce an rss2 feed via feedgenerator module
176 """
177 """
177 self.load_default_context()
178 self.load_default_context()
178
179
179 def _generate_feed():
180 def _generate_feed():
180 feed = Rss201rev2Feed(
181 feed = Rss201rev2Feed(
181 title=self.title % self.db_repo_name,
182 title=self.title % self.db_repo_name,
182 link=h.route_url('repo_summary', repo_name=self.db_repo_name),
183 link=h.route_url('repo_summary', repo_name=self.db_repo_name),
183 description=self.description % self.db_repo_name,
184 description=self.description % self.db_repo_name,
184 language=self.language,
185 language=self.language,
185 ttl=self.ttl
186 ttl=self.ttl
186 )
187 )
187
188
188 for commit in reversed(self._get_commits()):
189 for commit in reversed(self._get_commits()):
189 date = self._set_timezone(commit.date)
190 date = self._set_timezone(commit.date)
190 feed.add_item(
191 feed.add_item(
191 unique_id=self.uid(self.db_repo.repo_id, commit.raw_id),
192 unique_id=self.uid(self.db_repo.repo_id, commit.raw_id),
192 title=self._get_title(commit),
193 title=self._get_title(commit),
193 author_name=commit.author,
194 author_name=commit.author,
194 description=self._get_description(commit),
195 description=self._get_description(commit),
195 link=h.route_url(
196 link=h.route_url(
196 'repo_commit', repo_name=self.db_repo_name,
197 'repo_commit', repo_name=self.db_repo_name,
197 commit_id=commit.raw_id),
198 commit_id=commit.raw_id),
198 pubdate=date,)
199 pubdate=date,)
199
200
200 return feed.mime_type, feed.writeString('utf-8')
201 return feed.mime_type, feed.writeString('utf-8')
201
202
202 @cache_region('long_term')
203 @cache_region('long_term')
203 def _generate_feed_and_cache(cache_key):
204 def _generate_feed_and_cache(cache_key):
204 return _generate_feed()
205 return _generate_feed()
205
206
206 if self.path_filter.is_enabled:
207 if self.path_filter.is_enabled:
208 mime_type, feed = _generate_feed()
209 else:
207 invalidator_context = CacheKey.repo_context_cache(
210 invalidator_context = CacheKey.repo_context_cache(
208 _generate_feed_and_cache, self.db_repo_name, CacheKey.CACHE_TYPE_RSS)
211 _generate_feed_and_cache, self.db_repo_name,
212 CacheKey.CACHE_TYPE_RSS)
209
213
210 with invalidator_context as context:
214 with invalidator_context as context:
211 context.invalidate()
215 context.invalidate()
212 mime_type, feed = context.compute()
216 mime_type, feed = context.compute()
213 else:
214 mime_type, feed = _generate_feed()
215
217
216 response = Response(feed)
218 response = Response(feed)
217 response.content_type = mime_type
219 response.content_type = mime_type
218 return response
220 return response
General Comments 0
You need to be logged in to leave comments. Login now