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