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