Show More
@@ -1,223 +1,223 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2011-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2011-2017 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 |
|
22 | |||
23 | from pyramid.view import view_config |
|
23 | from pyramid.view import view_config | |
24 | from pyramid.httpexceptions import HTTPFound |
|
24 | from pyramid.httpexceptions import HTTPFound | |
25 |
|
25 | |||
26 | from rhodecode.apps._base import RepoAppView |
|
26 | from rhodecode.apps._base import RepoAppView | |
27 | from rhodecode.lib import helpers as h |
|
27 | from rhodecode.lib import helpers as h | |
28 | from rhodecode.lib import audit_logger |
|
28 | from rhodecode.lib import audit_logger | |
29 | from rhodecode.lib.auth import ( |
|
29 | from rhodecode.lib.auth import ( | |
30 | LoginRequired, HasRepoPermissionAnyDecorator, CSRFRequired) |
|
30 | LoginRequired, HasRepoPermissionAnyDecorator, CSRFRequired) | |
31 | from rhodecode.lib.exceptions import AttachedForksError |
|
31 | from rhodecode.lib.exceptions import AttachedForksError | |
32 | from rhodecode.lib.utils2 import safe_int |
|
32 | from rhodecode.lib.utils2 import safe_int | |
33 | from rhodecode.lib.vcs import RepositoryError |
|
33 | from rhodecode.lib.vcs import RepositoryError | |
34 | from rhodecode.model.db import Session, UserFollowing, User, Repository |
|
34 | from rhodecode.model.db import Session, UserFollowing, User, Repository | |
35 | from rhodecode.model.repo import RepoModel |
|
35 | from rhodecode.model.repo import RepoModel | |
36 | from rhodecode.model.scm import ScmModel |
|
36 | from rhodecode.model.scm import ScmModel | |
37 |
|
37 | |||
38 | log = logging.getLogger(__name__) |
|
38 | log = logging.getLogger(__name__) | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | class RepoSettingsView(RepoAppView): |
|
41 | class RepoSettingsView(RepoAppView): | |
42 |
|
42 | |||
43 | def load_default_context(self): |
|
43 | def load_default_context(self): | |
44 | c = self._get_local_tmpl_context() |
|
44 | c = self._get_local_tmpl_context() | |
45 |
|
45 | |||
46 | self._register_global_c(c) |
|
46 | self._register_global_c(c) | |
47 | return c |
|
47 | return c | |
48 |
|
48 | |||
49 | @LoginRequired() |
|
49 | @LoginRequired() | |
50 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
50 | @HasRepoPermissionAnyDecorator('repository.admin') | |
51 | @view_config( |
|
51 | @view_config( | |
52 | route_name='edit_repo_advanced', request_method='GET', |
|
52 | route_name='edit_repo_advanced', request_method='GET', | |
53 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') |
|
53 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') | |
54 | def edit_advanced(self): |
|
54 | def edit_advanced(self): | |
55 | c = self.load_default_context() |
|
55 | c = self.load_default_context() | |
56 | c.active = 'advanced' |
|
56 | c.active = 'advanced' | |
57 |
|
57 | |||
58 | c.default_user_id = User.get_default_user().user_id |
|
58 | c.default_user_id = User.get_default_user().user_id | |
59 | c.in_public_journal = UserFollowing.query() \ |
|
59 | c.in_public_journal = UserFollowing.query() \ | |
60 | .filter(UserFollowing.user_id == c.default_user_id) \ |
|
60 | .filter(UserFollowing.user_id == c.default_user_id) \ | |
61 | .filter(UserFollowing.follows_repository == self.db_repo).scalar() |
|
61 | .filter(UserFollowing.follows_repository == self.db_repo).scalar() | |
62 |
|
62 | |||
63 | c.has_origin_repo_read_perm = False |
|
63 | c.has_origin_repo_read_perm = False | |
64 | if self.db_repo.fork: |
|
64 | if self.db_repo.fork: | |
65 | c.has_origin_repo_read_perm = h.HasRepoPermissionAny( |
|
65 | c.has_origin_repo_read_perm = h.HasRepoPermissionAny( | |
66 | 'repository.write', 'repository.read', 'repository.admin')( |
|
66 | 'repository.write', 'repository.read', 'repository.admin')( | |
67 | self.db_repo.fork.repo_name, 'repo set as fork page') |
|
67 | self.db_repo.fork.repo_name, 'repo set as fork page') | |
68 |
|
68 | |||
69 | return self._get_template_context(c) |
|
69 | return self._get_template_context(c) | |
70 |
|
70 | |||
71 | @LoginRequired() |
|
71 | @LoginRequired() | |
72 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
72 | @HasRepoPermissionAnyDecorator('repository.admin') | |
73 | @CSRFRequired() |
|
73 | @CSRFRequired() | |
74 | @view_config( |
|
74 | @view_config( | |
75 | route_name='edit_repo_advanced_delete', request_method='POST', |
|
75 | route_name='edit_repo_advanced_delete', request_method='POST', | |
76 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') |
|
76 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') | |
77 | def edit_advanced_delete(self): |
|
77 | def edit_advanced_delete(self): | |
78 | """ |
|
78 | """ | |
79 | Deletes the repository, or shows warnings if deletion is not possible |
|
79 | Deletes the repository, or shows warnings if deletion is not possible | |
80 | because of attached forks or other errors. |
|
80 | because of attached forks or other errors. | |
81 | """ |
|
81 | """ | |
82 | _ = self.request.translate |
|
82 | _ = self.request.translate | |
83 | handle_forks = self.request.POST.get('forks', None) |
|
83 | handle_forks = self.request.POST.get('forks', None) | |
84 |
|
84 | |||
85 | try: |
|
85 | try: | |
86 | _forks = self.db_repo.forks.count() |
|
86 | _forks = self.db_repo.forks.count() | |
87 | if _forks and handle_forks: |
|
87 | if _forks and handle_forks: | |
88 | if handle_forks == 'detach_forks': |
|
88 | if handle_forks == 'detach_forks': | |
89 | handle_forks = 'detach' |
|
89 | handle_forks = 'detach' | |
90 | h.flash(_('Detached %s forks') % _forks, category='success') |
|
90 | h.flash(_('Detached %s forks') % _forks, category='success') | |
91 | elif handle_forks == 'delete_forks': |
|
91 | elif handle_forks == 'delete_forks': | |
92 | handle_forks = 'delete' |
|
92 | handle_forks = 'delete' | |
93 | h.flash(_('Deleted %s forks') % _forks, category='success') |
|
93 | h.flash(_('Deleted %s forks') % _forks, category='success') | |
94 |
|
94 | |||
95 | old_data = self.db_repo.get_api_data() |
|
95 | old_data = self.db_repo.get_api_data() | |
96 | RepoModel().delete(self.db_repo, forks=handle_forks) |
|
96 | RepoModel().delete(self.db_repo, forks=handle_forks) | |
97 |
|
97 | |||
98 | repo = audit_logger.RepoWrap(repo_id=None, |
|
98 | repo = audit_logger.RepoWrap(repo_id=None, | |
99 | repo_name=self.db_repo.repo_name) |
|
99 | repo_name=self.db_repo.repo_name) | |
100 | audit_logger.store_web( |
|
100 | audit_logger.store_web( | |
101 | 'repo.delete', action_data={'old_data': old_data}, |
|
101 | 'repo.delete', action_data={'old_data': old_data}, | |
102 | user=self._rhodecode_user, repo=repo) |
|
102 | user=self._rhodecode_user, repo=repo) | |
103 |
|
103 | |||
104 | ScmModel().mark_for_invalidation(self.db_repo_name, delete=True) |
|
104 | ScmModel().mark_for_invalidation(self.db_repo_name, delete=True) | |
105 | h.flash( |
|
105 | h.flash( | |
106 | _('Deleted repository `%s`') % self.db_repo_name, |
|
106 | _('Deleted repository `%s`') % self.db_repo_name, | |
107 | category='success') |
|
107 | category='success') | |
108 | Session().commit() |
|
108 | Session().commit() | |
109 | except AttachedForksError: |
|
109 | except AttachedForksError: | |
110 | repo_advanced_url = h.route_path( |
|
110 | repo_advanced_url = h.route_path( | |
111 | 'edit_repo_advanced', repo_name=self.db_repo_name, |
|
111 | 'edit_repo_advanced', repo_name=self.db_repo_name, | |
112 | _anchor='advanced-delete') |
|
112 | _anchor='advanced-delete') | |
113 | delete_anchor = h.link_to(_('detach or delete'), repo_advanced_url) |
|
113 | delete_anchor = h.link_to(_('detach or delete'), repo_advanced_url) | |
114 | h.flash(_('Cannot delete `{repo}` it still contains attached forks. ' |
|
114 | h.flash(_('Cannot delete `{repo}` it still contains attached forks. ' | |
115 | 'Try using {delete_or_detach} option.') |
|
115 | 'Try using {delete_or_detach} option.') | |
116 | .format(repo=self.db_repo_name, delete_or_detach=delete_anchor), |
|
116 | .format(repo=self.db_repo_name, delete_or_detach=delete_anchor), | |
117 | category='warning') |
|
117 | category='warning') | |
118 |
|
118 | |||
119 | # redirect to advanced for forks handle action ? |
|
119 | # redirect to advanced for forks handle action ? | |
120 | raise HTTPFound(repo_advanced_url) |
|
120 | raise HTTPFound(repo_advanced_url) | |
121 |
|
121 | |||
122 | except Exception: |
|
122 | except Exception: | |
123 | log.exception("Exception during deletion of repository") |
|
123 | log.exception("Exception during deletion of repository") | |
124 | h.flash(_('An error occurred during deletion of `%s`') |
|
124 | h.flash(_('An error occurred during deletion of `%s`') | |
125 | % self.db_repo_name, category='error') |
|
125 | % self.db_repo_name, category='error') | |
126 | # redirect to advanced for more deletion options |
|
126 | # redirect to advanced for more deletion options | |
127 | raise HTTPFound( |
|
127 | raise HTTPFound( | |
128 |
h.route_path('edit_repo_advanced', repo_name=self.db_repo_name |
|
128 | h.route_path('edit_repo_advanced', repo_name=self.db_repo_name, | |
129 | _anchor='advanced-delete') |
|
129 | _anchor='advanced-delete')) | |
130 |
|
130 | |||
131 | raise HTTPFound(h.route_path('home')) |
|
131 | raise HTTPFound(h.route_path('home')) | |
132 |
|
132 | |||
133 | @LoginRequired() |
|
133 | @LoginRequired() | |
134 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
134 | @HasRepoPermissionAnyDecorator('repository.admin') | |
135 | @CSRFRequired() |
|
135 | @CSRFRequired() | |
136 | @view_config( |
|
136 | @view_config( | |
137 | route_name='edit_repo_advanced_journal', request_method='POST', |
|
137 | route_name='edit_repo_advanced_journal', request_method='POST', | |
138 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') |
|
138 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') | |
139 | def edit_advanced_journal(self): |
|
139 | def edit_advanced_journal(self): | |
140 | """ |
|
140 | """ | |
141 | Set's this repository to be visible in public journal, |
|
141 | Set's this repository to be visible in public journal, | |
142 | in other words making default user to follow this repo |
|
142 | in other words making default user to follow this repo | |
143 | """ |
|
143 | """ | |
144 | _ = self.request.translate |
|
144 | _ = self.request.translate | |
145 |
|
145 | |||
146 | try: |
|
146 | try: | |
147 | user_id = User.get_default_user().user_id |
|
147 | user_id = User.get_default_user().user_id | |
148 | ScmModel().toggle_following_repo(self.db_repo.repo_id, user_id) |
|
148 | ScmModel().toggle_following_repo(self.db_repo.repo_id, user_id) | |
149 | h.flash(_('Updated repository visibility in public journal'), |
|
149 | h.flash(_('Updated repository visibility in public journal'), | |
150 | category='success') |
|
150 | category='success') | |
151 | Session().commit() |
|
151 | Session().commit() | |
152 | except Exception: |
|
152 | except Exception: | |
153 | h.flash(_('An error occurred during setting this ' |
|
153 | h.flash(_('An error occurred during setting this ' | |
154 | 'repository in public journal'), |
|
154 | 'repository in public journal'), | |
155 | category='error') |
|
155 | category='error') | |
156 |
|
156 | |||
157 | raise HTTPFound( |
|
157 | raise HTTPFound( | |
158 | h.route_path('edit_repo_advanced', repo_name=self.db_repo_name)) |
|
158 | h.route_path('edit_repo_advanced', repo_name=self.db_repo_name)) | |
159 |
|
159 | |||
160 | @LoginRequired() |
|
160 | @LoginRequired() | |
161 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
161 | @HasRepoPermissionAnyDecorator('repository.admin') | |
162 | @CSRFRequired() |
|
162 | @CSRFRequired() | |
163 | @view_config( |
|
163 | @view_config( | |
164 | route_name='edit_repo_advanced_fork', request_method='POST', |
|
164 | route_name='edit_repo_advanced_fork', request_method='POST', | |
165 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') |
|
165 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') | |
166 | def edit_advanced_fork(self): |
|
166 | def edit_advanced_fork(self): | |
167 | """ |
|
167 | """ | |
168 | Mark given repository as a fork of another |
|
168 | Mark given repository as a fork of another | |
169 | """ |
|
169 | """ | |
170 | _ = self.request.translate |
|
170 | _ = self.request.translate | |
171 |
|
171 | |||
172 | new_fork_id = self.request.POST.get('id_fork_of') |
|
172 | new_fork_id = self.request.POST.get('id_fork_of') | |
173 | try: |
|
173 | try: | |
174 |
|
174 | |||
175 | if new_fork_id and not new_fork_id.isdigit(): |
|
175 | if new_fork_id and not new_fork_id.isdigit(): | |
176 | log.error('Given fork id %s is not an INT', new_fork_id) |
|
176 | log.error('Given fork id %s is not an INT', new_fork_id) | |
177 |
|
177 | |||
178 | fork_id = safe_int(new_fork_id) |
|
178 | fork_id = safe_int(new_fork_id) | |
179 | repo = ScmModel().mark_as_fork( |
|
179 | repo = ScmModel().mark_as_fork( | |
180 | self.db_repo_name, fork_id, self._rhodecode_user.user_id) |
|
180 | self.db_repo_name, fork_id, self._rhodecode_user.user_id) | |
181 | fork = repo.fork.repo_name if repo.fork else _('Nothing') |
|
181 | fork = repo.fork.repo_name if repo.fork else _('Nothing') | |
182 | Session().commit() |
|
182 | Session().commit() | |
183 | h.flash(_('Marked repo %s as fork of %s') % (self.db_repo_name, fork), |
|
183 | h.flash(_('Marked repo %s as fork of %s') % (self.db_repo_name, fork), | |
184 | category='success') |
|
184 | category='success') | |
185 | except RepositoryError as e: |
|
185 | except RepositoryError as e: | |
186 | log.exception("Repository Error occurred") |
|
186 | log.exception("Repository Error occurred") | |
187 | h.flash(str(e), category='error') |
|
187 | h.flash(str(e), category='error') | |
188 | except Exception as e: |
|
188 | except Exception as e: | |
189 | log.exception("Exception while editing fork") |
|
189 | log.exception("Exception while editing fork") | |
190 | h.flash(_('An error occurred during this operation'), |
|
190 | h.flash(_('An error occurred during this operation'), | |
191 | category='error') |
|
191 | category='error') | |
192 |
|
192 | |||
193 | raise HTTPFound( |
|
193 | raise HTTPFound( | |
194 | h.route_path('edit_repo_advanced', repo_name=self.db_repo_name)) |
|
194 | h.route_path('edit_repo_advanced', repo_name=self.db_repo_name)) | |
195 |
|
195 | |||
196 | @LoginRequired() |
|
196 | @LoginRequired() | |
197 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
197 | @HasRepoPermissionAnyDecorator('repository.admin') | |
198 | @CSRFRequired() |
|
198 | @CSRFRequired() | |
199 | @view_config( |
|
199 | @view_config( | |
200 | route_name='edit_repo_advanced_locking', request_method='POST', |
|
200 | route_name='edit_repo_advanced_locking', request_method='POST', | |
201 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') |
|
201 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') | |
202 | def edit_advanced_locking(self): |
|
202 | def edit_advanced_locking(self): | |
203 | """ |
|
203 | """ | |
204 | Toggle locking of repository |
|
204 | Toggle locking of repository | |
205 | """ |
|
205 | """ | |
206 | _ = self.request.translate |
|
206 | _ = self.request.translate | |
207 | set_lock = self.request.POST.get('set_lock') |
|
207 | set_lock = self.request.POST.get('set_lock') | |
208 | set_unlock = self.request.POST.get('set_unlock') |
|
208 | set_unlock = self.request.POST.get('set_unlock') | |
209 |
|
209 | |||
210 | try: |
|
210 | try: | |
211 | if set_lock: |
|
211 | if set_lock: | |
212 | Repository.lock(self.db_repo, self._rhodecode_user.user_id, |
|
212 | Repository.lock(self.db_repo, self._rhodecode_user.user_id, | |
213 | lock_reason=Repository.LOCK_WEB) |
|
213 | lock_reason=Repository.LOCK_WEB) | |
214 | h.flash(_('Locked repository'), category='success') |
|
214 | h.flash(_('Locked repository'), category='success') | |
215 | elif set_unlock: |
|
215 | elif set_unlock: | |
216 | Repository.unlock(self.db_repo) |
|
216 | Repository.unlock(self.db_repo) | |
217 | h.flash(_('Unlocked repository'), category='success') |
|
217 | h.flash(_('Unlocked repository'), category='success') | |
218 | except Exception as e: |
|
218 | except Exception as e: | |
219 | log.exception("Exception during unlocking") |
|
219 | log.exception("Exception during unlocking") | |
220 | h.flash(_('An error occurred during unlocking'), category='error') |
|
220 | h.flash(_('An error occurred during unlocking'), category='error') | |
221 |
|
221 | |||
222 | raise HTTPFound( |
|
222 | raise HTTPFound( | |
223 | h.route_path('edit_repo_advanced', repo_name=self.db_repo_name)) |
|
223 | h.route_path('edit_repo_advanced', repo_name=self.db_repo_name)) |
General Comments 0
You need to be logged in to leave comments.
Login now