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