Show More
@@ -1,517 +1,517 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.controllers.admin.settings |
|
3 | rhodecode.controllers.admin.settings | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | settings controller for rhodecode admin |
|
6 | settings controller for rhodecode admin | |
7 |
|
7 | |||
8 | :created_on: Jul 14, 2010 |
|
8 | :created_on: Jul 14, 2010 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software: you can redistribute it and/or modify |
|
13 | # This program is free software: you can redistribute it and/or modify | |
14 | # it under the terms of the GNU General Public License as published by |
|
14 | # it under the terms of the GNU General Public License as published by | |
15 | # the Free Software Foundation, either version 3 of the License, or |
|
15 | # the Free Software Foundation, either version 3 of the License, or | |
16 | # (at your option) any later version. |
|
16 | # (at your option) any later version. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 |
|
25 | |||
26 | import logging |
|
26 | import logging | |
27 | import traceback |
|
27 | import traceback | |
28 | import formencode |
|
28 | import formencode | |
29 | import pkg_resources |
|
29 | import pkg_resources | |
30 | import platform |
|
30 | import platform | |
31 |
|
31 | |||
32 | from sqlalchemy import func |
|
32 | from sqlalchemy import func | |
33 | from formencode import htmlfill |
|
33 | from formencode import htmlfill | |
34 | from pylons import request, session, tmpl_context as c, url, config |
|
34 | from pylons import request, session, tmpl_context as c, url, config | |
35 | from pylons.controllers.util import abort, redirect |
|
35 | from pylons.controllers.util import abort, redirect | |
36 | from pylons.i18n.translation import _ |
|
36 | from pylons.i18n.translation import _ | |
37 |
|
37 | |||
38 | from rhodecode.lib import helpers as h |
|
38 | from rhodecode.lib import helpers as h | |
39 | from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \ |
|
39 | from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \ | |
40 | HasPermissionAnyDecorator, NotAnonymous |
|
40 | HasPermissionAnyDecorator, NotAnonymous | |
41 | from rhodecode.lib.base import BaseController, render |
|
41 | from rhodecode.lib.base import BaseController, render | |
42 | from rhodecode.lib.celerylib import tasks, run_task |
|
42 | from rhodecode.lib.celerylib import tasks, run_task | |
43 | from rhodecode.lib.utils import repo2db_mapper, invalidate_cache, \ |
|
43 | from rhodecode.lib.utils import repo2db_mapper, invalidate_cache, \ | |
44 | set_rhodecode_config, repo_name_slug, check_git_version |
|
44 | set_rhodecode_config, repo_name_slug, check_git_version | |
45 | from rhodecode.model.db import RhodeCodeUi, Repository, RepoGroup, \ |
|
45 | from rhodecode.model.db import RhodeCodeUi, Repository, RepoGroup, \ | |
46 | RhodeCodeSetting, PullRequest, PullRequestReviewers |
|
46 | RhodeCodeSetting, PullRequest, PullRequestReviewers | |
47 | from rhodecode.model.forms import UserForm, ApplicationSettingsForm, \ |
|
47 | from rhodecode.model.forms import UserForm, ApplicationSettingsForm, \ | |
48 | ApplicationUiSettingsForm, ApplicationVisualisationForm |
|
48 | ApplicationUiSettingsForm, ApplicationVisualisationForm | |
49 | from rhodecode.model.scm import ScmModel |
|
49 | from rhodecode.model.scm import ScmModel | |
50 | from rhodecode.model.user import UserModel |
|
50 | from rhodecode.model.user import UserModel | |
51 | from rhodecode.model.db import User |
|
51 | from rhodecode.model.db import User | |
52 | from rhodecode.model.notification import EmailNotificationModel |
|
52 | from rhodecode.model.notification import EmailNotificationModel | |
53 | from rhodecode.model.meta import Session |
|
53 | from rhodecode.model.meta import Session | |
54 | from rhodecode.lib.utils2 import str2bool |
|
54 | from rhodecode.lib.utils2 import str2bool, safe_unicode | |
55 |
|
55 | |||
56 | log = logging.getLogger(__name__) |
|
56 | log = logging.getLogger(__name__) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | class SettingsController(BaseController): |
|
59 | class SettingsController(BaseController): | |
60 | """REST Controller styled on the Atom Publishing Protocol""" |
|
60 | """REST Controller styled on the Atom Publishing Protocol""" | |
61 | # To properly map this controller, ensure your config/routing.py |
|
61 | # To properly map this controller, ensure your config/routing.py | |
62 | # file has a resource setup: |
|
62 | # file has a resource setup: | |
63 | # map.resource('setting', 'settings', controller='admin/settings', |
|
63 | # map.resource('setting', 'settings', controller='admin/settings', | |
64 | # path_prefix='/admin', name_prefix='admin_') |
|
64 | # path_prefix='/admin', name_prefix='admin_') | |
65 |
|
65 | |||
66 | @LoginRequired() |
|
66 | @LoginRequired() | |
67 | def __before__(self): |
|
67 | def __before__(self): | |
68 | c.admin_user = session.get('admin_user') |
|
68 | c.admin_user = session.get('admin_user') | |
69 | c.admin_username = session.get('admin_username') |
|
69 | c.admin_username = session.get('admin_username') | |
70 | c.modules = sorted([(p.project_name, p.version) |
|
70 | c.modules = sorted([(p.project_name, p.version) | |
71 | for p in pkg_resources.working_set] |
|
71 | for p in pkg_resources.working_set] | |
72 | + [('git', check_git_version())], |
|
72 | + [('git', check_git_version())], | |
73 | key=lambda k: k[0].lower()) |
|
73 | key=lambda k: k[0].lower()) | |
74 | c.py_version = platform.python_version() |
|
74 | c.py_version = platform.python_version() | |
75 | c.platform = platform.platform() |
|
75 | c.platform = platform.platform() | |
76 | super(SettingsController, self).__before__() |
|
76 | super(SettingsController, self).__before__() | |
77 |
|
77 | |||
78 | @HasPermissionAllDecorator('hg.admin') |
|
78 | @HasPermissionAllDecorator('hg.admin') | |
79 | def index(self, format='html'): |
|
79 | def index(self, format='html'): | |
80 | """GET /admin/settings: All items in the collection""" |
|
80 | """GET /admin/settings: All items in the collection""" | |
81 | # url('admin_settings') |
|
81 | # url('admin_settings') | |
82 |
|
82 | |||
83 | defaults = RhodeCodeSetting.get_app_settings() |
|
83 | defaults = RhodeCodeSetting.get_app_settings() | |
84 | defaults.update(self._get_hg_ui_settings()) |
|
84 | defaults.update(self._get_hg_ui_settings()) | |
85 |
|
85 | |||
86 | return htmlfill.render( |
|
86 | return htmlfill.render( | |
87 | render('admin/settings/settings.html'), |
|
87 | render('admin/settings/settings.html'), | |
88 | defaults=defaults, |
|
88 | defaults=defaults, | |
89 | encoding="UTF-8", |
|
89 | encoding="UTF-8", | |
90 | force_defaults=False |
|
90 | force_defaults=False | |
91 | ) |
|
91 | ) | |
92 |
|
92 | |||
93 | @HasPermissionAllDecorator('hg.admin') |
|
93 | @HasPermissionAllDecorator('hg.admin') | |
94 | def create(self): |
|
94 | def create(self): | |
95 | """POST /admin/settings: Create a new item""" |
|
95 | """POST /admin/settings: Create a new item""" | |
96 | # url('admin_settings') |
|
96 | # url('admin_settings') | |
97 |
|
97 | |||
98 | @HasPermissionAllDecorator('hg.admin') |
|
98 | @HasPermissionAllDecorator('hg.admin') | |
99 | def new(self, format='html'): |
|
99 | def new(self, format='html'): | |
100 | """GET /admin/settings/new: Form to create a new item""" |
|
100 | """GET /admin/settings/new: Form to create a new item""" | |
101 | # url('admin_new_setting') |
|
101 | # url('admin_new_setting') | |
102 |
|
102 | |||
103 | @HasPermissionAllDecorator('hg.admin') |
|
103 | @HasPermissionAllDecorator('hg.admin') | |
104 | def update(self, setting_id): |
|
104 | def update(self, setting_id): | |
105 | """PUT /admin/settings/setting_id: Update an existing item""" |
|
105 | """PUT /admin/settings/setting_id: Update an existing item""" | |
106 | # Forms posted to this method should contain a hidden field: |
|
106 | # Forms posted to this method should contain a hidden field: | |
107 | # <input type="hidden" name="_method" value="PUT" /> |
|
107 | # <input type="hidden" name="_method" value="PUT" /> | |
108 | # Or using helpers: |
|
108 | # Or using helpers: | |
109 | # h.form(url('admin_setting', setting_id=ID), |
|
109 | # h.form(url('admin_setting', setting_id=ID), | |
110 | # method='put') |
|
110 | # method='put') | |
111 | # url('admin_setting', setting_id=ID) |
|
111 | # url('admin_setting', setting_id=ID) | |
112 |
|
112 | |||
113 | if setting_id == 'mapping': |
|
113 | if setting_id == 'mapping': | |
114 | rm_obsolete = request.POST.get('destroy', False) |
|
114 | rm_obsolete = request.POST.get('destroy', False) | |
115 | log.debug('Rescanning directories with destroy=%s' % rm_obsolete) |
|
115 | log.debug('Rescanning directories with destroy=%s' % rm_obsolete) | |
116 | initial = ScmModel().repo_scan() |
|
116 | initial = ScmModel().repo_scan() | |
117 | log.debug('invalidating all repositories') |
|
117 | log.debug('invalidating all repositories') | |
118 | for repo_name in initial.keys(): |
|
118 | for repo_name in initial.keys(): | |
119 | invalidate_cache('get_repo_cached_%s' % repo_name) |
|
119 | invalidate_cache('get_repo_cached_%s' % repo_name) | |
120 |
|
120 | |||
121 | added, removed = repo2db_mapper(initial, rm_obsolete) |
|
121 | added, removed = repo2db_mapper(initial, rm_obsolete) | |
122 |
|
122 | _repr = lambda l: ', '.join(map(safe_unicode, l)) or '-' | ||
123 | h.flash(_('Repositories successfully' |
|
123 | h.flash(_('Repositories successfully ' | |
124 |
' |
|
124 | 'rescanned added: %s ; removed: %s') % | |
125 |
( |
|
125 | (_repr(added), _repr(removed)), | |
126 | category='success') |
|
126 | category='success') | |
127 |
|
127 | |||
128 | if setting_id == 'whoosh': |
|
128 | if setting_id == 'whoosh': | |
129 | repo_location = self._get_hg_ui_settings()['paths_root_path'] |
|
129 | repo_location = self._get_hg_ui_settings()['paths_root_path'] | |
130 | full_index = request.POST.get('full_index', False) |
|
130 | full_index = request.POST.get('full_index', False) | |
131 | run_task(tasks.whoosh_index, repo_location, full_index) |
|
131 | run_task(tasks.whoosh_index, repo_location, full_index) | |
132 | h.flash(_('Whoosh reindex task scheduled'), category='success') |
|
132 | h.flash(_('Whoosh reindex task scheduled'), category='success') | |
133 |
|
133 | |||
134 | if setting_id == 'global': |
|
134 | if setting_id == 'global': | |
135 |
|
135 | |||
136 | application_form = ApplicationSettingsForm()() |
|
136 | application_form = ApplicationSettingsForm()() | |
137 | try: |
|
137 | try: | |
138 | form_result = application_form.to_python(dict(request.POST)) |
|
138 | form_result = application_form.to_python(dict(request.POST)) | |
139 | except formencode.Invalid, errors: |
|
139 | except formencode.Invalid, errors: | |
140 | return htmlfill.render( |
|
140 | return htmlfill.render( | |
141 | render('admin/settings/settings.html'), |
|
141 | render('admin/settings/settings.html'), | |
142 | defaults=errors.value, |
|
142 | defaults=errors.value, | |
143 | errors=errors.error_dict or {}, |
|
143 | errors=errors.error_dict or {}, | |
144 | prefix_error=False, |
|
144 | prefix_error=False, | |
145 | encoding="UTF-8" |
|
145 | encoding="UTF-8" | |
146 | ) |
|
146 | ) | |
147 |
|
147 | |||
148 | try: |
|
148 | try: | |
149 | sett1 = RhodeCodeSetting.get_by_name_or_create('title') |
|
149 | sett1 = RhodeCodeSetting.get_by_name_or_create('title') | |
150 | sett1.app_settings_value = form_result['rhodecode_title'] |
|
150 | sett1.app_settings_value = form_result['rhodecode_title'] | |
151 | Session().add(sett1) |
|
151 | Session().add(sett1) | |
152 |
|
152 | |||
153 | sett2 = RhodeCodeSetting.get_by_name_or_create('realm') |
|
153 | sett2 = RhodeCodeSetting.get_by_name_or_create('realm') | |
154 | sett2.app_settings_value = form_result['rhodecode_realm'] |
|
154 | sett2.app_settings_value = form_result['rhodecode_realm'] | |
155 | Session().add(sett2) |
|
155 | Session().add(sett2) | |
156 |
|
156 | |||
157 | sett3 = RhodeCodeSetting.get_by_name_or_create('ga_code') |
|
157 | sett3 = RhodeCodeSetting.get_by_name_or_create('ga_code') | |
158 | sett3.app_settings_value = form_result['rhodecode_ga_code'] |
|
158 | sett3.app_settings_value = form_result['rhodecode_ga_code'] | |
159 | Session().add(sett3) |
|
159 | Session().add(sett3) | |
160 |
|
160 | |||
161 | Session().commit() |
|
161 | Session().commit() | |
162 | set_rhodecode_config(config) |
|
162 | set_rhodecode_config(config) | |
163 | h.flash(_('Updated application settings'), category='success') |
|
163 | h.flash(_('Updated application settings'), category='success') | |
164 |
|
164 | |||
165 | except Exception: |
|
165 | except Exception: | |
166 | log.error(traceback.format_exc()) |
|
166 | log.error(traceback.format_exc()) | |
167 | h.flash(_('error occurred during updating ' |
|
167 | h.flash(_('error occurred during updating ' | |
168 | 'application settings'), |
|
168 | 'application settings'), | |
169 | category='error') |
|
169 | category='error') | |
170 |
|
170 | |||
171 | if setting_id == 'visual': |
|
171 | if setting_id == 'visual': | |
172 |
|
172 | |||
173 | application_form = ApplicationVisualisationForm()() |
|
173 | application_form = ApplicationVisualisationForm()() | |
174 | try: |
|
174 | try: | |
175 | form_result = application_form.to_python(dict(request.POST)) |
|
175 | form_result = application_form.to_python(dict(request.POST)) | |
176 | except formencode.Invalid, errors: |
|
176 | except formencode.Invalid, errors: | |
177 | return htmlfill.render( |
|
177 | return htmlfill.render( | |
178 | render('admin/settings/settings.html'), |
|
178 | render('admin/settings/settings.html'), | |
179 | defaults=errors.value, |
|
179 | defaults=errors.value, | |
180 | errors=errors.error_dict or {}, |
|
180 | errors=errors.error_dict or {}, | |
181 | prefix_error=False, |
|
181 | prefix_error=False, | |
182 | encoding="UTF-8" |
|
182 | encoding="UTF-8" | |
183 | ) |
|
183 | ) | |
184 |
|
184 | |||
185 | try: |
|
185 | try: | |
186 | sett1 = RhodeCodeSetting.get_by_name_or_create('show_public_icon') |
|
186 | sett1 = RhodeCodeSetting.get_by_name_or_create('show_public_icon') | |
187 | sett1.app_settings_value = \ |
|
187 | sett1.app_settings_value = \ | |
188 | form_result['rhodecode_show_public_icon'] |
|
188 | form_result['rhodecode_show_public_icon'] | |
189 | Session().add(sett1) |
|
189 | Session().add(sett1) | |
190 |
|
190 | |||
191 | sett2 = RhodeCodeSetting.get_by_name_or_create('show_private_icon') |
|
191 | sett2 = RhodeCodeSetting.get_by_name_or_create('show_private_icon') | |
192 | sett2.app_settings_value = \ |
|
192 | sett2.app_settings_value = \ | |
193 | form_result['rhodecode_show_private_icon'] |
|
193 | form_result['rhodecode_show_private_icon'] | |
194 | Session().add(sett2) |
|
194 | Session().add(sett2) | |
195 |
|
195 | |||
196 | sett3 = RhodeCodeSetting.get_by_name_or_create('stylify_metatags') |
|
196 | sett3 = RhodeCodeSetting.get_by_name_or_create('stylify_metatags') | |
197 | sett3.app_settings_value = \ |
|
197 | sett3.app_settings_value = \ | |
198 | form_result['rhodecode_stylify_metatags'] |
|
198 | form_result['rhodecode_stylify_metatags'] | |
199 | Session().add(sett3) |
|
199 | Session().add(sett3) | |
200 |
|
200 | |||
201 | sett4 = RhodeCodeSetting.get_by_name_or_create('lightweight_dashboard') |
|
201 | sett4 = RhodeCodeSetting.get_by_name_or_create('lightweight_dashboard') | |
202 | sett4.app_settings_value = \ |
|
202 | sett4.app_settings_value = \ | |
203 | form_result['rhodecode_lightweight_dashboard'] |
|
203 | form_result['rhodecode_lightweight_dashboard'] | |
204 | Session().add(sett4) |
|
204 | Session().add(sett4) | |
205 |
|
205 | |||
206 | Session().commit() |
|
206 | Session().commit() | |
207 | set_rhodecode_config(config) |
|
207 | set_rhodecode_config(config) | |
208 | h.flash(_('Updated visualisation settings'), |
|
208 | h.flash(_('Updated visualisation settings'), | |
209 | category='success') |
|
209 | category='success') | |
210 |
|
210 | |||
211 | except Exception: |
|
211 | except Exception: | |
212 | log.error(traceback.format_exc()) |
|
212 | log.error(traceback.format_exc()) | |
213 | h.flash(_('error occurred during updating ' |
|
213 | h.flash(_('error occurred during updating ' | |
214 | 'visualisation settings'), |
|
214 | 'visualisation settings'), | |
215 | category='error') |
|
215 | category='error') | |
216 |
|
216 | |||
217 | if setting_id == 'vcs': |
|
217 | if setting_id == 'vcs': | |
218 | application_form = ApplicationUiSettingsForm()() |
|
218 | application_form = ApplicationUiSettingsForm()() | |
219 | try: |
|
219 | try: | |
220 | form_result = application_form.to_python(dict(request.POST)) |
|
220 | form_result = application_form.to_python(dict(request.POST)) | |
221 | except formencode.Invalid, errors: |
|
221 | except formencode.Invalid, errors: | |
222 | return htmlfill.render( |
|
222 | return htmlfill.render( | |
223 | render('admin/settings/settings.html'), |
|
223 | render('admin/settings/settings.html'), | |
224 | defaults=errors.value, |
|
224 | defaults=errors.value, | |
225 | errors=errors.error_dict or {}, |
|
225 | errors=errors.error_dict or {}, | |
226 | prefix_error=False, |
|
226 | prefix_error=False, | |
227 | encoding="UTF-8" |
|
227 | encoding="UTF-8" | |
228 | ) |
|
228 | ) | |
229 |
|
229 | |||
230 | try: |
|
230 | try: | |
231 | # fix namespaces for hooks and extensions |
|
231 | # fix namespaces for hooks and extensions | |
232 | _f = lambda s: s.replace('.', '_') |
|
232 | _f = lambda s: s.replace('.', '_') | |
233 |
|
233 | |||
234 | sett = RhodeCodeUi.get_by_key('push_ssl') |
|
234 | sett = RhodeCodeUi.get_by_key('push_ssl') | |
235 | sett.ui_value = form_result['web_push_ssl'] |
|
235 | sett.ui_value = form_result['web_push_ssl'] | |
236 | Session().add(sett) |
|
236 | Session().add(sett) | |
237 |
|
237 | |||
238 | sett = RhodeCodeUi.get_by_key('/') |
|
238 | sett = RhodeCodeUi.get_by_key('/') | |
239 | sett.ui_value = form_result['paths_root_path'] |
|
239 | sett.ui_value = form_result['paths_root_path'] | |
240 | Session().add(sett) |
|
240 | Session().add(sett) | |
241 |
|
241 | |||
242 | #HOOKS |
|
242 | #HOOKS | |
243 | sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE) |
|
243 | sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE) | |
244 | sett.ui_active = form_result[_f('hooks_%s' % |
|
244 | sett.ui_active = form_result[_f('hooks_%s' % | |
245 | RhodeCodeUi.HOOK_UPDATE)] |
|
245 | RhodeCodeUi.HOOK_UPDATE)] | |
246 | Session().add(sett) |
|
246 | Session().add(sett) | |
247 |
|
247 | |||
248 | sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_REPO_SIZE) |
|
248 | sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_REPO_SIZE) | |
249 | sett.ui_active = form_result[_f('hooks_%s' % |
|
249 | sett.ui_active = form_result[_f('hooks_%s' % | |
250 | RhodeCodeUi.HOOK_REPO_SIZE)] |
|
250 | RhodeCodeUi.HOOK_REPO_SIZE)] | |
251 | Session().add(sett) |
|
251 | Session().add(sett) | |
252 |
|
252 | |||
253 | sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_PUSH) |
|
253 | sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_PUSH) | |
254 | sett.ui_active = form_result[_f('hooks_%s' % |
|
254 | sett.ui_active = form_result[_f('hooks_%s' % | |
255 | RhodeCodeUi.HOOK_PUSH)] |
|
255 | RhodeCodeUi.HOOK_PUSH)] | |
256 | Session().add(sett) |
|
256 | Session().add(sett) | |
257 |
|
257 | |||
258 | sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_PULL) |
|
258 | sett = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_PULL) | |
259 | sett.ui_active = form_result[_f('hooks_%s' % |
|
259 | sett.ui_active = form_result[_f('hooks_%s' % | |
260 | RhodeCodeUi.HOOK_PULL)] |
|
260 | RhodeCodeUi.HOOK_PULL)] | |
261 |
|
261 | |||
262 | Session().add(sett) |
|
262 | Session().add(sett) | |
263 |
|
263 | |||
264 | ## EXTENSIONS |
|
264 | ## EXTENSIONS | |
265 | sett = RhodeCodeUi.get_by_key('largefiles') |
|
265 | sett = RhodeCodeUi.get_by_key('largefiles') | |
266 | if not sett: |
|
266 | if not sett: | |
267 | #make one if it's not there ! |
|
267 | #make one if it's not there ! | |
268 | sett = RhodeCodeUi() |
|
268 | sett = RhodeCodeUi() | |
269 | sett.ui_key = 'largefiles' |
|
269 | sett.ui_key = 'largefiles' | |
270 | sett.ui_section = 'extensions' |
|
270 | sett.ui_section = 'extensions' | |
271 | sett.ui_active = form_result[_f('extensions_largefiles')] |
|
271 | sett.ui_active = form_result[_f('extensions_largefiles')] | |
272 | Session().add(sett) |
|
272 | Session().add(sett) | |
273 |
|
273 | |||
274 | sett = RhodeCodeUi.get_by_key('hgsubversion') |
|
274 | sett = RhodeCodeUi.get_by_key('hgsubversion') | |
275 | if not sett: |
|
275 | if not sett: | |
276 | #make one if it's not there ! |
|
276 | #make one if it's not there ! | |
277 | sett = RhodeCodeUi() |
|
277 | sett = RhodeCodeUi() | |
278 | sett.ui_key = 'hgsubversion' |
|
278 | sett.ui_key = 'hgsubversion' | |
279 | sett.ui_section = 'extensions' |
|
279 | sett.ui_section = 'extensions' | |
280 |
|
280 | |||
281 | sett.ui_active = form_result[_f('extensions_hgsubversion')] |
|
281 | sett.ui_active = form_result[_f('extensions_hgsubversion')] | |
282 | Session().add(sett) |
|
282 | Session().add(sett) | |
283 |
|
283 | |||
284 | # sett = RhodeCodeUi.get_by_key('hggit') |
|
284 | # sett = RhodeCodeUi.get_by_key('hggit') | |
285 | # if not sett: |
|
285 | # if not sett: | |
286 | # #make one if it's not there ! |
|
286 | # #make one if it's not there ! | |
287 | # sett = RhodeCodeUi() |
|
287 | # sett = RhodeCodeUi() | |
288 | # sett.ui_key = 'hggit' |
|
288 | # sett.ui_key = 'hggit' | |
289 | # sett.ui_section = 'extensions' |
|
289 | # sett.ui_section = 'extensions' | |
290 | # |
|
290 | # | |
291 | # sett.ui_active = form_result[_f('extensions_hggit')] |
|
291 | # sett.ui_active = form_result[_f('extensions_hggit')] | |
292 | # Session().add(sett) |
|
292 | # Session().add(sett) | |
293 |
|
293 | |||
294 | Session().commit() |
|
294 | Session().commit() | |
295 |
|
295 | |||
296 | h.flash(_('Updated VCS settings'), category='success') |
|
296 | h.flash(_('Updated VCS settings'), category='success') | |
297 |
|
297 | |||
298 | except Exception: |
|
298 | except Exception: | |
299 | log.error(traceback.format_exc()) |
|
299 | log.error(traceback.format_exc()) | |
300 | h.flash(_('error occurred during updating ' |
|
300 | h.flash(_('error occurred during updating ' | |
301 | 'application settings'), category='error') |
|
301 | 'application settings'), category='error') | |
302 |
|
302 | |||
303 | if setting_id == 'hooks': |
|
303 | if setting_id == 'hooks': | |
304 | ui_key = request.POST.get('new_hook_ui_key') |
|
304 | ui_key = request.POST.get('new_hook_ui_key') | |
305 | ui_value = request.POST.get('new_hook_ui_value') |
|
305 | ui_value = request.POST.get('new_hook_ui_value') | |
306 | try: |
|
306 | try: | |
307 |
|
307 | |||
308 | if ui_value and ui_key: |
|
308 | if ui_value and ui_key: | |
309 | RhodeCodeUi.create_or_update_hook(ui_key, ui_value) |
|
309 | RhodeCodeUi.create_or_update_hook(ui_key, ui_value) | |
310 | h.flash(_('Added new hook'), |
|
310 | h.flash(_('Added new hook'), | |
311 | category='success') |
|
311 | category='success') | |
312 |
|
312 | |||
313 | # check for edits |
|
313 | # check for edits | |
314 | update = False |
|
314 | update = False | |
315 | _d = request.POST.dict_of_lists() |
|
315 | _d = request.POST.dict_of_lists() | |
316 | for k, v in zip(_d.get('hook_ui_key', []), |
|
316 | for k, v in zip(_d.get('hook_ui_key', []), | |
317 | _d.get('hook_ui_value_new', [])): |
|
317 | _d.get('hook_ui_value_new', [])): | |
318 | RhodeCodeUi.create_or_update_hook(k, v) |
|
318 | RhodeCodeUi.create_or_update_hook(k, v) | |
319 | update = True |
|
319 | update = True | |
320 |
|
320 | |||
321 | if update: |
|
321 | if update: | |
322 | h.flash(_('Updated hooks'), category='success') |
|
322 | h.flash(_('Updated hooks'), category='success') | |
323 | Session().commit() |
|
323 | Session().commit() | |
324 | except Exception: |
|
324 | except Exception: | |
325 | log.error(traceback.format_exc()) |
|
325 | log.error(traceback.format_exc()) | |
326 | h.flash(_('error occurred during hook creation'), |
|
326 | h.flash(_('error occurred during hook creation'), | |
327 | category='error') |
|
327 | category='error') | |
328 |
|
328 | |||
329 | return redirect(url('admin_edit_setting', setting_id='hooks')) |
|
329 | return redirect(url('admin_edit_setting', setting_id='hooks')) | |
330 |
|
330 | |||
331 | if setting_id == 'email': |
|
331 | if setting_id == 'email': | |
332 | test_email = request.POST.get('test_email') |
|
332 | test_email = request.POST.get('test_email') | |
333 | test_email_subj = 'RhodeCode TestEmail' |
|
333 | test_email_subj = 'RhodeCode TestEmail' | |
334 | test_email_body = 'RhodeCode Email test' |
|
334 | test_email_body = 'RhodeCode Email test' | |
335 |
|
335 | |||
336 | test_email_html_body = EmailNotificationModel()\ |
|
336 | test_email_html_body = EmailNotificationModel()\ | |
337 | .get_email_tmpl(EmailNotificationModel.TYPE_DEFAULT, |
|
337 | .get_email_tmpl(EmailNotificationModel.TYPE_DEFAULT, | |
338 | body=test_email_body) |
|
338 | body=test_email_body) | |
339 |
|
339 | |||
340 | recipients = [test_email] if test_email else None |
|
340 | recipients = [test_email] if test_email else None | |
341 |
|
341 | |||
342 | run_task(tasks.send_email, recipients, test_email_subj, |
|
342 | run_task(tasks.send_email, recipients, test_email_subj, | |
343 | test_email_body, test_email_html_body) |
|
343 | test_email_body, test_email_html_body) | |
344 |
|
344 | |||
345 | h.flash(_('Email task created'), category='success') |
|
345 | h.flash(_('Email task created'), category='success') | |
346 | return redirect(url('admin_settings')) |
|
346 | return redirect(url('admin_settings')) | |
347 |
|
347 | |||
348 | @HasPermissionAllDecorator('hg.admin') |
|
348 | @HasPermissionAllDecorator('hg.admin') | |
349 | def delete(self, setting_id): |
|
349 | def delete(self, setting_id): | |
350 | """DELETE /admin/settings/setting_id: Delete an existing item""" |
|
350 | """DELETE /admin/settings/setting_id: Delete an existing item""" | |
351 | # Forms posted to this method should contain a hidden field: |
|
351 | # Forms posted to this method should contain a hidden field: | |
352 | # <input type="hidden" name="_method" value="DELETE" /> |
|
352 | # <input type="hidden" name="_method" value="DELETE" /> | |
353 | # Or using helpers: |
|
353 | # Or using helpers: | |
354 | # h.form(url('admin_setting', setting_id=ID), |
|
354 | # h.form(url('admin_setting', setting_id=ID), | |
355 | # method='delete') |
|
355 | # method='delete') | |
356 | # url('admin_setting', setting_id=ID) |
|
356 | # url('admin_setting', setting_id=ID) | |
357 | if setting_id == 'hooks': |
|
357 | if setting_id == 'hooks': | |
358 | hook_id = request.POST.get('hook_id') |
|
358 | hook_id = request.POST.get('hook_id') | |
359 | RhodeCodeUi.delete(hook_id) |
|
359 | RhodeCodeUi.delete(hook_id) | |
360 | Session().commit() |
|
360 | Session().commit() | |
361 |
|
361 | |||
362 | @HasPermissionAllDecorator('hg.admin') |
|
362 | @HasPermissionAllDecorator('hg.admin') | |
363 | def show(self, setting_id, format='html'): |
|
363 | def show(self, setting_id, format='html'): | |
364 | """ |
|
364 | """ | |
365 | GET /admin/settings/setting_id: Show a specific item""" |
|
365 | GET /admin/settings/setting_id: Show a specific item""" | |
366 | # url('admin_setting', setting_id=ID) |
|
366 | # url('admin_setting', setting_id=ID) | |
367 |
|
367 | |||
368 | @HasPermissionAllDecorator('hg.admin') |
|
368 | @HasPermissionAllDecorator('hg.admin') | |
369 | def edit(self, setting_id, format='html'): |
|
369 | def edit(self, setting_id, format='html'): | |
370 | """ |
|
370 | """ | |
371 | GET /admin/settings/setting_id/edit: Form to |
|
371 | GET /admin/settings/setting_id/edit: Form to | |
372 | edit an existing item""" |
|
372 | edit an existing item""" | |
373 | # url('admin_edit_setting', setting_id=ID) |
|
373 | # url('admin_edit_setting', setting_id=ID) | |
374 |
|
374 | |||
375 | c.hooks = RhodeCodeUi.get_builtin_hooks() |
|
375 | c.hooks = RhodeCodeUi.get_builtin_hooks() | |
376 | c.custom_hooks = RhodeCodeUi.get_custom_hooks() |
|
376 | c.custom_hooks = RhodeCodeUi.get_custom_hooks() | |
377 |
|
377 | |||
378 | return htmlfill.render( |
|
378 | return htmlfill.render( | |
379 | render('admin/settings/hooks.html'), |
|
379 | render('admin/settings/hooks.html'), | |
380 | defaults={}, |
|
380 | defaults={}, | |
381 | encoding="UTF-8", |
|
381 | encoding="UTF-8", | |
382 | force_defaults=False |
|
382 | force_defaults=False | |
383 | ) |
|
383 | ) | |
384 |
|
384 | |||
385 | @NotAnonymous() |
|
385 | @NotAnonymous() | |
386 | def my_account(self): |
|
386 | def my_account(self): | |
387 | """ |
|
387 | """ | |
388 | GET /_admin/my_account Displays info about my account |
|
388 | GET /_admin/my_account Displays info about my account | |
389 | """ |
|
389 | """ | |
390 | # url('admin_settings_my_account') |
|
390 | # url('admin_settings_my_account') | |
391 |
|
391 | |||
392 | c.user = User.get(self.rhodecode_user.user_id) |
|
392 | c.user = User.get(self.rhodecode_user.user_id) | |
393 | all_repos = Session().query(Repository)\ |
|
393 | all_repos = Session().query(Repository)\ | |
394 | .filter(Repository.user_id == c.user.user_id)\ |
|
394 | .filter(Repository.user_id == c.user.user_id)\ | |
395 | .order_by(func.lower(Repository.repo_name)).all() |
|
395 | .order_by(func.lower(Repository.repo_name)).all() | |
396 |
|
396 | |||
397 | c.user_repos = ScmModel().get_repos(all_repos) |
|
397 | c.user_repos = ScmModel().get_repos(all_repos) | |
398 |
|
398 | |||
399 | if c.user.username == 'default': |
|
399 | if c.user.username == 'default': | |
400 | h.flash(_("You can't edit this user since it's" |
|
400 | h.flash(_("You can't edit this user since it's" | |
401 | " crucial for entire application"), category='warning') |
|
401 | " crucial for entire application"), category='warning') | |
402 | return redirect(url('users')) |
|
402 | return redirect(url('users')) | |
403 |
|
403 | |||
404 | defaults = c.user.get_dict() |
|
404 | defaults = c.user.get_dict() | |
405 |
|
405 | |||
406 | c.form = htmlfill.render( |
|
406 | c.form = htmlfill.render( | |
407 | render('admin/users/user_edit_my_account_form.html'), |
|
407 | render('admin/users/user_edit_my_account_form.html'), | |
408 | defaults=defaults, |
|
408 | defaults=defaults, | |
409 | encoding="UTF-8", |
|
409 | encoding="UTF-8", | |
410 | force_defaults=False |
|
410 | force_defaults=False | |
411 | ) |
|
411 | ) | |
412 | return render('admin/users/user_edit_my_account.html') |
|
412 | return render('admin/users/user_edit_my_account.html') | |
413 |
|
413 | |||
414 | @NotAnonymous() |
|
414 | @NotAnonymous() | |
415 | def my_account_update(self): |
|
415 | def my_account_update(self): | |
416 | """PUT /_admin/my_account_update: Update an existing item""" |
|
416 | """PUT /_admin/my_account_update: Update an existing item""" | |
417 | # Forms posted to this method should contain a hidden field: |
|
417 | # Forms posted to this method should contain a hidden field: | |
418 | # <input type="hidden" name="_method" value="PUT" /> |
|
418 | # <input type="hidden" name="_method" value="PUT" /> | |
419 | # Or using helpers: |
|
419 | # Or using helpers: | |
420 | # h.form(url('admin_settings_my_account_update'), |
|
420 | # h.form(url('admin_settings_my_account_update'), | |
421 | # method='put') |
|
421 | # method='put') | |
422 | # url('admin_settings_my_account_update', id=ID) |
|
422 | # url('admin_settings_my_account_update', id=ID) | |
423 | uid = self.rhodecode_user.user_id |
|
423 | uid = self.rhodecode_user.user_id | |
424 | email = self.rhodecode_user.email |
|
424 | email = self.rhodecode_user.email | |
425 | _form = UserForm(edit=True, |
|
425 | _form = UserForm(edit=True, | |
426 | old_data={'user_id': uid, 'email': email})() |
|
426 | old_data={'user_id': uid, 'email': email})() | |
427 | form_result = {} |
|
427 | form_result = {} | |
428 | try: |
|
428 | try: | |
429 | form_result = _form.to_python(dict(request.POST)) |
|
429 | form_result = _form.to_python(dict(request.POST)) | |
430 | UserModel().update_my_account(uid, form_result) |
|
430 | UserModel().update_my_account(uid, form_result) | |
431 | h.flash(_('Your account was updated successfully'), |
|
431 | h.flash(_('Your account was updated successfully'), | |
432 | category='success') |
|
432 | category='success') | |
433 | Session().commit() |
|
433 | Session().commit() | |
434 | except formencode.Invalid, errors: |
|
434 | except formencode.Invalid, errors: | |
435 | c.user = User.get(self.rhodecode_user.user_id) |
|
435 | c.user = User.get(self.rhodecode_user.user_id) | |
436 |
|
436 | |||
437 | c.form = htmlfill.render( |
|
437 | c.form = htmlfill.render( | |
438 | render('admin/users/user_edit_my_account_form.html'), |
|
438 | render('admin/users/user_edit_my_account_form.html'), | |
439 | defaults=errors.value, |
|
439 | defaults=errors.value, | |
440 | errors=errors.error_dict or {}, |
|
440 | errors=errors.error_dict or {}, | |
441 | prefix_error=False, |
|
441 | prefix_error=False, | |
442 | encoding="UTF-8") |
|
442 | encoding="UTF-8") | |
443 | return render('admin/users/user_edit_my_account.html') |
|
443 | return render('admin/users/user_edit_my_account.html') | |
444 | except Exception: |
|
444 | except Exception: | |
445 | log.error(traceback.format_exc()) |
|
445 | log.error(traceback.format_exc()) | |
446 | h.flash(_('error occurred during update of user %s') \ |
|
446 | h.flash(_('error occurred during update of user %s') \ | |
447 | % form_result.get('username'), category='error') |
|
447 | % form_result.get('username'), category='error') | |
448 |
|
448 | |||
449 | return redirect(url('my_account')) |
|
449 | return redirect(url('my_account')) | |
450 |
|
450 | |||
451 | @NotAnonymous() |
|
451 | @NotAnonymous() | |
452 | def my_account_my_repos(self): |
|
452 | def my_account_my_repos(self): | |
453 | all_repos = Session().query(Repository)\ |
|
453 | all_repos = Session().query(Repository)\ | |
454 | .filter(Repository.user_id == self.rhodecode_user.user_id)\ |
|
454 | .filter(Repository.user_id == self.rhodecode_user.user_id)\ | |
455 | .order_by(func.lower(Repository.repo_name))\ |
|
455 | .order_by(func.lower(Repository.repo_name))\ | |
456 | .all() |
|
456 | .all() | |
457 | c.user_repos = ScmModel().get_repos(all_repos) |
|
457 | c.user_repos = ScmModel().get_repos(all_repos) | |
458 | return render('admin/users/user_edit_my_account_repos.html') |
|
458 | return render('admin/users/user_edit_my_account_repos.html') | |
459 |
|
459 | |||
460 | @NotAnonymous() |
|
460 | @NotAnonymous() | |
461 | def my_account_my_pullrequests(self): |
|
461 | def my_account_my_pullrequests(self): | |
462 | c.my_pull_requests = PullRequest.query()\ |
|
462 | c.my_pull_requests = PullRequest.query()\ | |
463 | .filter(PullRequest.user_id== |
|
463 | .filter(PullRequest.user_id== | |
464 | self.rhodecode_user.user_id)\ |
|
464 | self.rhodecode_user.user_id)\ | |
465 | .all() |
|
465 | .all() | |
466 | c.participate_in_pull_requests = \ |
|
466 | c.participate_in_pull_requests = \ | |
467 | [x.pull_request for x in PullRequestReviewers.query()\ |
|
467 | [x.pull_request for x in PullRequestReviewers.query()\ | |
468 | .filter(PullRequestReviewers.user_id== |
|
468 | .filter(PullRequestReviewers.user_id== | |
469 | self.rhodecode_user.user_id)\ |
|
469 | self.rhodecode_user.user_id)\ | |
470 | .all()] |
|
470 | .all()] | |
471 | return render('admin/users/user_edit_my_account_pullrequests.html') |
|
471 | return render('admin/users/user_edit_my_account_pullrequests.html') | |
472 |
|
472 | |||
473 | @NotAnonymous() |
|
473 | @NotAnonymous() | |
474 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
474 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') | |
475 | def create_repository(self): |
|
475 | def create_repository(self): | |
476 | """GET /_admin/create_repository: Form to create a new item""" |
|
476 | """GET /_admin/create_repository: Form to create a new item""" | |
477 |
|
477 | |||
478 | c.repo_groups = RepoGroup.groups_choices(check_perms=True) |
|
478 | c.repo_groups = RepoGroup.groups_choices(check_perms=True) | |
479 | c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) |
|
479 | c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) | |
480 | choices, c.landing_revs = ScmModel().get_repo_landing_revs() |
|
480 | choices, c.landing_revs = ScmModel().get_repo_landing_revs() | |
481 |
|
481 | |||
482 | new_repo = request.GET.get('repo', '') |
|
482 | new_repo = request.GET.get('repo', '') | |
483 | c.new_repo = repo_name_slug(new_repo) |
|
483 | c.new_repo = repo_name_slug(new_repo) | |
484 |
|
484 | |||
485 | ## apply the defaults from defaults page |
|
485 | ## apply the defaults from defaults page | |
486 | defaults = RhodeCodeSetting.get_default_repo_settings(strip_prefix=True) |
|
486 | defaults = RhodeCodeSetting.get_default_repo_settings(strip_prefix=True) | |
487 | return htmlfill.render( |
|
487 | return htmlfill.render( | |
488 | render('admin/repos/repo_add_create_repository.html'), |
|
488 | render('admin/repos/repo_add_create_repository.html'), | |
489 | defaults=defaults, |
|
489 | defaults=defaults, | |
490 | errors={}, |
|
490 | errors={}, | |
491 | prefix_error=False, |
|
491 | prefix_error=False, | |
492 | encoding="UTF-8" |
|
492 | encoding="UTF-8" | |
493 | ) |
|
493 | ) | |
494 |
|
494 | |||
495 | def _get_hg_ui_settings(self): |
|
495 | def _get_hg_ui_settings(self): | |
496 | ret = RhodeCodeUi.query().all() |
|
496 | ret = RhodeCodeUi.query().all() | |
497 |
|
497 | |||
498 | if not ret: |
|
498 | if not ret: | |
499 | raise Exception('Could not get application ui settings !') |
|
499 | raise Exception('Could not get application ui settings !') | |
500 | settings = {} |
|
500 | settings = {} | |
501 | for each in ret: |
|
501 | for each in ret: | |
502 | k = each.ui_key |
|
502 | k = each.ui_key | |
503 | v = each.ui_value |
|
503 | v = each.ui_value | |
504 | if k == '/': |
|
504 | if k == '/': | |
505 | k = 'root_path' |
|
505 | k = 'root_path' | |
506 |
|
506 | |||
507 | if k == 'push_ssl': |
|
507 | if k == 'push_ssl': | |
508 | v = str2bool(v) |
|
508 | v = str2bool(v) | |
509 |
|
509 | |||
510 | if k.find('.') != -1: |
|
510 | if k.find('.') != -1: | |
511 | k = k.replace('.', '_') |
|
511 | k = k.replace('.', '_') | |
512 |
|
512 | |||
513 | if each.ui_section in ['hooks', 'extensions']: |
|
513 | if each.ui_section in ['hooks', 'extensions']: | |
514 | v = each.ui_active |
|
514 | v = each.ui_active | |
515 |
|
515 | |||
516 | settings[each.ui_section + '_' + k] = v |
|
516 | settings[each.ui_section + '_' + k] = v | |
517 | return settings |
|
517 | return settings |
General Comments 0
You need to be logged in to leave comments.
Login now