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