Show More
@@ -1,242 +1,205 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2017-2019 RhodeCode GmbH |
|
3 | # Copyright (C) 2017-2019 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 | import pytz |
|
20 | import pytz | |
21 | import logging |
|
21 | import logging | |
22 |
|
22 | |||
23 | from pyramid.view import view_config |
|
23 | from pyramid.view import view_config | |
24 | from pyramid.response import Response |
|
24 | from pyramid.response import Response | |
25 | from webhelpers.feedgenerator import Rss201rev2Feed, Atom1Feed |
|
25 | from webhelpers.feedgenerator import Rss201rev2Feed, Atom1Feed | |
26 |
|
26 | |||
27 | from rhodecode.apps._base import RepoAppView |
|
27 | from rhodecode.apps._base import RepoAppView | |
28 | from rhodecode.lib import audit_logger |
|
28 | from rhodecode.lib import audit_logger | |
29 | from rhodecode.lib import rc_cache |
|
29 | from rhodecode.lib import rc_cache | |
30 | from rhodecode.lib import helpers as h |
|
30 | from rhodecode.lib import helpers as h | |
31 | from rhodecode.lib.auth import ( |
|
31 | from rhodecode.lib.auth import ( | |
32 | LoginRequired, HasRepoPermissionAnyDecorator) |
|
32 | LoginRequired, HasRepoPermissionAnyDecorator) | |
33 | from rhodecode.lib.diffs import DiffProcessor, LimitedDiffContainer |
|
33 | from rhodecode.lib.diffs import DiffProcessor, LimitedDiffContainer | |
34 | from rhodecode.lib.utils2 import str2bool, safe_int, md5_safe |
|
34 | from rhodecode.lib.utils2 import str2bool, safe_int, md5_safe | |
35 | from rhodecode.model.db import UserApiKeys, CacheKey |
|
35 | from rhodecode.model.db import UserApiKeys, CacheKey | |
36 |
|
36 | |||
37 | log = logging.getLogger(__name__) |
|
37 | log = logging.getLogger(__name__) | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | class RepoFeedView(RepoAppView): |
|
40 | class RepoFeedView(RepoAppView): | |
41 | def load_default_context(self): |
|
41 | def load_default_context(self): | |
42 | c = self._get_local_tmpl_context() |
|
42 | c = self._get_local_tmpl_context() | |
43 | self._load_defaults() |
|
43 | self._load_defaults() | |
44 | return c |
|
44 | return c | |
45 |
|
45 | |||
46 | def _get_config(self): |
|
46 | def _get_config(self): | |
47 | import rhodecode |
|
47 | import rhodecode | |
48 | config = rhodecode.CONFIG |
|
48 | config = rhodecode.CONFIG | |
49 |
|
49 | |||
50 | return { |
|
50 | return { | |
51 | 'language': 'en-us', |
|
51 | 'language': 'en-us', | |
52 | 'feed_ttl': '5', # TTL of feed, |
|
52 | 'feed_ttl': '5', # TTL of feed, | |
53 | 'feed_include_diff': |
|
53 | 'feed_include_diff': | |
54 | str2bool(config.get('rss_include_diff', False)), |
|
54 | str2bool(config.get('rss_include_diff', False)), | |
55 | 'feed_items_per_page': |
|
55 | 'feed_items_per_page': | |
56 | safe_int(config.get('rss_items_per_page', 20)), |
|
56 | safe_int(config.get('rss_items_per_page', 20)), | |
57 | 'feed_diff_limit': |
|
57 | 'feed_diff_limit': | |
58 | # we need to protect from parsing huge diffs here other way |
|
58 | # we need to protect from parsing huge diffs here other way | |
59 | # we can kill the server |
|
59 | # we can kill the server | |
60 | safe_int(config.get('rss_cut_off_limit', 32 * 1024)), |
|
60 | safe_int(config.get('rss_cut_off_limit', 32 * 1024)), | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | def _load_defaults(self): |
|
63 | def _load_defaults(self): | |
64 | _ = self.request.translate |
|
64 | _ = self.request.translate | |
65 | config = self._get_config() |
|
65 | config = self._get_config() | |
66 | # common values for feeds |
|
66 | # common values for feeds | |
67 | self.description = _('Changes on %s repository') |
|
67 | self.description = _('Changes on %s repository') | |
68 | self.title = self.title = _('%s %s feed') % (self.db_repo_name, '%s') |
|
68 | self.title = self.title = _('%s %s feed') % (self.db_repo_name, '%s') | |
69 | self.language = config["language"] |
|
69 | self.language = config["language"] | |
70 | self.ttl = config["feed_ttl"] |
|
70 | self.ttl = config["feed_ttl"] | |
71 | self.feed_include_diff = config['feed_include_diff'] |
|
71 | self.feed_include_diff = config['feed_include_diff'] | |
72 | self.feed_diff_limit = config['feed_diff_limit'] |
|
72 | self.feed_diff_limit = config['feed_diff_limit'] | |
73 | self.feed_items_per_page = config['feed_items_per_page'] |
|
73 | self.feed_items_per_page = config['feed_items_per_page'] | |
74 |
|
74 | |||
75 | def _changes(self, commit): |
|
75 | def _changes(self, commit): | |
76 | diff_processor = DiffProcessor( |
|
76 | diff_processor = DiffProcessor( | |
77 | commit.diff(), diff_limit=self.feed_diff_limit) |
|
77 | commit.diff(), diff_limit=self.feed_diff_limit) | |
78 | _parsed = diff_processor.prepare(inline_diff=False) |
|
78 | _parsed = diff_processor.prepare(inline_diff=False) | |
79 | limited_diff = isinstance(_parsed, LimitedDiffContainer) |
|
79 | limited_diff = isinstance(_parsed, LimitedDiffContainer) | |
80 |
|
80 | |||
81 | return diff_processor, _parsed, limited_diff |
|
81 | return diff_processor, _parsed, limited_diff | |
82 |
|
82 | |||
83 | def _get_title(self, commit): |
|
83 | def _get_title(self, commit): | |
84 | return h.shorter(commit.message, 160) |
|
84 | return h.shorter(commit.message, 160) | |
85 |
|
85 | |||
86 | def _get_description(self, commit): |
|
86 | def _get_description(self, commit): | |
87 | _renderer = self.request.get_partial_renderer( |
|
87 | _renderer = self.request.get_partial_renderer( | |
88 | 'rhodecode:templates/feed/atom_feed_entry.mako') |
|
88 | 'rhodecode:templates/feed/atom_feed_entry.mako') | |
89 | diff_processor, parsed_diff, limited_diff = self._changes(commit) |
|
89 | diff_processor, parsed_diff, limited_diff = self._changes(commit) | |
90 | filtered_parsed_diff, has_hidden_changes = self.path_filter.filter_patchset(parsed_diff) |
|
90 | filtered_parsed_diff, has_hidden_changes = self.path_filter.filter_patchset(parsed_diff) | |
91 | return _renderer( |
|
91 | return _renderer( | |
92 | 'body', |
|
92 | 'body', | |
93 | commit=commit, |
|
93 | commit=commit, | |
94 | parsed_diff=filtered_parsed_diff, |
|
94 | parsed_diff=filtered_parsed_diff, | |
95 | limited_diff=limited_diff, |
|
95 | limited_diff=limited_diff, | |
96 | feed_include_diff=self.feed_include_diff, |
|
96 | feed_include_diff=self.feed_include_diff, | |
97 | diff_processor=diff_processor, |
|
97 | diff_processor=diff_processor, | |
98 | has_hidden_changes=has_hidden_changes |
|
98 | has_hidden_changes=has_hidden_changes | |
99 | ) |
|
99 | ) | |
100 |
|
100 | |||
101 | def _set_timezone(self, date, tzinfo=pytz.utc): |
|
101 | def _set_timezone(self, date, tzinfo=pytz.utc): | |
102 | if not getattr(date, "tzinfo", None): |
|
102 | if not getattr(date, "tzinfo", None): | |
103 | date.replace(tzinfo=tzinfo) |
|
103 | date.replace(tzinfo=tzinfo) | |
104 | return date |
|
104 | return date | |
105 |
|
105 | |||
106 | def _get_commits(self): |
|
106 | def _get_commits(self): | |
107 | return list(self.rhodecode_vcs_repo[-self.feed_items_per_page:]) |
|
107 | return list(self.rhodecode_vcs_repo[-self.feed_items_per_page:]) | |
108 |
|
108 | |||
109 | def uid(self, repo_id, commit_id): |
|
109 | def uid(self, repo_id, commit_id): | |
110 | return '{}:{}'.format(md5_safe(repo_id), md5_safe(commit_id)) |
|
110 | return '{}:{}'.format(md5_safe(repo_id), md5_safe(commit_id)) | |
111 |
|
111 | |||
112 | @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED]) |
|
112 | @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED]) | |
113 | @HasRepoPermissionAnyDecorator( |
|
113 | @HasRepoPermissionAnyDecorator( | |
114 | 'repository.read', 'repository.write', 'repository.admin') |
|
114 | 'repository.read', 'repository.write', 'repository.admin') | |
115 | @view_config( |
|
115 | @view_config(route_name='atom_feed_home', request_method='GET', renderer=None) | |
116 |
|
|
116 | @view_config(route_name='atom_feed_home_old', request_method='GET', renderer=None) | |
117 | renderer=None) |
|
|||
118 | @view_config( |
|
|||
119 | route_name='atom_feed_home_old', request_method='GET', |
|
|||
120 | renderer=None) |
|
|||
121 | def atom(self): |
|
117 | def atom(self): | |
122 | """ |
|
118 | """ | |
123 | Produce an atom-1.0 feed via feedgenerator module |
|
119 | Produce an atom-1.0 feed via feedgenerator module | |
124 | """ |
|
120 | """ | |
125 | self.load_default_context() |
|
121 | self.load_default_context() | |
126 |
|
122 | |||
127 |
cache_namespace_uid = 'cache_repo_ |
|
123 | cache_namespace_uid = 'cache_repo_feed.{}'.format(self.db_repo.repo_id) | |
128 | self.db_repo.repo_id, CacheKey.CACHE_TYPE_FEED) |
|
|||
129 | invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format( |
|
|||
130 | repo_id=self.db_repo.repo_id) |
|
|||
131 |
|
||||
132 | region = rc_cache.get_or_create_region('cache_repo_longterm', |
|
|||
133 | cache_namespace_uid) |
|
|||
134 |
|
||||
135 | condition = not self.path_filter.is_enabled |
|
124 | condition = not self.path_filter.is_enabled | |
|
125 | region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid) | |||
136 |
|
126 | |||
137 | @region.conditional_cache_on_arguments(namespace=cache_namespace_uid, |
|
127 | @region.conditional_cache_on_arguments(namespace=cache_namespace_uid, | |
138 | condition=condition) |
|
128 | condition=condition) | |
139 | def generate_atom_feed(repo_id, _repo_name, _feed_type): |
|
129 | def generate_atom_feed(repo_id, _repo_name, commit_id, _feed_type): | |
140 | feed = Atom1Feed( |
|
130 | feed = Atom1Feed( | |
141 | title=self.title % _repo_name, |
|
131 | title=self.title % _repo_name, | |
142 | link=h.route_url('repo_summary', repo_name=_repo_name), |
|
132 | link=h.route_url('repo_summary', repo_name=_repo_name), | |
143 | description=self.description % _repo_name, |
|
133 | description=self.description % _repo_name, | |
144 | language=self.language, |
|
134 | language=self.language, | |
145 | ttl=self.ttl |
|
135 | ttl=self.ttl | |
146 | ) |
|
136 | ) | |
147 |
|
137 | |||
148 | for commit in reversed(self._get_commits()): |
|
138 | for commit in reversed(self._get_commits()): | |
149 | date = self._set_timezone(commit.date) |
|
139 | date = self._set_timezone(commit.date) | |
150 | feed.add_item( |
|
140 | feed.add_item( | |
151 | unique_id=self.uid(repo_id, commit.raw_id), |
|
141 | unique_id=self.uid(repo_id, commit.raw_id), | |
152 | title=self._get_title(commit), |
|
142 | title=self._get_title(commit), | |
153 | author_name=commit.author, |
|
143 | author_name=commit.author, | |
154 | description=self._get_description(commit), |
|
144 | description=self._get_description(commit), | |
155 | link=h.route_url( |
|
145 | link=h.route_url( | |
156 | 'repo_commit', repo_name=_repo_name, |
|
146 | 'repo_commit', repo_name=_repo_name, | |
157 | commit_id=commit.raw_id), |
|
147 | commit_id=commit.raw_id), | |
158 | pubdate=date,) |
|
148 | pubdate=date,) | |
159 |
|
149 | |||
160 | return feed.mime_type, feed.writeString('utf-8') |
|
150 | return feed.mime_type, feed.writeString('utf-8') | |
161 |
|
151 | |||
162 | inv_context_manager = rc_cache.InvalidationContext( |
|
152 | commit_id = self.db_repo.changeset_cache.get('raw_id') | |
163 | uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace) |
|
153 | mime_type, feed = generate_atom_feed( | |
164 | with inv_context_manager as invalidation_context: |
|
154 | self.db_repo.repo_id, self.db_repo.repo_name, commit_id, 'atom') | |
165 | args = (self.db_repo.repo_id, self.db_repo.repo_name, 'atom',) |
|
|||
166 | # re-compute and store cache if we get invalidate signal |
|
|||
167 | if invalidation_context.should_invalidate(): |
|
|||
168 | mime_type, feed = generate_atom_feed.refresh(*args) |
|
|||
169 | else: |
|
|||
170 | mime_type, feed = generate_atom_feed(*args) |
|
|||
171 |
|
||||
172 | log.debug('Repo ATOM feed computed in %.4fs', |
|
|||
173 | inv_context_manager.compute_time) |
|
|||
174 |
|
155 | |||
175 | response = Response(feed) |
|
156 | response = Response(feed) | |
176 | response.content_type = mime_type |
|
157 | response.content_type = mime_type | |
177 | return response |
|
158 | return response | |
178 |
|
159 | |||
179 | @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED]) |
|
160 | @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED]) | |
180 | @HasRepoPermissionAnyDecorator( |
|
161 | @HasRepoPermissionAnyDecorator( | |
181 | 'repository.read', 'repository.write', 'repository.admin') |
|
162 | 'repository.read', 'repository.write', 'repository.admin') | |
182 | @view_config( |
|
163 | @view_config(route_name='rss_feed_home', request_method='GET', renderer=None) | |
183 |
|
|
164 | @view_config(route_name='rss_feed_home_old', request_method='GET', renderer=None) | |
184 | renderer=None) |
|
|||
185 | @view_config( |
|
|||
186 | route_name='rss_feed_home_old', request_method='GET', |
|
|||
187 | renderer=None) |
|
|||
188 | def rss(self): |
|
165 | def rss(self): | |
189 | """ |
|
166 | """ | |
190 | Produce an rss2 feed via feedgenerator module |
|
167 | Produce an rss2 feed via feedgenerator module | |
191 | """ |
|
168 | """ | |
192 | self.load_default_context() |
|
169 | self.load_default_context() | |
193 |
|
170 | |||
194 |
cache_namespace_uid = 'cache_repo_ |
|
171 | cache_namespace_uid = 'cache_repo_feed.{}'.format(self.db_repo.repo_id) | |
195 | self.db_repo.repo_id, CacheKey.CACHE_TYPE_FEED) |
|
|||
196 | invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format( |
|
|||
197 | repo_id=self.db_repo.repo_id) |
|
|||
198 | region = rc_cache.get_or_create_region('cache_repo_longterm', |
|
|||
199 | cache_namespace_uid) |
|
|||
200 |
|
||||
201 | condition = not self.path_filter.is_enabled |
|
172 | condition = not self.path_filter.is_enabled | |
|
173 | region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid) | |||
202 |
|
174 | |||
203 | @region.conditional_cache_on_arguments(namespace=cache_namespace_uid, |
|
175 | @region.conditional_cache_on_arguments(namespace=cache_namespace_uid, | |
204 | condition=condition) |
|
176 | condition=condition) | |
205 | def generate_rss_feed(repo_id, _repo_name, _feed_type): |
|
177 | def generate_rss_feed(repo_id, _repo_name, commit_id, _feed_type): | |
206 | feed = Rss201rev2Feed( |
|
178 | feed = Rss201rev2Feed( | |
207 | title=self.title % _repo_name, |
|
179 | title=self.title % _repo_name, | |
208 | link=h.route_url('repo_summary', repo_name=_repo_name), |
|
180 | link=h.route_url('repo_summary', repo_name=_repo_name), | |
209 | description=self.description % _repo_name, |
|
181 | description=self.description % _repo_name, | |
210 | language=self.language, |
|
182 | language=self.language, | |
211 | ttl=self.ttl |
|
183 | ttl=self.ttl | |
212 | ) |
|
184 | ) | |
213 |
|
185 | |||
214 | for commit in reversed(self._get_commits()): |
|
186 | for commit in reversed(self._get_commits()): | |
215 | date = self._set_timezone(commit.date) |
|
187 | date = self._set_timezone(commit.date) | |
216 | feed.add_item( |
|
188 | feed.add_item( | |
217 | unique_id=self.uid(repo_id, commit.raw_id), |
|
189 | unique_id=self.uid(repo_id, commit.raw_id), | |
218 | title=self._get_title(commit), |
|
190 | title=self._get_title(commit), | |
219 | author_name=commit.author, |
|
191 | author_name=commit.author, | |
220 | description=self._get_description(commit), |
|
192 | description=self._get_description(commit), | |
221 | link=h.route_url( |
|
193 | link=h.route_url( | |
222 | 'repo_commit', repo_name=_repo_name, |
|
194 | 'repo_commit', repo_name=_repo_name, | |
223 | commit_id=commit.raw_id), |
|
195 | commit_id=commit.raw_id), | |
224 | pubdate=date,) |
|
196 | pubdate=date,) | |
225 |
|
||||
226 | return feed.mime_type, feed.writeString('utf-8') |
|
197 | return feed.mime_type, feed.writeString('utf-8') | |
227 |
|
198 | |||
228 | inv_context_manager = rc_cache.InvalidationContext( |
|
199 | commit_id = self.db_repo.changeset_cache.get('raw_id') | |
229 | uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace) |
|
200 | mime_type, feed = generate_rss_feed( | |
230 | with inv_context_manager as invalidation_context: |
|
201 | self.db_repo.repo_id, self.db_repo.repo_name, commit_id, 'rss') | |
231 | args = (self.db_repo.repo_id, self.db_repo.repo_name, 'rss',) |
|
|||
232 | # re-compute and store cache if we get invalidate signal |
|
|||
233 | if invalidation_context.should_invalidate(): |
|
|||
234 | mime_type, feed = generate_rss_feed.refresh(*args) |
|
|||
235 | else: |
|
|||
236 | mime_type, feed = generate_rss_feed(*args) |
|
|||
237 | log.debug( |
|
|||
238 | 'Repo RSS feed computed in %.4fs', inv_context_manager.compute_time) |
|
|||
239 |
|
202 | |||
240 | response = Response(feed) |
|
203 | response = Response(feed) | |
241 | response.content_type = mime_type |
|
204 | response.content_type = mime_type | |
242 | return response |
|
205 | return response |
General Comments 0
You need to be logged in to leave comments.
Login now