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