Show More
@@ -1,715 +1,716 b'' | |||
|
1 | 1 | # Copyright (C) 2010-2023 RhodeCode GmbH |
|
2 | 2 | # |
|
3 | 3 | # This program is free software: you can redistribute it and/or modify |
|
4 | 4 | # it under the terms of the GNU Affero General Public License, version 3 |
|
5 | 5 | # (only), as published by the Free Software Foundation. |
|
6 | 6 | # |
|
7 | 7 | # This program is distributed in the hope that it will be useful, |
|
8 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 | 10 | # GNU General Public License for more details. |
|
11 | 11 | # |
|
12 | 12 | # You should have received a copy of the GNU Affero General Public License |
|
13 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
14 | 14 | # |
|
15 | 15 | # This program is dual-licensed. If you wish to learn more about the |
|
16 | 16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
17 | 17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
18 | 18 | |
|
19 | 19 | |
|
20 | 20 | import logging |
|
21 | 21 | import collections |
|
22 | 22 | |
|
23 | 23 | import datetime |
|
24 | 24 | import formencode |
|
25 | 25 | import formencode.htmlfill |
|
26 | 26 | |
|
27 | 27 | import rhodecode |
|
28 | 28 | |
|
29 | 29 | from pyramid.httpexceptions import HTTPFound, HTTPNotFound |
|
30 | 30 | from pyramid.renderers import render |
|
31 | 31 | from pyramid.response import Response |
|
32 | 32 | |
|
33 | 33 | from rhodecode.apps._base import BaseAppView |
|
34 | 34 | from rhodecode.apps._base.navigation import navigation_list |
|
35 | 35 | from rhodecode.apps.svn_support import config_keys |
|
36 | 36 | from rhodecode.lib import helpers as h |
|
37 | 37 | from rhodecode.lib.auth import ( |
|
38 | 38 | LoginRequired, HasPermissionAllDecorator, CSRFRequired) |
|
39 | 39 | from rhodecode.lib.celerylib import tasks, run_task |
|
40 | 40 | from rhodecode.lib.str_utils import safe_str |
|
41 | 41 | from rhodecode.lib.utils import repo2db_mapper, get_rhodecode_repo_store_path |
|
42 | 42 | from rhodecode.lib.utils2 import str2bool, AttributeDict |
|
43 | 43 | from rhodecode.lib.index import searcher_from_config |
|
44 | 44 | |
|
45 | 45 | from rhodecode.model.db import RhodeCodeUi, Repository |
|
46 | 46 | from rhodecode.model.forms import (ApplicationSettingsForm, |
|
47 | 47 | ApplicationUiSettingsForm, ApplicationVisualisationForm, |
|
48 | 48 | LabsSettingsForm, IssueTrackerPatternsForm) |
|
49 | 49 | from rhodecode.model.permission import PermissionModel |
|
50 | 50 | from rhodecode.model.repo_group import RepoGroupModel |
|
51 | 51 | |
|
52 | 52 | from rhodecode.model.scm import ScmModel |
|
53 | 53 | from rhodecode.model.notification import EmailNotificationModel |
|
54 | 54 | from rhodecode.model.meta import Session |
|
55 | 55 | from rhodecode.model.settings import ( |
|
56 | 56 | IssueTrackerSettingsModel, VcsSettingsModel, SettingNotFound, |
|
57 | 57 | SettingsModel) |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | log = logging.getLogger(__name__) |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | class AdminSettingsView(BaseAppView): |
|
64 | 64 | |
|
65 | 65 | def load_default_context(self): |
|
66 | 66 | c = self._get_local_tmpl_context() |
|
67 | 67 | c.labs_active = str2bool( |
|
68 | 68 | rhodecode.CONFIG.get('labs_settings_active', 'true')) |
|
69 | 69 | c.navlist = navigation_list(self.request) |
|
70 | 70 | return c |
|
71 | 71 | |
|
72 | 72 | @classmethod |
|
73 | 73 | def _get_ui_settings(cls): |
|
74 | 74 | ret = RhodeCodeUi.query().all() |
|
75 | 75 | |
|
76 | 76 | if not ret: |
|
77 | 77 | raise Exception('Could not get application ui settings !') |
|
78 | 78 | settings = { |
|
79 | 79 | # legacy param that needs to be kept |
|
80 | 'web_push_ssl': False | |
|
80 | 'web_push_ssl': False, | |
|
81 | 'extensions_hgsubversion': False | |
|
81 | 82 | } |
|
82 | 83 | for each in ret: |
|
83 | 84 | k = each.ui_key |
|
84 | 85 | v = each.ui_value |
|
86 | section = each.ui_section | |
|
87 | ||
|
85 | 88 | # skip some options if they are defined |
|
86 | if k in ['push_ssl']: | |
|
89 | if f"{section}_{k}" in ['web_push_ssl', 'extensions_hgsubversion']: | |
|
87 | 90 | continue |
|
88 | 91 | |
|
89 | 92 | if k == '/': |
|
90 | 93 | k = 'root_path' |
|
91 | 94 | |
|
92 | 95 | if k in ['publish', 'enabled']: |
|
93 | 96 | v = str2bool(v) |
|
94 | 97 | |
|
95 | 98 | if k.find('.') != -1: |
|
96 | 99 | k = k.replace('.', '_') |
|
97 | 100 | |
|
98 | 101 | if each.ui_section in ['hooks', 'extensions']: |
|
99 | 102 | v = each.ui_active |
|
100 | 103 | |
|
101 |
settings[ |
|
|
104 | settings[section + '_' + k] = v | |
|
102 | 105 | |
|
103 | 106 | return settings |
|
104 | 107 | |
|
105 | 108 | @classmethod |
|
106 | 109 | def _form_defaults(cls): |
|
107 | 110 | defaults = SettingsModel().get_all_settings() |
|
108 | 111 | defaults.update(cls._get_ui_settings()) |
|
109 | 112 | |
|
110 | 113 | defaults.update({ |
|
111 | 114 | 'new_svn_branch': '', |
|
112 | 115 | 'new_svn_tag': '', |
|
113 | 116 | }) |
|
114 | 117 | return defaults |
|
115 | 118 | |
|
116 | 119 | @LoginRequired() |
|
117 | 120 | @HasPermissionAllDecorator('hg.admin') |
|
118 | 121 | def settings_vcs(self): |
|
119 | 122 | c = self.load_default_context() |
|
120 | 123 | c.active = 'vcs' |
|
121 | 124 | model = VcsSettingsModel() |
|
122 | 125 | c.svn_branch_patterns = model.get_global_svn_branch_patterns() |
|
123 | 126 | c.svn_tag_patterns = model.get_global_svn_tag_patterns() |
|
124 | 127 | c.svn_generate_config = rhodecode.ConfigGet().get_bool(config_keys.generate_config) |
|
125 | 128 | c.svn_config_path = rhodecode.ConfigGet().get_str(config_keys.config_file_path) |
|
126 | 129 | defaults = self._form_defaults() |
|
127 | 130 | |
|
128 | model.create_largeobjects_dirs_if_needed(defaults['paths_root_path']) | |
|
129 | ||
|
130 | 131 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
131 | 132 | self._get_template_context(c), self.request) |
|
132 | 133 | html = formencode.htmlfill.render( |
|
133 | 134 | data, |
|
134 | 135 | defaults=defaults, |
|
135 | 136 | encoding="UTF-8", |
|
136 | 137 | force_defaults=False |
|
137 | 138 | ) |
|
138 | 139 | return Response(html) |
|
139 | 140 | |
|
140 | 141 | @LoginRequired() |
|
141 | 142 | @HasPermissionAllDecorator('hg.admin') |
|
142 | 143 | @CSRFRequired() |
|
143 | 144 | def settings_vcs_update(self): |
|
144 | 145 | _ = self.request.translate |
|
145 | 146 | c = self.load_default_context() |
|
146 | 147 | c.active = 'vcs' |
|
147 | 148 | |
|
148 | 149 | model = VcsSettingsModel() |
|
149 | 150 | c.svn_branch_patterns = model.get_global_svn_branch_patterns() |
|
150 | 151 | c.svn_tag_patterns = model.get_global_svn_tag_patterns() |
|
151 | 152 | |
|
152 | 153 | c.svn_generate_config = rhodecode.ConfigGet().get_bool(config_keys.generate_config) |
|
153 | 154 | c.svn_config_path = rhodecode.ConfigGet().get_str(config_keys.config_file_path) |
|
154 | 155 | application_form = ApplicationUiSettingsForm(self.request.translate)() |
|
155 | 156 | |
|
156 | 157 | try: |
|
157 | 158 | form_result = application_form.to_python(dict(self.request.POST)) |
|
158 | 159 | except formencode.Invalid as errors: |
|
159 | 160 | h.flash( |
|
160 | 161 | _("Some form inputs contain invalid data."), |
|
161 | 162 | category='error') |
|
162 | 163 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
163 | 164 | self._get_template_context(c), self.request) |
|
164 | 165 | html = formencode.htmlfill.render( |
|
165 | 166 | data, |
|
166 | 167 | defaults=errors.value, |
|
167 | 168 | errors=errors.unpack_errors() or {}, |
|
168 | 169 | prefix_error=False, |
|
169 | 170 | encoding="UTF-8", |
|
170 | 171 | force_defaults=False |
|
171 | 172 | ) |
|
172 | 173 | return Response(html) |
|
173 | 174 | |
|
174 | 175 | try: |
|
175 | 176 | model.update_global_hook_settings(form_result) |
|
176 | 177 | |
|
177 | 178 | model.create_or_update_global_svn_settings(form_result) |
|
178 | 179 | model.create_or_update_global_hg_settings(form_result) |
|
179 | 180 | model.create_or_update_global_git_settings(form_result) |
|
180 | 181 | model.create_or_update_global_pr_settings(form_result) |
|
181 | 182 | except Exception: |
|
182 | 183 | log.exception("Exception while updating settings") |
|
183 | 184 | h.flash(_('Error occurred during updating ' |
|
184 | 185 | 'application settings'), category='error') |
|
185 | 186 | else: |
|
186 | 187 | Session().commit() |
|
187 | 188 | h.flash(_('Updated VCS settings'), category='success') |
|
188 | 189 | raise HTTPFound(h.route_path('admin_settings_vcs')) |
|
189 | 190 | |
|
190 | 191 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
191 | 192 | self._get_template_context(c), self.request) |
|
192 | 193 | html = formencode.htmlfill.render( |
|
193 | 194 | data, |
|
194 | 195 | defaults=self._form_defaults(), |
|
195 | 196 | encoding="UTF-8", |
|
196 | 197 | force_defaults=False |
|
197 | 198 | ) |
|
198 | 199 | return Response(html) |
|
199 | 200 | |
|
200 | 201 | @LoginRequired() |
|
201 | 202 | @HasPermissionAllDecorator('hg.admin') |
|
202 | 203 | @CSRFRequired() |
|
203 | 204 | def settings_vcs_delete_svn_pattern(self): |
|
204 | 205 | delete_pattern_id = self.request.POST.get('delete_svn_pattern') |
|
205 | 206 | model = VcsSettingsModel() |
|
206 | 207 | try: |
|
207 | 208 | model.delete_global_svn_pattern(delete_pattern_id) |
|
208 | 209 | except SettingNotFound: |
|
209 | 210 | log.exception( |
|
210 | 211 | 'Failed to delete svn_pattern with id %s', delete_pattern_id) |
|
211 | 212 | raise HTTPNotFound() |
|
212 | 213 | |
|
213 | 214 | Session().commit() |
|
214 | 215 | return True |
|
215 | 216 | |
|
216 | 217 | @LoginRequired() |
|
217 | 218 | @HasPermissionAllDecorator('hg.admin') |
|
218 | 219 | def settings_mapping(self): |
|
219 | 220 | c = self.load_default_context() |
|
220 | 221 | c.active = 'mapping' |
|
221 | 222 | c.storage_path = get_rhodecode_repo_store_path() |
|
222 | 223 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
223 | 224 | self._get_template_context(c), self.request) |
|
224 | 225 | html = formencode.htmlfill.render( |
|
225 | 226 | data, |
|
226 | 227 | defaults=self._form_defaults(), |
|
227 | 228 | encoding="UTF-8", |
|
228 | 229 | force_defaults=False |
|
229 | 230 | ) |
|
230 | 231 | return Response(html) |
|
231 | 232 | |
|
232 | 233 | @LoginRequired() |
|
233 | 234 | @HasPermissionAllDecorator('hg.admin') |
|
234 | 235 | @CSRFRequired() |
|
235 | 236 | def settings_mapping_update(self): |
|
236 | 237 | _ = self.request.translate |
|
237 | 238 | c = self.load_default_context() |
|
238 | 239 | c.active = 'mapping' |
|
239 | 240 | rm_obsolete = self.request.POST.get('destroy', False) |
|
240 | 241 | invalidate_cache = self.request.POST.get('invalidate', False) |
|
241 | 242 | log.debug('rescanning repo location with destroy obsolete=%s', rm_obsolete) |
|
242 | 243 | |
|
243 | 244 | if invalidate_cache: |
|
244 | 245 | log.debug('invalidating all repositories cache') |
|
245 | 246 | for repo in Repository.get_all(): |
|
246 | 247 | ScmModel().mark_for_invalidation(repo.repo_name, delete=True) |
|
247 | 248 | |
|
248 | 249 | filesystem_repos = ScmModel().repo_scan() |
|
249 | 250 | added, removed = repo2db_mapper(filesystem_repos, rm_obsolete, force_hooks_rebuild=True) |
|
250 | 251 | PermissionModel().trigger_permission_flush() |
|
251 | 252 | |
|
252 | 253 | def _repr(rm_repo): |
|
253 | 254 | return ', '.join(map(safe_str, rm_repo)) or '-' |
|
254 | 255 | |
|
255 | 256 | h.flash(_('Repositories successfully ' |
|
256 | 257 | 'rescanned added: %s ; removed: %s') % |
|
257 | 258 | (_repr(added), _repr(removed)), |
|
258 | 259 | category='success') |
|
259 | 260 | raise HTTPFound(h.route_path('admin_settings_mapping')) |
|
260 | 261 | |
|
261 | 262 | @LoginRequired() |
|
262 | 263 | @HasPermissionAllDecorator('hg.admin') |
|
263 | 264 | def settings_global(self): |
|
264 | 265 | c = self.load_default_context() |
|
265 | 266 | c.active = 'global' |
|
266 | 267 | c.personal_repo_group_default_pattern = RepoGroupModel()\ |
|
267 | 268 | .get_personal_group_name_pattern() |
|
268 | 269 | |
|
269 | 270 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
270 | 271 | self._get_template_context(c), self.request) |
|
271 | 272 | html = formencode.htmlfill.render( |
|
272 | 273 | data, |
|
273 | 274 | defaults=self._form_defaults(), |
|
274 | 275 | encoding="UTF-8", |
|
275 | 276 | force_defaults=False |
|
276 | 277 | ) |
|
277 | 278 | return Response(html) |
|
278 | 279 | |
|
279 | 280 | @LoginRequired() |
|
280 | 281 | @HasPermissionAllDecorator('hg.admin') |
|
281 | 282 | @CSRFRequired() |
|
282 | 283 | def settings_global_update(self): |
|
283 | 284 | _ = self.request.translate |
|
284 | 285 | c = self.load_default_context() |
|
285 | 286 | c.active = 'global' |
|
286 | 287 | c.personal_repo_group_default_pattern = RepoGroupModel()\ |
|
287 | 288 | .get_personal_group_name_pattern() |
|
288 | 289 | application_form = ApplicationSettingsForm(self.request.translate)() |
|
289 | 290 | try: |
|
290 | 291 | form_result = application_form.to_python(dict(self.request.POST)) |
|
291 | 292 | except formencode.Invalid as errors: |
|
292 | 293 | h.flash( |
|
293 | 294 | _("Some form inputs contain invalid data."), |
|
294 | 295 | category='error') |
|
295 | 296 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
296 | 297 | self._get_template_context(c), self.request) |
|
297 | 298 | html = formencode.htmlfill.render( |
|
298 | 299 | data, |
|
299 | 300 | defaults=errors.value, |
|
300 | 301 | errors=errors.unpack_errors() or {}, |
|
301 | 302 | prefix_error=False, |
|
302 | 303 | encoding="UTF-8", |
|
303 | 304 | force_defaults=False |
|
304 | 305 | ) |
|
305 | 306 | return Response(html) |
|
306 | 307 | |
|
307 | 308 | settings = [ |
|
308 | 309 | ('title', 'rhodecode_title', 'unicode'), |
|
309 | 310 | ('realm', 'rhodecode_realm', 'unicode'), |
|
310 | 311 | ('pre_code', 'rhodecode_pre_code', 'unicode'), |
|
311 | 312 | ('post_code', 'rhodecode_post_code', 'unicode'), |
|
312 | 313 | ('captcha_public_key', 'rhodecode_captcha_public_key', 'unicode'), |
|
313 | 314 | ('captcha_private_key', 'rhodecode_captcha_private_key', 'unicode'), |
|
314 | 315 | ('create_personal_repo_group', 'rhodecode_create_personal_repo_group', 'bool'), |
|
315 | 316 | ('personal_repo_group_pattern', 'rhodecode_personal_repo_group_pattern', 'unicode'), |
|
316 | 317 | ] |
|
317 | 318 | |
|
318 | 319 | try: |
|
319 | 320 | for setting, form_key, type_ in settings: |
|
320 | 321 | sett = SettingsModel().create_or_update_setting( |
|
321 | 322 | setting, form_result[form_key], type_) |
|
322 | 323 | Session().add(sett) |
|
323 | 324 | |
|
324 | 325 | Session().commit() |
|
325 | 326 | SettingsModel().invalidate_settings_cache() |
|
326 | 327 | h.flash(_('Updated application settings'), category='success') |
|
327 | 328 | except Exception: |
|
328 | 329 | log.exception("Exception while updating application settings") |
|
329 | 330 | h.flash( |
|
330 | 331 | _('Error occurred during updating application settings'), |
|
331 | 332 | category='error') |
|
332 | 333 | |
|
333 | 334 | raise HTTPFound(h.route_path('admin_settings_global')) |
|
334 | 335 | |
|
335 | 336 | @LoginRequired() |
|
336 | 337 | @HasPermissionAllDecorator('hg.admin') |
|
337 | 338 | def settings_visual(self): |
|
338 | 339 | c = self.load_default_context() |
|
339 | 340 | c.active = 'visual' |
|
340 | 341 | |
|
341 | 342 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
342 | 343 | self._get_template_context(c), self.request) |
|
343 | 344 | html = formencode.htmlfill.render( |
|
344 | 345 | data, |
|
345 | 346 | defaults=self._form_defaults(), |
|
346 | 347 | encoding="UTF-8", |
|
347 | 348 | force_defaults=False |
|
348 | 349 | ) |
|
349 | 350 | return Response(html) |
|
350 | 351 | |
|
351 | 352 | @LoginRequired() |
|
352 | 353 | @HasPermissionAllDecorator('hg.admin') |
|
353 | 354 | @CSRFRequired() |
|
354 | 355 | def settings_visual_update(self): |
|
355 | 356 | _ = self.request.translate |
|
356 | 357 | c = self.load_default_context() |
|
357 | 358 | c.active = 'visual' |
|
358 | 359 | application_form = ApplicationVisualisationForm(self.request.translate)() |
|
359 | 360 | try: |
|
360 | 361 | form_result = application_form.to_python(dict(self.request.POST)) |
|
361 | 362 | except formencode.Invalid as errors: |
|
362 | 363 | h.flash( |
|
363 | 364 | _("Some form inputs contain invalid data."), |
|
364 | 365 | category='error') |
|
365 | 366 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
366 | 367 | self._get_template_context(c), self.request) |
|
367 | 368 | html = formencode.htmlfill.render( |
|
368 | 369 | data, |
|
369 | 370 | defaults=errors.value, |
|
370 | 371 | errors=errors.unpack_errors() or {}, |
|
371 | 372 | prefix_error=False, |
|
372 | 373 | encoding="UTF-8", |
|
373 | 374 | force_defaults=False |
|
374 | 375 | ) |
|
375 | 376 | return Response(html) |
|
376 | 377 | |
|
377 | 378 | try: |
|
378 | 379 | settings = [ |
|
379 | 380 | ('show_public_icon', 'rhodecode_show_public_icon', 'bool'), |
|
380 | 381 | ('show_private_icon', 'rhodecode_show_private_icon', 'bool'), |
|
381 | 382 | ('stylify_metatags', 'rhodecode_stylify_metatags', 'bool'), |
|
382 | 383 | ('repository_fields', 'rhodecode_repository_fields', 'bool'), |
|
383 | 384 | ('dashboard_items', 'rhodecode_dashboard_items', 'int'), |
|
384 | 385 | ('admin_grid_items', 'rhodecode_admin_grid_items', 'int'), |
|
385 | 386 | ('show_version', 'rhodecode_show_version', 'bool'), |
|
386 | 387 | ('use_gravatar', 'rhodecode_use_gravatar', 'bool'), |
|
387 | 388 | ('markup_renderer', 'rhodecode_markup_renderer', 'unicode'), |
|
388 | 389 | ('gravatar_url', 'rhodecode_gravatar_url', 'unicode'), |
|
389 | 390 | ('clone_uri_tmpl', 'rhodecode_clone_uri_tmpl', 'unicode'), |
|
390 | 391 | ('clone_uri_id_tmpl', 'rhodecode_clone_uri_id_tmpl', 'unicode'), |
|
391 | 392 | ('clone_uri_ssh_tmpl', 'rhodecode_clone_uri_ssh_tmpl', 'unicode'), |
|
392 | 393 | ('support_url', 'rhodecode_support_url', 'unicode'), |
|
393 | 394 | ('show_revision_number', 'rhodecode_show_revision_number', 'bool'), |
|
394 | 395 | ('show_sha_length', 'rhodecode_show_sha_length', 'int'), |
|
395 | 396 | ] |
|
396 | 397 | for setting, form_key, type_ in settings: |
|
397 | 398 | sett = SettingsModel().create_or_update_setting( |
|
398 | 399 | setting, form_result[form_key], type_) |
|
399 | 400 | Session().add(sett) |
|
400 | 401 | |
|
401 | 402 | Session().commit() |
|
402 | 403 | SettingsModel().invalidate_settings_cache() |
|
403 | 404 | h.flash(_('Updated visualisation settings'), category='success') |
|
404 | 405 | except Exception: |
|
405 | 406 | log.exception("Exception updating visualization settings") |
|
406 | 407 | h.flash(_('Error occurred during updating ' |
|
407 | 408 | 'visualisation settings'), |
|
408 | 409 | category='error') |
|
409 | 410 | |
|
410 | 411 | raise HTTPFound(h.route_path('admin_settings_visual')) |
|
411 | 412 | |
|
412 | 413 | @LoginRequired() |
|
413 | 414 | @HasPermissionAllDecorator('hg.admin') |
|
414 | 415 | def settings_issuetracker(self): |
|
415 | 416 | c = self.load_default_context() |
|
416 | 417 | c.active = 'issuetracker' |
|
417 | 418 | defaults = c.rc_config |
|
418 | 419 | |
|
419 | 420 | entry_key = 'rhodecode_issuetracker_pat_' |
|
420 | 421 | |
|
421 | 422 | c.issuetracker_entries = {} |
|
422 | 423 | for k, v in defaults.items(): |
|
423 | 424 | if k.startswith(entry_key): |
|
424 | 425 | uid = k[len(entry_key):] |
|
425 | 426 | c.issuetracker_entries[uid] = None |
|
426 | 427 | |
|
427 | 428 | for uid in c.issuetracker_entries: |
|
428 | 429 | c.issuetracker_entries[uid] = AttributeDict({ |
|
429 | 430 | 'pat': defaults.get('rhodecode_issuetracker_pat_' + uid), |
|
430 | 431 | 'url': defaults.get('rhodecode_issuetracker_url_' + uid), |
|
431 | 432 | 'pref': defaults.get('rhodecode_issuetracker_pref_' + uid), |
|
432 | 433 | 'desc': defaults.get('rhodecode_issuetracker_desc_' + uid), |
|
433 | 434 | }) |
|
434 | 435 | |
|
435 | 436 | return self._get_template_context(c) |
|
436 | 437 | |
|
437 | 438 | @LoginRequired() |
|
438 | 439 | @HasPermissionAllDecorator('hg.admin') |
|
439 | 440 | @CSRFRequired() |
|
440 | 441 | def settings_issuetracker_test(self): |
|
441 | 442 | error_container = [] |
|
442 | 443 | |
|
443 | 444 | urlified_commit = h.urlify_commit_message( |
|
444 | 445 | self.request.POST.get('test_text', ''), |
|
445 | 446 | 'repo_group/test_repo1', error_container=error_container) |
|
446 | 447 | if error_container: |
|
447 | 448 | def converter(inp): |
|
448 | 449 | return h.html_escape(inp) |
|
449 | 450 | |
|
450 | 451 | return 'ERRORS: ' + '\n'.join(map(converter, error_container)) |
|
451 | 452 | |
|
452 | 453 | return urlified_commit |
|
453 | 454 | |
|
454 | 455 | @LoginRequired() |
|
455 | 456 | @HasPermissionAllDecorator('hg.admin') |
|
456 | 457 | @CSRFRequired() |
|
457 | 458 | def settings_issuetracker_update(self): |
|
458 | 459 | _ = self.request.translate |
|
459 | 460 | self.load_default_context() |
|
460 | 461 | settings_model = IssueTrackerSettingsModel() |
|
461 | 462 | |
|
462 | 463 | try: |
|
463 | 464 | form = IssueTrackerPatternsForm(self.request.translate)() |
|
464 | 465 | data = form.to_python(self.request.POST) |
|
465 | 466 | except formencode.Invalid as errors: |
|
466 | 467 | log.exception('Failed to add new pattern') |
|
467 | 468 | error = errors |
|
468 | 469 | h.flash(_(f'Invalid issue tracker pattern: {error}'), |
|
469 | 470 | category='error') |
|
470 | 471 | raise HTTPFound(h.route_path('admin_settings_issuetracker')) |
|
471 | 472 | |
|
472 | 473 | if data: |
|
473 | 474 | for uid in data.get('delete_patterns', []): |
|
474 | 475 | settings_model.delete_entries(uid) |
|
475 | 476 | |
|
476 | 477 | for pattern in data.get('patterns', []): |
|
477 | 478 | for setting, value, type_ in pattern: |
|
478 | 479 | sett = settings_model.create_or_update_setting( |
|
479 | 480 | setting, value, type_) |
|
480 | 481 | Session().add(sett) |
|
481 | 482 | |
|
482 | 483 | Session().commit() |
|
483 | 484 | |
|
484 | 485 | SettingsModel().invalidate_settings_cache() |
|
485 | 486 | h.flash(_('Updated issue tracker entries'), category='success') |
|
486 | 487 | raise HTTPFound(h.route_path('admin_settings_issuetracker')) |
|
487 | 488 | |
|
488 | 489 | @LoginRequired() |
|
489 | 490 | @HasPermissionAllDecorator('hg.admin') |
|
490 | 491 | @CSRFRequired() |
|
491 | 492 | def settings_issuetracker_delete(self): |
|
492 | 493 | _ = self.request.translate |
|
493 | 494 | self.load_default_context() |
|
494 | 495 | uid = self.request.POST.get('uid') |
|
495 | 496 | try: |
|
496 | 497 | IssueTrackerSettingsModel().delete_entries(uid) |
|
497 | 498 | except Exception: |
|
498 | 499 | log.exception('Failed to delete issue tracker setting %s', uid) |
|
499 | 500 | raise HTTPNotFound() |
|
500 | 501 | |
|
501 | 502 | SettingsModel().invalidate_settings_cache() |
|
502 | 503 | h.flash(_('Removed issue tracker entry.'), category='success') |
|
503 | 504 | |
|
504 | 505 | return {'deleted': uid} |
|
505 | 506 | |
|
506 | 507 | @LoginRequired() |
|
507 | 508 | @HasPermissionAllDecorator('hg.admin') |
|
508 | 509 | def settings_email(self): |
|
509 | 510 | c = self.load_default_context() |
|
510 | 511 | c.active = 'email' |
|
511 | 512 | c.rhodecode_ini = rhodecode.CONFIG |
|
512 | 513 | |
|
513 | 514 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
514 | 515 | self._get_template_context(c), self.request) |
|
515 | 516 | html = formencode.htmlfill.render( |
|
516 | 517 | data, |
|
517 | 518 | defaults=self._form_defaults(), |
|
518 | 519 | encoding="UTF-8", |
|
519 | 520 | force_defaults=False |
|
520 | 521 | ) |
|
521 | 522 | return Response(html) |
|
522 | 523 | |
|
523 | 524 | @LoginRequired() |
|
524 | 525 | @HasPermissionAllDecorator('hg.admin') |
|
525 | 526 | @CSRFRequired() |
|
526 | 527 | def settings_email_update(self): |
|
527 | 528 | _ = self.request.translate |
|
528 | 529 | c = self.load_default_context() |
|
529 | 530 | c.active = 'email' |
|
530 | 531 | |
|
531 | 532 | test_email = self.request.POST.get('test_email') |
|
532 | 533 | |
|
533 | 534 | if not test_email: |
|
534 | 535 | h.flash(_('Please enter email address'), category='error') |
|
535 | 536 | raise HTTPFound(h.route_path('admin_settings_email')) |
|
536 | 537 | |
|
537 | 538 | email_kwargs = { |
|
538 | 539 | 'date': datetime.datetime.now(), |
|
539 | 540 | 'user': self._rhodecode_db_user |
|
540 | 541 | } |
|
541 | 542 | |
|
542 | 543 | (subject, email_body, email_body_plaintext) = EmailNotificationModel().render_email( |
|
543 | 544 | EmailNotificationModel.TYPE_EMAIL_TEST, **email_kwargs) |
|
544 | 545 | |
|
545 | 546 | recipients = [test_email] if test_email else None |
|
546 | 547 | |
|
547 | 548 | run_task(tasks.send_email, recipients, subject, |
|
548 | 549 | email_body_plaintext, email_body) |
|
549 | 550 | |
|
550 | 551 | h.flash(_('Send email task created'), category='success') |
|
551 | 552 | raise HTTPFound(h.route_path('admin_settings_email')) |
|
552 | 553 | |
|
553 | 554 | @LoginRequired() |
|
554 | 555 | @HasPermissionAllDecorator('hg.admin') |
|
555 | 556 | def settings_hooks(self): |
|
556 | 557 | c = self.load_default_context() |
|
557 | 558 | c.active = 'hooks' |
|
558 | 559 | |
|
559 | 560 | model = SettingsModel() |
|
560 | 561 | c.hooks = model.get_builtin_hooks() |
|
561 | 562 | c.custom_hooks = model.get_custom_hooks() |
|
562 | 563 | |
|
563 | 564 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
564 | 565 | self._get_template_context(c), self.request) |
|
565 | 566 | html = formencode.htmlfill.render( |
|
566 | 567 | data, |
|
567 | 568 | defaults=self._form_defaults(), |
|
568 | 569 | encoding="UTF-8", |
|
569 | 570 | force_defaults=False |
|
570 | 571 | ) |
|
571 | 572 | return Response(html) |
|
572 | 573 | |
|
573 | 574 | @LoginRequired() |
|
574 | 575 | @HasPermissionAllDecorator('hg.admin') |
|
575 | 576 | @CSRFRequired() |
|
576 | 577 | def settings_hooks_update(self): |
|
577 | 578 | _ = self.request.translate |
|
578 | 579 | c = self.load_default_context() |
|
579 | 580 | c.active = 'hooks' |
|
580 | 581 | if c.visual.allow_custom_hooks_settings: |
|
581 | 582 | ui_key = self.request.POST.get('new_hook_ui_key') |
|
582 | 583 | ui_value = self.request.POST.get('new_hook_ui_value') |
|
583 | 584 | |
|
584 | 585 | hook_id = self.request.POST.get('hook_id') |
|
585 | 586 | new_hook = False |
|
586 | 587 | |
|
587 | 588 | model = SettingsModel() |
|
588 | 589 | try: |
|
589 | 590 | if ui_value and ui_key: |
|
590 | 591 | model.create_or_update_hook(ui_key, ui_value) |
|
591 | 592 | h.flash(_('Added new hook'), category='success') |
|
592 | 593 | new_hook = True |
|
593 | 594 | elif hook_id: |
|
594 | 595 | RhodeCodeUi.delete(hook_id) |
|
595 | 596 | Session().commit() |
|
596 | 597 | |
|
597 | 598 | # check for edits |
|
598 | 599 | update = False |
|
599 | 600 | _d = self.request.POST.dict_of_lists() |
|
600 | 601 | for k, v in zip(_d.get('hook_ui_key', []), |
|
601 | 602 | _d.get('hook_ui_value_new', [])): |
|
602 | 603 | model.create_or_update_hook(k, v) |
|
603 | 604 | update = True |
|
604 | 605 | |
|
605 | 606 | if update and not new_hook: |
|
606 | 607 | h.flash(_('Updated hooks'), category='success') |
|
607 | 608 | Session().commit() |
|
608 | 609 | except Exception: |
|
609 | 610 | log.exception("Exception during hook creation") |
|
610 | 611 | h.flash(_('Error occurred during hook creation'), |
|
611 | 612 | category='error') |
|
612 | 613 | |
|
613 | 614 | raise HTTPFound(h.route_path('admin_settings_hooks')) |
|
614 | 615 | |
|
615 | 616 | @LoginRequired() |
|
616 | 617 | @HasPermissionAllDecorator('hg.admin') |
|
617 | 618 | def settings_search(self): |
|
618 | 619 | c = self.load_default_context() |
|
619 | 620 | c.active = 'search' |
|
620 | 621 | |
|
621 | 622 | c.searcher = searcher_from_config(self.request.registry.settings) |
|
622 | 623 | c.statistics = c.searcher.statistics(self.request.translate) |
|
623 | 624 | |
|
624 | 625 | return self._get_template_context(c) |
|
625 | 626 | |
|
626 | 627 | @LoginRequired() |
|
627 | 628 | @HasPermissionAllDecorator('hg.admin') |
|
628 | 629 | def settings_labs(self): |
|
629 | 630 | c = self.load_default_context() |
|
630 | 631 | if not c.labs_active: |
|
631 | 632 | raise HTTPFound(h.route_path('admin_settings')) |
|
632 | 633 | |
|
633 | 634 | c.active = 'labs' |
|
634 | 635 | c.lab_settings = _LAB_SETTINGS |
|
635 | 636 | |
|
636 | 637 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
637 | 638 | self._get_template_context(c), self.request) |
|
638 | 639 | html = formencode.htmlfill.render( |
|
639 | 640 | data, |
|
640 | 641 | defaults=self._form_defaults(), |
|
641 | 642 | encoding="UTF-8", |
|
642 | 643 | force_defaults=False |
|
643 | 644 | ) |
|
644 | 645 | return Response(html) |
|
645 | 646 | |
|
646 | 647 | @LoginRequired() |
|
647 | 648 | @HasPermissionAllDecorator('hg.admin') |
|
648 | 649 | @CSRFRequired() |
|
649 | 650 | def settings_labs_update(self): |
|
650 | 651 | _ = self.request.translate |
|
651 | 652 | c = self.load_default_context() |
|
652 | 653 | c.active = 'labs' |
|
653 | 654 | |
|
654 | 655 | application_form = LabsSettingsForm(self.request.translate)() |
|
655 | 656 | try: |
|
656 | 657 | form_result = application_form.to_python(dict(self.request.POST)) |
|
657 | 658 | except formencode.Invalid as errors: |
|
658 | 659 | h.flash( |
|
659 | 660 | _("Some form inputs contain invalid data."), |
|
660 | 661 | category='error') |
|
661 | 662 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
662 | 663 | self._get_template_context(c), self.request) |
|
663 | 664 | html = formencode.htmlfill.render( |
|
664 | 665 | data, |
|
665 | 666 | defaults=errors.value, |
|
666 | 667 | errors=errors.unpack_errors() or {}, |
|
667 | 668 | prefix_error=False, |
|
668 | 669 | encoding="UTF-8", |
|
669 | 670 | force_defaults=False |
|
670 | 671 | ) |
|
671 | 672 | return Response(html) |
|
672 | 673 | |
|
673 | 674 | try: |
|
674 | 675 | session = Session() |
|
675 | 676 | for setting in _LAB_SETTINGS: |
|
676 | 677 | setting_name = setting.key[len('rhodecode_'):] |
|
677 | 678 | sett = SettingsModel().create_or_update_setting( |
|
678 | 679 | setting_name, form_result[setting.key], setting.type) |
|
679 | 680 | session.add(sett) |
|
680 | 681 | |
|
681 | 682 | except Exception: |
|
682 | 683 | log.exception('Exception while updating lab settings') |
|
683 | 684 | h.flash(_('Error occurred during updating labs settings'), |
|
684 | 685 | category='error') |
|
685 | 686 | else: |
|
686 | 687 | Session().commit() |
|
687 | 688 | SettingsModel().invalidate_settings_cache() |
|
688 | 689 | h.flash(_('Updated Labs settings'), category='success') |
|
689 | 690 | raise HTTPFound(h.route_path('admin_settings_labs')) |
|
690 | 691 | |
|
691 | 692 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
692 | 693 | self._get_template_context(c), self.request) |
|
693 | 694 | html = formencode.htmlfill.render( |
|
694 | 695 | data, |
|
695 | 696 | defaults=self._form_defaults(), |
|
696 | 697 | encoding="UTF-8", |
|
697 | 698 | force_defaults=False |
|
698 | 699 | ) |
|
699 | 700 | return Response(html) |
|
700 | 701 | |
|
701 | 702 | |
|
702 | 703 | # :param key: name of the setting including the 'rhodecode_' prefix |
|
703 | 704 | # :param type: the RhodeCodeSetting type to use. |
|
704 | 705 | # :param group: the i18ned group in which we should dispaly this setting |
|
705 | 706 | # :param label: the i18ned label we should display for this setting |
|
706 | 707 | # :param help: the i18ned help we should dispaly for this setting |
|
707 | 708 | LabSetting = collections.namedtuple( |
|
708 | 709 | 'LabSetting', ('key', 'type', 'group', 'label', 'help')) |
|
709 | 710 | |
|
710 | 711 | |
|
711 | 712 | # This list has to be kept in sync with the form |
|
712 | 713 | # rhodecode.model.forms.LabsSettingsForm. |
|
713 | 714 | _LAB_SETTINGS = [ |
|
714 | 715 | |
|
715 | 716 | ] |
General Comments 0
You need to be logged in to leave comments.
Login now