Show More
@@ -1,391 +1,393 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2011-2019 RhodeCode GmbH |
|
3 | # Copyright (C) 2011-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 |
|
20 | |||
21 | import logging |
|
21 | import logging | |
22 | import string |
|
22 | import string | |
23 | import rhodecode |
|
23 | import rhodecode | |
24 |
|
24 | |||
25 | from pyramid.view import view_config |
|
25 | from pyramid.view import view_config | |
26 |
|
26 | |||
27 | from rhodecode.lib.view_utils import get_format_ref_id |
|
27 | from rhodecode.lib.view_utils import get_format_ref_id | |
28 | from rhodecode.apps._base import RepoAppView |
|
28 | from rhodecode.apps._base import RepoAppView | |
29 | from rhodecode.config.conf import (LANGUAGES_EXTENSIONS_MAP) |
|
29 | from rhodecode.config.conf import (LANGUAGES_EXTENSIONS_MAP) | |
30 | from rhodecode.lib import helpers as h, rc_cache |
|
30 | from rhodecode.lib import helpers as h, rc_cache | |
31 | from rhodecode.lib.utils2 import safe_str, safe_int |
|
31 | from rhodecode.lib.utils2 import safe_str, safe_int | |
32 | from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator |
|
32 | from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator | |
33 | from rhodecode.lib.markup_renderer import MarkupRenderer, relative_links |
|
33 | from rhodecode.lib.markup_renderer import MarkupRenderer, relative_links | |
34 | from rhodecode.lib.ext_json import json |
|
34 | from rhodecode.lib.ext_json import json | |
35 | from rhodecode.lib.vcs.backends.base import EmptyCommit |
|
35 | from rhodecode.lib.vcs.backends.base import EmptyCommit | |
36 | from rhodecode.lib.vcs.exceptions import ( |
|
36 | from rhodecode.lib.vcs.exceptions import ( | |
37 | CommitError, EmptyRepositoryError, CommitDoesNotExistError) |
|
37 | CommitError, EmptyRepositoryError, CommitDoesNotExistError) | |
38 | from rhodecode.model.db import Statistics, CacheKey, User |
|
38 | from rhodecode.model.db import Statistics, CacheKey, User | |
39 | from rhodecode.model.meta import Session |
|
39 | from rhodecode.model.meta import Session | |
40 | from rhodecode.model.repo import ReadmeFinder |
|
40 | from rhodecode.model.repo import ReadmeFinder | |
41 | from rhodecode.model.scm import ScmModel |
|
41 | from rhodecode.model.scm import ScmModel | |
42 |
|
42 | |||
43 | log = logging.getLogger(__name__) |
|
43 | log = logging.getLogger(__name__) | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | class RepoSummaryView(RepoAppView): |
|
46 | class RepoSummaryView(RepoAppView): | |
47 |
|
47 | |||
48 | def load_default_context(self): |
|
48 | def load_default_context(self): | |
49 | c = self._get_local_tmpl_context(include_app_defaults=True) |
|
49 | c = self._get_local_tmpl_context(include_app_defaults=True) | |
50 | c.rhodecode_repo = None |
|
50 | c.rhodecode_repo = None | |
51 | if not c.repository_requirements_missing: |
|
51 | if not c.repository_requirements_missing: | |
52 | c.rhodecode_repo = self.rhodecode_vcs_repo |
|
52 | c.rhodecode_repo = self.rhodecode_vcs_repo | |
53 | return c |
|
53 | return c | |
54 |
|
54 | |||
55 | def _get_readme_data(self, db_repo, renderer_type): |
|
55 | def _get_readme_data(self, db_repo, renderer_type): | |
56 |
|
56 | |||
57 | log.debug('Looking for README file') |
|
57 | log.debug('Looking for README file') | |
58 |
|
58 | |||
59 | cache_namespace_uid = 'cache_repo_instance.{}_{}'.format( |
|
59 | cache_namespace_uid = 'cache_repo_instance.{}_{}'.format( | |
60 | db_repo.repo_id, CacheKey.CACHE_TYPE_README) |
|
60 | db_repo.repo_id, CacheKey.CACHE_TYPE_README) | |
61 | invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format( |
|
61 | invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format( | |
62 | repo_id=self.db_repo.repo_id) |
|
62 | repo_id=self.db_repo.repo_id) | |
63 | region = rc_cache.get_or_create_region('cache_repo_longterm', cache_namespace_uid) |
|
63 | region = rc_cache.get_or_create_region('cache_repo_longterm', cache_namespace_uid) | |
64 |
|
64 | |||
65 | @region.conditional_cache_on_arguments(namespace=cache_namespace_uid) |
|
65 | @region.conditional_cache_on_arguments(namespace=cache_namespace_uid) | |
66 | def generate_repo_readme(repo_id, _repo_name, _renderer_type): |
|
66 | def generate_repo_readme(repo_id, _repo_name, _renderer_type): | |
67 | readme_data = None |
|
67 | readme_data = None | |
68 | readme_node = None |
|
68 | readme_node = None | |
69 | readme_filename = None |
|
69 | readme_filename = None | |
70 | commit = self._get_landing_commit_or_none(db_repo) |
|
70 | commit = self._get_landing_commit_or_none(db_repo) | |
71 | if commit: |
|
71 | if commit: | |
72 | log.debug("Searching for a README file.") |
|
72 | log.debug("Searching for a README file.") | |
73 | readme_node = ReadmeFinder(_renderer_type).search(commit) |
|
73 | readme_node = ReadmeFinder(_renderer_type).search(commit) | |
74 | if readme_node: |
|
74 | if readme_node: | |
75 | relative_urls = { |
|
75 | relative_urls = { | |
76 | 'raw': h.route_path( |
|
76 | 'raw': h.route_path( | |
77 | 'repo_file_raw', repo_name=_repo_name, |
|
77 | 'repo_file_raw', repo_name=_repo_name, | |
78 | commit_id=commit.raw_id, f_path=readme_node.path), |
|
78 | commit_id=commit.raw_id, f_path=readme_node.path), | |
79 | 'standard': h.route_path( |
|
79 | 'standard': h.route_path( | |
80 | 'repo_files', repo_name=_repo_name, |
|
80 | 'repo_files', repo_name=_repo_name, | |
81 | commit_id=commit.raw_id, f_path=readme_node.path), |
|
81 | commit_id=commit.raw_id, f_path=readme_node.path), | |
82 | } |
|
82 | } | |
83 | readme_data = self._render_readme_or_none( |
|
83 | readme_data = self._render_readme_or_none( | |
84 | commit, readme_node, relative_urls) |
|
84 | commit, readme_node, relative_urls) | |
85 | readme_filename = readme_node.path |
|
85 | readme_filename = readme_node.path | |
86 | return readme_data, readme_filename |
|
86 | return readme_data, readme_filename | |
87 |
|
87 | |||
88 | inv_context_manager = rc_cache.InvalidationContext( |
|
88 | inv_context_manager = rc_cache.InvalidationContext( | |
89 | uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace) |
|
89 | uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace) | |
90 | with inv_context_manager as invalidation_context: |
|
90 | with inv_context_manager as invalidation_context: | |
91 | args = (db_repo.repo_id, db_repo.repo_name, renderer_type,) |
|
91 | args = (db_repo.repo_id, db_repo.repo_name, renderer_type,) | |
92 | # re-compute and store cache if we get invalidate signal |
|
92 | # re-compute and store cache if we get invalidate signal | |
93 | if invalidation_context.should_invalidate(): |
|
93 | if invalidation_context.should_invalidate(): | |
94 | instance = generate_repo_readme.refresh(*args) |
|
94 | instance = generate_repo_readme.refresh(*args) | |
95 | else: |
|
95 | else: | |
96 | instance = generate_repo_readme(*args) |
|
96 | instance = generate_repo_readme(*args) | |
97 |
|
97 | |||
98 | log.debug( |
|
98 | log.debug( | |
99 | 'Repo readme generated and computed in %.3fs', |
|
99 | 'Repo readme generated and computed in %.3fs', | |
100 | inv_context_manager.compute_time) |
|
100 | inv_context_manager.compute_time) | |
101 | return instance |
|
101 | return instance | |
102 |
|
102 | |||
103 | def _get_landing_commit_or_none(self, db_repo): |
|
103 | def _get_landing_commit_or_none(self, db_repo): | |
104 | log.debug("Getting the landing commit.") |
|
104 | log.debug("Getting the landing commit.") | |
105 | try: |
|
105 | try: | |
106 | commit = db_repo.get_landing_commit() |
|
106 | commit = db_repo.get_landing_commit() | |
107 | if not isinstance(commit, EmptyCommit): |
|
107 | if not isinstance(commit, EmptyCommit): | |
108 | return commit |
|
108 | return commit | |
109 | else: |
|
109 | else: | |
110 | log.debug("Repository is empty, no README to render.") |
|
110 | log.debug("Repository is empty, no README to render.") | |
111 | except CommitError: |
|
111 | except CommitError: | |
112 | log.exception( |
|
112 | log.exception( | |
113 | "Problem getting commit when trying to render the README.") |
|
113 | "Problem getting commit when trying to render the README.") | |
114 |
|
114 | |||
115 | def _render_readme_or_none(self, commit, readme_node, relative_urls): |
|
115 | def _render_readme_or_none(self, commit, readme_node, relative_urls): | |
116 | log.debug( |
|
116 | log.debug( | |
117 | 'Found README file `%s` rendering...', readme_node.path) |
|
117 | 'Found README file `%s` rendering...', readme_node.path) | |
118 | renderer = MarkupRenderer() |
|
118 | renderer = MarkupRenderer() | |
119 | try: |
|
119 | try: | |
120 | html_source = renderer.render( |
|
120 | html_source = renderer.render( | |
121 | readme_node.content, filename=readme_node.path) |
|
121 | readme_node.content, filename=readme_node.path) | |
122 | if relative_urls: |
|
122 | if relative_urls: | |
123 | return relative_links(html_source, relative_urls) |
|
123 | return relative_links(html_source, relative_urls) | |
124 | return html_source |
|
124 | return html_source | |
125 | except Exception: |
|
125 | except Exception: | |
126 | log.exception( |
|
126 | log.exception( | |
127 | "Exception while trying to render the README") |
|
127 | "Exception while trying to render the README") | |
128 |
|
128 | |||
129 | def _load_commits_context(self, c): |
|
129 | def _load_commits_context(self, c): | |
130 | p = safe_int(self.request.GET.get('page'), 1) |
|
130 | p = safe_int(self.request.GET.get('page'), 1) | |
131 | size = safe_int(self.request.GET.get('size'), 10) |
|
131 | size = safe_int(self.request.GET.get('size'), 10) | |
132 |
|
132 | |||
133 | def url_generator(**kw): |
|
133 | def url_generator(**kw): | |
134 | query_params = { |
|
134 | query_params = { | |
135 | 'size': size |
|
135 | 'size': size | |
136 | } |
|
136 | } | |
137 | query_params.update(kw) |
|
137 | query_params.update(kw) | |
138 | return h.route_path( |
|
138 | return h.route_path( | |
139 | 'repo_summary_commits', |
|
139 | 'repo_summary_commits', | |
140 | repo_name=c.rhodecode_db_repo.repo_name, _query=query_params) |
|
140 | repo_name=c.rhodecode_db_repo.repo_name, _query=query_params) | |
141 |
|
141 | |||
142 | pre_load = ['author', 'branch', 'date', 'message'] |
|
142 | pre_load = ['author', 'branch', 'date', 'message'] | |
143 | try: |
|
143 | try: | |
144 | collection = self.rhodecode_vcs_repo.get_commits( |
|
144 | collection = self.rhodecode_vcs_repo.get_commits( | |
145 | pre_load=pre_load, translate_tags=False) |
|
145 | pre_load=pre_load, translate_tags=False) | |
146 | except EmptyRepositoryError: |
|
146 | except EmptyRepositoryError: | |
147 | collection = self.rhodecode_vcs_repo |
|
147 | collection = self.rhodecode_vcs_repo | |
148 |
|
148 | |||
149 | c.repo_commits = h.RepoPage( |
|
149 | c.repo_commits = h.RepoPage( | |
150 | collection, page=p, items_per_page=size, url=url_generator) |
|
150 | collection, page=p, items_per_page=size, url=url_generator) | |
151 | page_ids = [x.raw_id for x in c.repo_commits] |
|
151 | page_ids = [x.raw_id for x in c.repo_commits] | |
152 | c.comments = self.db_repo.get_comments(page_ids) |
|
152 | c.comments = self.db_repo.get_comments(page_ids) | |
153 | c.statuses = self.db_repo.statuses(page_ids) |
|
153 | c.statuses = self.db_repo.statuses(page_ids) | |
154 |
|
154 | |||
|
155 | def _prepare_and_set_clone_url(self, c): | |||
|
156 | username = '' | |||
|
157 | if self._rhodecode_user.username != User.DEFAULT_USER: | |||
|
158 | username = safe_str(self._rhodecode_user.username) | |||
|
159 | ||||
|
160 | _def_clone_uri = _def_clone_uri_id = c.clone_uri_tmpl | |||
|
161 | _def_clone_uri_ssh = c.clone_uri_ssh_tmpl | |||
|
162 | ||||
|
163 | if '{repo}' in _def_clone_uri: | |||
|
164 | _def_clone_uri_id = _def_clone_uri.replace('{repo}', '_{repoid}') | |||
|
165 | elif '{repoid}' in _def_clone_uri: | |||
|
166 | _def_clone_uri_id = _def_clone_uri.replace('_{repoid}', '{repo}') | |||
|
167 | ||||
|
168 | c.clone_repo_url = self.db_repo.clone_url( | |||
|
169 | user=username, uri_tmpl=_def_clone_uri) | |||
|
170 | c.clone_repo_url_id = self.db_repo.clone_url( | |||
|
171 | user=username, uri_tmpl=_def_clone_uri_id) | |||
|
172 | c.clone_repo_url_ssh = self.db_repo.clone_url( | |||
|
173 | uri_tmpl=_def_clone_uri_ssh, ssh=True) | |||
|
174 | ||||
155 | @LoginRequired() |
|
175 | @LoginRequired() | |
156 | @HasRepoPermissionAnyDecorator( |
|
176 | @HasRepoPermissionAnyDecorator( | |
157 | 'repository.read', 'repository.write', 'repository.admin') |
|
177 | 'repository.read', 'repository.write', 'repository.admin') | |
158 | @view_config( |
|
178 | @view_config( | |
159 | route_name='repo_summary_commits', request_method='GET', |
|
179 | route_name='repo_summary_commits', request_method='GET', | |
160 | renderer='rhodecode:templates/summary/summary_commits.mako') |
|
180 | renderer='rhodecode:templates/summary/summary_commits.mako') | |
161 | def summary_commits(self): |
|
181 | def summary_commits(self): | |
162 | c = self.load_default_context() |
|
182 | c = self.load_default_context() | |
|
183 | self._prepare_and_set_clone_url(c) | |||
163 | self._load_commits_context(c) |
|
184 | self._load_commits_context(c) | |
164 | return self._get_template_context(c) |
|
185 | return self._get_template_context(c) | |
165 |
|
186 | |||
166 | @LoginRequired() |
|
187 | @LoginRequired() | |
167 | @HasRepoPermissionAnyDecorator( |
|
188 | @HasRepoPermissionAnyDecorator( | |
168 | 'repository.read', 'repository.write', 'repository.admin') |
|
189 | 'repository.read', 'repository.write', 'repository.admin') | |
169 | @view_config( |
|
190 | @view_config( | |
170 | route_name='repo_summary', request_method='GET', |
|
191 | route_name='repo_summary', request_method='GET', | |
171 | renderer='rhodecode:templates/summary/summary.mako') |
|
192 | renderer='rhodecode:templates/summary/summary.mako') | |
172 | @view_config( |
|
193 | @view_config( | |
173 | route_name='repo_summary_slash', request_method='GET', |
|
194 | route_name='repo_summary_slash', request_method='GET', | |
174 | renderer='rhodecode:templates/summary/summary.mako') |
|
195 | renderer='rhodecode:templates/summary/summary.mako') | |
175 | @view_config( |
|
196 | @view_config( | |
176 | route_name='repo_summary_explicit', request_method='GET', |
|
197 | route_name='repo_summary_explicit', request_method='GET', | |
177 | renderer='rhodecode:templates/summary/summary.mako') |
|
198 | renderer='rhodecode:templates/summary/summary.mako') | |
178 | def summary(self): |
|
199 | def summary(self): | |
179 | c = self.load_default_context() |
|
200 | c = self.load_default_context() | |
180 |
|
201 | |||
181 | # Prepare the clone URL |
|
202 | # Prepare the clone URL | |
182 | username = '' |
|
203 | self._prepare_and_set_clone_url(c) | |
183 | if self._rhodecode_user.username != User.DEFAULT_USER: |
|
|||
184 | username = safe_str(self._rhodecode_user.username) |
|
|||
185 |
|
||||
186 | _def_clone_uri = _def_clone_uri_id = c.clone_uri_tmpl |
|
|||
187 | _def_clone_uri_ssh = c.clone_uri_ssh_tmpl |
|
|||
188 |
|
||||
189 | if '{repo}' in _def_clone_uri: |
|
|||
190 | _def_clone_uri_id = _def_clone_uri.replace( |
|
|||
191 | '{repo}', '_{repoid}') |
|
|||
192 | elif '{repoid}' in _def_clone_uri: |
|
|||
193 | _def_clone_uri_id = _def_clone_uri.replace( |
|
|||
194 | '_{repoid}', '{repo}') |
|
|||
195 |
|
||||
196 | c.clone_repo_url = self.db_repo.clone_url( |
|
|||
197 | user=username, uri_tmpl=_def_clone_uri) |
|
|||
198 | c.clone_repo_url_id = self.db_repo.clone_url( |
|
|||
199 | user=username, uri_tmpl=_def_clone_uri_id) |
|
|||
200 | c.clone_repo_url_ssh = self.db_repo.clone_url( |
|
|||
201 | uri_tmpl=_def_clone_uri_ssh, ssh=True) |
|
|||
202 |
|
204 | |||
203 | # If enabled, get statistics data |
|
205 | # If enabled, get statistics data | |
204 |
|
206 | |||
205 | c.show_stats = bool(self.db_repo.enable_statistics) |
|
207 | c.show_stats = bool(self.db_repo.enable_statistics) | |
206 |
|
208 | |||
207 | stats = Session().query(Statistics) \ |
|
209 | stats = Session().query(Statistics) \ | |
208 | .filter(Statistics.repository == self.db_repo) \ |
|
210 | .filter(Statistics.repository == self.db_repo) \ | |
209 | .scalar() |
|
211 | .scalar() | |
210 |
|
212 | |||
211 | c.stats_percentage = 0 |
|
213 | c.stats_percentage = 0 | |
212 |
|
214 | |||
213 | if stats and stats.languages: |
|
215 | if stats and stats.languages: | |
214 | c.no_data = False is self.db_repo.enable_statistics |
|
216 | c.no_data = False is self.db_repo.enable_statistics | |
215 | lang_stats_d = json.loads(stats.languages) |
|
217 | lang_stats_d = json.loads(stats.languages) | |
216 |
|
218 | |||
217 | # Sort first by decreasing count and second by the file extension, |
|
219 | # Sort first by decreasing count and second by the file extension, | |
218 | # so we have a consistent output. |
|
220 | # so we have a consistent output. | |
219 | lang_stats_items = sorted(lang_stats_d.iteritems(), |
|
221 | lang_stats_items = sorted(lang_stats_d.iteritems(), | |
220 | key=lambda k: (-k[1], k[0]))[:10] |
|
222 | key=lambda k: (-k[1], k[0]))[:10] | |
221 | lang_stats = [(x, {"count": y, |
|
223 | lang_stats = [(x, {"count": y, | |
222 | "desc": LANGUAGES_EXTENSIONS_MAP.get(x)}) |
|
224 | "desc": LANGUAGES_EXTENSIONS_MAP.get(x)}) | |
223 | for x, y in lang_stats_items] |
|
225 | for x, y in lang_stats_items] | |
224 |
|
226 | |||
225 | c.trending_languages = json.dumps(lang_stats) |
|
227 | c.trending_languages = json.dumps(lang_stats) | |
226 | else: |
|
228 | else: | |
227 | c.no_data = True |
|
229 | c.no_data = True | |
228 | c.trending_languages = json.dumps({}) |
|
230 | c.trending_languages = json.dumps({}) | |
229 |
|
231 | |||
230 | scm_model = ScmModel() |
|
232 | scm_model = ScmModel() | |
231 | c.enable_downloads = self.db_repo.enable_downloads |
|
233 | c.enable_downloads = self.db_repo.enable_downloads | |
232 | c.repository_followers = scm_model.get_followers(self.db_repo) |
|
234 | c.repository_followers = scm_model.get_followers(self.db_repo) | |
233 | c.repository_forks = scm_model.get_forks(self.db_repo) |
|
235 | c.repository_forks = scm_model.get_forks(self.db_repo) | |
234 | c.repository_is_user_following = scm_model.is_following_repo( |
|
236 | c.repository_is_user_following = scm_model.is_following_repo( | |
235 | self.db_repo_name, self._rhodecode_user.user_id) |
|
237 | self.db_repo_name, self._rhodecode_user.user_id) | |
236 |
|
238 | |||
237 | # first interaction with the VCS instance after here... |
|
239 | # first interaction with the VCS instance after here... | |
238 | if c.repository_requirements_missing: |
|
240 | if c.repository_requirements_missing: | |
239 | self.request.override_renderer = \ |
|
241 | self.request.override_renderer = \ | |
240 | 'rhodecode:templates/summary/missing_requirements.mako' |
|
242 | 'rhodecode:templates/summary/missing_requirements.mako' | |
241 | return self._get_template_context(c) |
|
243 | return self._get_template_context(c) | |
242 |
|
244 | |||
243 | c.readme_data, c.readme_file = \ |
|
245 | c.readme_data, c.readme_file = \ | |
244 | self._get_readme_data(self.db_repo, c.visual.default_renderer) |
|
246 | self._get_readme_data(self.db_repo, c.visual.default_renderer) | |
245 |
|
247 | |||
246 | # loads the summary commits template context |
|
248 | # loads the summary commits template context | |
247 | self._load_commits_context(c) |
|
249 | self._load_commits_context(c) | |
248 |
|
250 | |||
249 | return self._get_template_context(c) |
|
251 | return self._get_template_context(c) | |
250 |
|
252 | |||
251 | def get_request_commit_id(self): |
|
253 | def get_request_commit_id(self): | |
252 | return self.request.matchdict['commit_id'] |
|
254 | return self.request.matchdict['commit_id'] | |
253 |
|
255 | |||
254 | @LoginRequired() |
|
256 | @LoginRequired() | |
255 | @HasRepoPermissionAnyDecorator( |
|
257 | @HasRepoPermissionAnyDecorator( | |
256 | 'repository.read', 'repository.write', 'repository.admin') |
|
258 | 'repository.read', 'repository.write', 'repository.admin') | |
257 | @view_config( |
|
259 | @view_config( | |
258 | route_name='repo_stats', request_method='GET', |
|
260 | route_name='repo_stats', request_method='GET', | |
259 | renderer='json_ext') |
|
261 | renderer='json_ext') | |
260 | def repo_stats(self): |
|
262 | def repo_stats(self): | |
261 | commit_id = self.get_request_commit_id() |
|
263 | commit_id = self.get_request_commit_id() | |
262 | show_stats = bool(self.db_repo.enable_statistics) |
|
264 | show_stats = bool(self.db_repo.enable_statistics) | |
263 | repo_id = self.db_repo.repo_id |
|
265 | repo_id = self.db_repo.repo_id | |
264 |
|
266 | |||
265 | cache_seconds = safe_int( |
|
267 | cache_seconds = safe_int( | |
266 | rhodecode.CONFIG.get('rc_cache.cache_repo.expiration_time')) |
|
268 | rhodecode.CONFIG.get('rc_cache.cache_repo.expiration_time')) | |
267 | cache_on = cache_seconds > 0 |
|
269 | cache_on = cache_seconds > 0 | |
268 | log.debug( |
|
270 | log.debug( | |
269 | 'Computing REPO TREE for repo_id %s commit_id `%s` ' |
|
271 | 'Computing REPO TREE for repo_id %s commit_id `%s` ' | |
270 | 'with caching: %s[TTL: %ss]' % ( |
|
272 | 'with caching: %s[TTL: %ss]' % ( | |
271 | repo_id, commit_id, cache_on, cache_seconds or 0)) |
|
273 | repo_id, commit_id, cache_on, cache_seconds or 0)) | |
272 |
|
274 | |||
273 | cache_namespace_uid = 'cache_repo.{}'.format(repo_id) |
|
275 | cache_namespace_uid = 'cache_repo.{}'.format(repo_id) | |
274 | region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid) |
|
276 | region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid) | |
275 |
|
277 | |||
276 | @region.conditional_cache_on_arguments(namespace=cache_namespace_uid, |
|
278 | @region.conditional_cache_on_arguments(namespace=cache_namespace_uid, | |
277 | condition=cache_on) |
|
279 | condition=cache_on) | |
278 | def compute_stats(repo_id, commit_id, show_stats): |
|
280 | def compute_stats(repo_id, commit_id, show_stats): | |
279 | code_stats = {} |
|
281 | code_stats = {} | |
280 | size = 0 |
|
282 | size = 0 | |
281 | try: |
|
283 | try: | |
282 | scm_instance = self.db_repo.scm_instance() |
|
284 | scm_instance = self.db_repo.scm_instance() | |
283 | commit = scm_instance.get_commit(commit_id) |
|
285 | commit = scm_instance.get_commit(commit_id) | |
284 |
|
286 | |||
285 | for node in commit.get_filenodes_generator(): |
|
287 | for node in commit.get_filenodes_generator(): | |
286 | size += node.size |
|
288 | size += node.size | |
287 | if not show_stats: |
|
289 | if not show_stats: | |
288 | continue |
|
290 | continue | |
289 | ext = string.lower(node.extension) |
|
291 | ext = string.lower(node.extension) | |
290 | ext_info = LANGUAGES_EXTENSIONS_MAP.get(ext) |
|
292 | ext_info = LANGUAGES_EXTENSIONS_MAP.get(ext) | |
291 | if ext_info: |
|
293 | if ext_info: | |
292 | if ext in code_stats: |
|
294 | if ext in code_stats: | |
293 | code_stats[ext]['count'] += 1 |
|
295 | code_stats[ext]['count'] += 1 | |
294 | else: |
|
296 | else: | |
295 | code_stats[ext] = {"count": 1, "desc": ext_info} |
|
297 | code_stats[ext] = {"count": 1, "desc": ext_info} | |
296 | except (EmptyRepositoryError, CommitDoesNotExistError): |
|
298 | except (EmptyRepositoryError, CommitDoesNotExistError): | |
297 | pass |
|
299 | pass | |
298 | return {'size': h.format_byte_size_binary(size), |
|
300 | return {'size': h.format_byte_size_binary(size), | |
299 | 'code_stats': code_stats} |
|
301 | 'code_stats': code_stats} | |
300 |
|
302 | |||
301 | stats = compute_stats(self.db_repo.repo_id, commit_id, show_stats) |
|
303 | stats = compute_stats(self.db_repo.repo_id, commit_id, show_stats) | |
302 | return stats |
|
304 | return stats | |
303 |
|
305 | |||
304 | @LoginRequired() |
|
306 | @LoginRequired() | |
305 | @HasRepoPermissionAnyDecorator( |
|
307 | @HasRepoPermissionAnyDecorator( | |
306 | 'repository.read', 'repository.write', 'repository.admin') |
|
308 | 'repository.read', 'repository.write', 'repository.admin') | |
307 | @view_config( |
|
309 | @view_config( | |
308 | route_name='repo_refs_data', request_method='GET', |
|
310 | route_name='repo_refs_data', request_method='GET', | |
309 | renderer='json_ext') |
|
311 | renderer='json_ext') | |
310 | def repo_refs_data(self): |
|
312 | def repo_refs_data(self): | |
311 | _ = self.request.translate |
|
313 | _ = self.request.translate | |
312 | self.load_default_context() |
|
314 | self.load_default_context() | |
313 |
|
315 | |||
314 | repo = self.rhodecode_vcs_repo |
|
316 | repo = self.rhodecode_vcs_repo | |
315 | refs_to_create = [ |
|
317 | refs_to_create = [ | |
316 | (_("Branch"), repo.branches, 'branch'), |
|
318 | (_("Branch"), repo.branches, 'branch'), | |
317 | (_("Tag"), repo.tags, 'tag'), |
|
319 | (_("Tag"), repo.tags, 'tag'), | |
318 | (_("Bookmark"), repo.bookmarks, 'book'), |
|
320 | (_("Bookmark"), repo.bookmarks, 'book'), | |
319 | ] |
|
321 | ] | |
320 | res = self._create_reference_data( |
|
322 | res = self._create_reference_data( | |
321 | repo, self.db_repo_name, refs_to_create) |
|
323 | repo, self.db_repo_name, refs_to_create) | |
322 | data = { |
|
324 | data = { | |
323 | 'more': False, |
|
325 | 'more': False, | |
324 | 'results': res |
|
326 | 'results': res | |
325 | } |
|
327 | } | |
326 | return data |
|
328 | return data | |
327 |
|
329 | |||
328 | @LoginRequired() |
|
330 | @LoginRequired() | |
329 | @HasRepoPermissionAnyDecorator( |
|
331 | @HasRepoPermissionAnyDecorator( | |
330 | 'repository.read', 'repository.write', 'repository.admin') |
|
332 | 'repository.read', 'repository.write', 'repository.admin') | |
331 | @view_config( |
|
333 | @view_config( | |
332 | route_name='repo_refs_changelog_data', request_method='GET', |
|
334 | route_name='repo_refs_changelog_data', request_method='GET', | |
333 | renderer='json_ext') |
|
335 | renderer='json_ext') | |
334 | def repo_refs_changelog_data(self): |
|
336 | def repo_refs_changelog_data(self): | |
335 | _ = self.request.translate |
|
337 | _ = self.request.translate | |
336 | self.load_default_context() |
|
338 | self.load_default_context() | |
337 |
|
339 | |||
338 | repo = self.rhodecode_vcs_repo |
|
340 | repo = self.rhodecode_vcs_repo | |
339 |
|
341 | |||
340 | refs_to_create = [ |
|
342 | refs_to_create = [ | |
341 | (_("Branches"), repo.branches, 'branch'), |
|
343 | (_("Branches"), repo.branches, 'branch'), | |
342 | (_("Closed branches"), repo.branches_closed, 'branch_closed'), |
|
344 | (_("Closed branches"), repo.branches_closed, 'branch_closed'), | |
343 | # TODO: enable when vcs can handle bookmarks filters |
|
345 | # TODO: enable when vcs can handle bookmarks filters | |
344 | # (_("Bookmarks"), repo.bookmarks, "book"), |
|
346 | # (_("Bookmarks"), repo.bookmarks, "book"), | |
345 | ] |
|
347 | ] | |
346 | res = self._create_reference_data( |
|
348 | res = self._create_reference_data( | |
347 | repo, self.db_repo_name, refs_to_create) |
|
349 | repo, self.db_repo_name, refs_to_create) | |
348 | data = { |
|
350 | data = { | |
349 | 'more': False, |
|
351 | 'more': False, | |
350 | 'results': res |
|
352 | 'results': res | |
351 | } |
|
353 | } | |
352 | return data |
|
354 | return data | |
353 |
|
355 | |||
354 | def _create_reference_data(self, repo, full_repo_name, refs_to_create): |
|
356 | def _create_reference_data(self, repo, full_repo_name, refs_to_create): | |
355 | format_ref_id = get_format_ref_id(repo) |
|
357 | format_ref_id = get_format_ref_id(repo) | |
356 |
|
358 | |||
357 | result = [] |
|
359 | result = [] | |
358 | for title, refs, ref_type in refs_to_create: |
|
360 | for title, refs, ref_type in refs_to_create: | |
359 | if refs: |
|
361 | if refs: | |
360 | result.append({ |
|
362 | result.append({ | |
361 | 'text': title, |
|
363 | 'text': title, | |
362 | 'children': self._create_reference_items( |
|
364 | 'children': self._create_reference_items( | |
363 | repo, full_repo_name, refs, ref_type, |
|
365 | repo, full_repo_name, refs, ref_type, | |
364 | format_ref_id), |
|
366 | format_ref_id), | |
365 | }) |
|
367 | }) | |
366 | return result |
|
368 | return result | |
367 |
|
369 | |||
368 | def _create_reference_items(self, repo, full_repo_name, refs, ref_type, |
|
370 | def _create_reference_items(self, repo, full_repo_name, refs, ref_type, | |
369 | format_ref_id): |
|
371 | format_ref_id): | |
370 | result = [] |
|
372 | result = [] | |
371 | is_svn = h.is_svn(repo) |
|
373 | is_svn = h.is_svn(repo) | |
372 | for ref_name, raw_id in refs.iteritems(): |
|
374 | for ref_name, raw_id in refs.iteritems(): | |
373 | files_url = self._create_files_url( |
|
375 | files_url = self._create_files_url( | |
374 | repo, full_repo_name, ref_name, raw_id, is_svn) |
|
376 | repo, full_repo_name, ref_name, raw_id, is_svn) | |
375 | result.append({ |
|
377 | result.append({ | |
376 | 'text': ref_name, |
|
378 | 'text': ref_name, | |
377 | 'id': format_ref_id(ref_name, raw_id), |
|
379 | 'id': format_ref_id(ref_name, raw_id), | |
378 | 'raw_id': raw_id, |
|
380 | 'raw_id': raw_id, | |
379 | 'type': ref_type, |
|
381 | 'type': ref_type, | |
380 | 'files_url': files_url, |
|
382 | 'files_url': files_url, | |
381 | }) |
|
383 | }) | |
382 | return result |
|
384 | return result | |
383 |
|
385 | |||
384 | def _create_files_url(self, repo, full_repo_name, ref_name, raw_id, is_svn): |
|
386 | def _create_files_url(self, repo, full_repo_name, ref_name, raw_id, is_svn): | |
385 | use_commit_id = '/' in ref_name or is_svn |
|
387 | use_commit_id = '/' in ref_name or is_svn | |
386 | return h.route_path( |
|
388 | return h.route_path( | |
387 | 'repo_files', |
|
389 | 'repo_files', | |
388 | repo_name=full_repo_name, |
|
390 | repo_name=full_repo_name, | |
389 | f_path=ref_name if is_svn else '', |
|
391 | f_path=ref_name if is_svn else '', | |
390 | commit_id=raw_id if use_commit_id else ref_name, |
|
392 | commit_id=raw_id if use_commit_id else ref_name, | |
391 | _query=dict(at=ref_name)) |
|
393 | _query=dict(at=ref_name)) |
General Comments 0
You need to be logged in to leave comments.
Login now