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