Show More
@@ -1,443 +1,447 | |||||
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 |
|
44 | set_rhodecode_config, repo_name_slug | |
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 |
|
48 | ApplicationUiSettingsForm | |
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 pylons.decorators import jsonify |
|
54 | from pylons.decorators import jsonify | |
55 | from rhodecode.model.pull_request import PullRequestModel |
|
55 | from rhodecode.model.pull_request import PullRequestModel | |
56 |
|
56 | |||
57 | log = logging.getLogger(__name__) |
|
57 | log = logging.getLogger(__name__) | |
58 |
|
58 | |||
59 |
|
59 | |||
60 | class SettingsController(BaseController): |
|
60 | class SettingsController(BaseController): | |
61 | """REST Controller styled on the Atom Publishing Protocol""" |
|
61 | """REST Controller styled on the Atom Publishing Protocol""" | |
62 | # To properly map this controller, ensure your config/routing.py |
|
62 | # To properly map this controller, ensure your config/routing.py | |
63 | # file has a resource setup: |
|
63 | # file has a resource setup: | |
64 | # map.resource('setting', 'settings', controller='admin/settings', |
|
64 | # map.resource('setting', 'settings', controller='admin/settings', | |
65 | # path_prefix='/admin', name_prefix='admin_') |
|
65 | # path_prefix='/admin', name_prefix='admin_') | |
66 |
|
66 | |||
67 | @LoginRequired() |
|
67 | @LoginRequired() | |
68 | def __before__(self): |
|
68 | def __before__(self): | |
69 | c.admin_user = session.get('admin_user') |
|
69 | c.admin_user = session.get('admin_user') | |
70 | c.admin_username = session.get('admin_username') |
|
70 | c.admin_username = session.get('admin_username') | |
71 | c.modules = sorted([(p.project_name, p.version) |
|
71 | c.modules = sorted([(p.project_name, p.version) | |
72 | for p in pkg_resources.working_set], |
|
72 | for p in pkg_resources.working_set], | |
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 | if setting_id == 'mapping': |
|
112 | if setting_id == 'mapping': | |
113 | rm_obsolete = request.POST.get('destroy', False) |
|
113 | rm_obsolete = request.POST.get('destroy', False) | |
114 | log.debug('Rescanning directories with destroy=%s' % rm_obsolete) |
|
114 | log.debug('Rescanning directories with destroy=%s' % rm_obsolete) | |
115 | initial = ScmModel().repo_scan() |
|
115 | initial = ScmModel().repo_scan() | |
116 | log.debug('invalidating all repositories') |
|
116 | log.debug('invalidating all repositories') | |
117 | for repo_name in initial.keys(): |
|
117 | for repo_name in initial.keys(): | |
118 | invalidate_cache('get_repo_cached_%s' % repo_name) |
|
118 | invalidate_cache('get_repo_cached_%s' % repo_name) | |
119 |
|
119 | |||
120 | added, removed = repo2db_mapper(initial, rm_obsolete) |
|
120 | added, removed = repo2db_mapper(initial, rm_obsolete) | |
121 |
|
121 | |||
122 | h.flash(_('Repositories successfully' |
|
122 | h.flash(_('Repositories successfully' | |
123 | ' rescanned added: %s,removed: %s') % (added, removed), |
|
123 | ' rescanned added: %s,removed: %s') % (added, removed), | |
124 | category='success') |
|
124 | category='success') | |
125 |
|
125 | |||
126 | if setting_id == 'whoosh': |
|
126 | if setting_id == 'whoosh': | |
127 | repo_location = self.get_hg_ui_settings()['paths_root_path'] |
|
127 | repo_location = self.get_hg_ui_settings()['paths_root_path'] | |
128 | full_index = request.POST.get('full_index', False) |
|
128 | full_index = request.POST.get('full_index', False) | |
129 | run_task(tasks.whoosh_index, repo_location, full_index) |
|
129 | run_task(tasks.whoosh_index, repo_location, full_index) | |
130 |
|
130 | |||
131 | h.flash(_('Whoosh reindex task scheduled'), category='success') |
|
131 | h.flash(_('Whoosh reindex task scheduled'), category='success') | |
132 | if setting_id == 'global': |
|
132 | if setting_id == 'global': | |
133 |
|
133 | |||
134 | application_form = ApplicationSettingsForm()() |
|
134 | application_form = ApplicationSettingsForm()() | |
135 | try: |
|
135 | try: | |
136 | form_result = application_form.to_python(dict(request.POST)) |
|
136 | form_result = application_form.to_python(dict(request.POST)) | |
137 |
|
137 | |||
138 | try: |
|
138 | try: | |
139 | hgsettings1 = RhodeCodeSetting.get_by_name('title') |
|
139 | hgsettings1 = RhodeCodeSetting.get_by_name('title') | |
140 | hgsettings1.app_settings_value = \ |
|
140 | hgsettings1.app_settings_value = \ | |
141 | form_result['rhodecode_title'] |
|
141 | form_result['rhodecode_title'] | |
142 |
|
142 | |||
143 | hgsettings2 = RhodeCodeSetting.get_by_name('realm') |
|
143 | hgsettings2 = RhodeCodeSetting.get_by_name('realm') | |
144 | hgsettings2.app_settings_value = \ |
|
144 | hgsettings2.app_settings_value = \ | |
145 | form_result['rhodecode_realm'] |
|
145 | form_result['rhodecode_realm'] | |
146 |
|
146 | |||
147 | hgsettings3 = RhodeCodeSetting.get_by_name('ga_code') |
|
147 | hgsettings3 = RhodeCodeSetting.get_by_name('ga_code') | |
148 | hgsettings3.app_settings_value = \ |
|
148 | hgsettings3.app_settings_value = \ | |
149 | form_result['rhodecode_ga_code'] |
|
149 | form_result['rhodecode_ga_code'] | |
150 |
|
150 | |||
151 | self.sa.add(hgsettings1) |
|
151 | self.sa.add(hgsettings1) | |
152 | self.sa.add(hgsettings2) |
|
152 | self.sa.add(hgsettings2) | |
153 | self.sa.add(hgsettings3) |
|
153 | self.sa.add(hgsettings3) | |
154 | self.sa.commit() |
|
154 | self.sa.commit() | |
155 | set_rhodecode_config(config) |
|
155 | set_rhodecode_config(config) | |
156 | h.flash(_('Updated application settings'), |
|
156 | h.flash(_('Updated application settings'), | |
157 | category='success') |
|
157 | category='success') | |
158 |
|
158 | |||
159 | except Exception: |
|
159 | except Exception: | |
160 | log.error(traceback.format_exc()) |
|
160 | log.error(traceback.format_exc()) | |
161 | h.flash(_('error occurred during updating ' |
|
161 | h.flash(_('error occurred during updating ' | |
162 | 'application settings'), |
|
162 | 'application settings'), | |
163 | category='error') |
|
163 | category='error') | |
164 |
|
164 | |||
165 | self.sa.rollback() |
|
165 | self.sa.rollback() | |
166 |
|
166 | |||
167 | except formencode.Invalid, errors: |
|
167 | except formencode.Invalid, errors: | |
168 | return htmlfill.render( |
|
168 | return htmlfill.render( | |
169 | render('admin/settings/settings.html'), |
|
169 | render('admin/settings/settings.html'), | |
170 | defaults=errors.value, |
|
170 | defaults=errors.value, | |
171 | errors=errors.error_dict or {}, |
|
171 | errors=errors.error_dict or {}, | |
172 | prefix_error=False, |
|
172 | prefix_error=False, | |
173 | encoding="UTF-8") |
|
173 | encoding="UTF-8") | |
174 |
|
174 | |||
175 | if setting_id == 'mercurial': |
|
175 | if setting_id == 'mercurial': | |
176 | application_form = ApplicationUiSettingsForm()() |
|
176 | application_form = ApplicationUiSettingsForm()() | |
177 | try: |
|
177 | try: | |
178 | form_result = application_form.to_python(dict(request.POST)) |
|
178 | form_result = application_form.to_python(dict(request.POST)) | |
179 | # fix namespaces for hooks |
|
179 | # fix namespaces for hooks | |
180 | _f = lambda s: s.replace('.', '_') |
|
180 | _f = lambda s: s.replace('.', '_') | |
181 | try: |
|
181 | try: | |
182 |
|
182 | |||
183 | hgsettings1 = self.sa.query(RhodeCodeUi)\ |
|
183 | hgsettings1 = self.sa.query(RhodeCodeUi)\ | |
184 | .filter(RhodeCodeUi.ui_key == 'push_ssl').one() |
|
184 | .filter(RhodeCodeUi.ui_key == 'push_ssl').one() | |
185 | hgsettings1.ui_value = form_result['web_push_ssl'] |
|
185 | hgsettings1.ui_value = form_result['web_push_ssl'] | |
186 |
|
186 | |||
187 | hgsettings2 = self.sa.query(RhodeCodeUi)\ |
|
187 | hgsettings2 = self.sa.query(RhodeCodeUi)\ | |
188 | .filter(RhodeCodeUi.ui_key == '/').one() |
|
188 | .filter(RhodeCodeUi.ui_key == '/').one() | |
189 | hgsettings2.ui_value = form_result['paths_root_path'] |
|
189 | hgsettings2.ui_value = form_result['paths_root_path'] | |
190 |
|
190 | |||
191 | #HOOKS |
|
191 | #HOOKS | |
192 | hgsettings3 = self.sa.query(RhodeCodeUi)\ |
|
192 | hgsettings3 = self.sa.query(RhodeCodeUi)\ | |
193 | .filter(RhodeCodeUi.ui_key == RhodeCodeUi.HOOK_UPDATE)\ |
|
193 | .filter(RhodeCodeUi.ui_key == RhodeCodeUi.HOOK_UPDATE)\ | |
194 | .one() |
|
194 | .one() | |
195 | hgsettings3.ui_active = bool(form_result[_f('hooks_%s' % |
|
195 | hgsettings3.ui_active = bool(form_result[_f('hooks_%s' % | |
196 | RhodeCodeUi.HOOK_UPDATE)]) |
|
196 | RhodeCodeUi.HOOK_UPDATE)]) | |
197 |
|
197 | |||
198 | hgsettings4 = self.sa.query(RhodeCodeUi)\ |
|
198 | hgsettings4 = self.sa.query(RhodeCodeUi)\ | |
199 | .filter(RhodeCodeUi.ui_key == RhodeCodeUi.HOOK_REPO_SIZE)\ |
|
199 | .filter(RhodeCodeUi.ui_key == RhodeCodeUi.HOOK_REPO_SIZE)\ | |
200 | .one() |
|
200 | .one() | |
201 | hgsettings4.ui_active = bool(form_result[_f('hooks_%s' % |
|
201 | hgsettings4.ui_active = bool(form_result[_f('hooks_%s' % | |
202 | RhodeCodeUi.HOOK_REPO_SIZE)]) |
|
202 | RhodeCodeUi.HOOK_REPO_SIZE)]) | |
203 |
|
203 | |||
204 | hgsettings5 = self.sa.query(RhodeCodeUi)\ |
|
204 | hgsettings5 = self.sa.query(RhodeCodeUi)\ | |
205 | .filter(RhodeCodeUi.ui_key == RhodeCodeUi.HOOK_PUSH)\ |
|
205 | .filter(RhodeCodeUi.ui_key == RhodeCodeUi.HOOK_PUSH)\ | |
206 | .one() |
|
206 | .one() | |
207 | hgsettings5.ui_active = bool(form_result[_f('hooks_%s' % |
|
207 | hgsettings5.ui_active = bool(form_result[_f('hooks_%s' % | |
208 | RhodeCodeUi.HOOK_PUSH)]) |
|
208 | RhodeCodeUi.HOOK_PUSH)]) | |
209 |
|
209 | |||
210 | hgsettings6 = self.sa.query(RhodeCodeUi)\ |
|
210 | hgsettings6 = self.sa.query(RhodeCodeUi)\ | |
211 | .filter(RhodeCodeUi.ui_key == RhodeCodeUi.HOOK_PULL)\ |
|
211 | .filter(RhodeCodeUi.ui_key == RhodeCodeUi.HOOK_PULL)\ | |
212 | .one() |
|
212 | .one() | |
213 | hgsettings6.ui_active = bool(form_result[_f('hooks_%s' % |
|
213 | hgsettings6.ui_active = bool(form_result[_f('hooks_%s' % | |
214 | RhodeCodeUi.HOOK_PULL)]) |
|
214 | RhodeCodeUi.HOOK_PULL)]) | |
215 |
|
215 | |||
216 | self.sa.add(hgsettings1) |
|
216 | self.sa.add(hgsettings1) | |
217 | self.sa.add(hgsettings2) |
|
217 | self.sa.add(hgsettings2) | |
218 | self.sa.add(hgsettings3) |
|
218 | self.sa.add(hgsettings3) | |
219 | self.sa.add(hgsettings4) |
|
219 | self.sa.add(hgsettings4) | |
220 | self.sa.add(hgsettings5) |
|
220 | self.sa.add(hgsettings5) | |
221 | self.sa.add(hgsettings6) |
|
221 | self.sa.add(hgsettings6) | |
222 | self.sa.commit() |
|
222 | self.sa.commit() | |
223 |
|
223 | |||
224 | h.flash(_('Updated mercurial settings'), |
|
224 | h.flash(_('Updated mercurial settings'), | |
225 | category='success') |
|
225 | category='success') | |
226 |
|
226 | |||
227 | except: |
|
227 | except: | |
228 | log.error(traceback.format_exc()) |
|
228 | log.error(traceback.format_exc()) | |
229 | h.flash(_('error occurred during updating ' |
|
229 | h.flash(_('error occurred during updating ' | |
230 | 'application settings'), category='error') |
|
230 | 'application settings'), category='error') | |
231 |
|
231 | |||
232 | self.sa.rollback() |
|
232 | self.sa.rollback() | |
233 |
|
233 | |||
234 | except formencode.Invalid, errors: |
|
234 | except formencode.Invalid, errors: | |
235 | return htmlfill.render( |
|
235 | return htmlfill.render( | |
236 | render('admin/settings/settings.html'), |
|
236 | render('admin/settings/settings.html'), | |
237 | defaults=errors.value, |
|
237 | defaults=errors.value, | |
238 | errors=errors.error_dict or {}, |
|
238 | errors=errors.error_dict or {}, | |
239 | prefix_error=False, |
|
239 | prefix_error=False, | |
240 | encoding="UTF-8") |
|
240 | encoding="UTF-8") | |
241 |
|
241 | |||
242 | if setting_id == 'hooks': |
|
242 | if setting_id == 'hooks': | |
243 | ui_key = request.POST.get('new_hook_ui_key') |
|
243 | ui_key = request.POST.get('new_hook_ui_key') | |
244 | ui_value = request.POST.get('new_hook_ui_value') |
|
244 | ui_value = request.POST.get('new_hook_ui_value') | |
245 | try: |
|
245 | try: | |
246 |
|
246 | |||
247 | if ui_value and ui_key: |
|
247 | if ui_value and ui_key: | |
248 | RhodeCodeUi.create_or_update_hook(ui_key, ui_value) |
|
248 | RhodeCodeUi.create_or_update_hook(ui_key, ui_value) | |
249 | h.flash(_('Added new hook'), |
|
249 | h.flash(_('Added new hook'), | |
250 | category='success') |
|
250 | category='success') | |
251 |
|
251 | |||
252 | # check for edits |
|
252 | # check for edits | |
253 | update = False |
|
253 | update = False | |
254 | _d = request.POST.dict_of_lists() |
|
254 | _d = request.POST.dict_of_lists() | |
255 | for k, v in zip(_d.get('hook_ui_key', []), |
|
255 | for k, v in zip(_d.get('hook_ui_key', []), | |
256 | _d.get('hook_ui_value_new', [])): |
|
256 | _d.get('hook_ui_value_new', [])): | |
257 | RhodeCodeUi.create_or_update_hook(k, v) |
|
257 | RhodeCodeUi.create_or_update_hook(k, v) | |
258 | update = True |
|
258 | update = True | |
259 |
|
259 | |||
260 | if update: |
|
260 | if update: | |
261 | h.flash(_('Updated hooks'), category='success') |
|
261 | h.flash(_('Updated hooks'), category='success') | |
262 | self.sa.commit() |
|
262 | self.sa.commit() | |
263 | except: |
|
263 | except: | |
264 | log.error(traceback.format_exc()) |
|
264 | log.error(traceback.format_exc()) | |
265 | h.flash(_('error occurred during hook creation'), |
|
265 | h.flash(_('error occurred during hook creation'), | |
266 | category='error') |
|
266 | category='error') | |
267 |
|
267 | |||
268 | return redirect(url('admin_edit_setting', setting_id='hooks')) |
|
268 | return redirect(url('admin_edit_setting', setting_id='hooks')) | |
269 |
|
269 | |||
270 | if setting_id == 'email': |
|
270 | if setting_id == 'email': | |
271 | test_email = request.POST.get('test_email') |
|
271 | test_email = request.POST.get('test_email') | |
272 | test_email_subj = 'RhodeCode TestEmail' |
|
272 | test_email_subj = 'RhodeCode TestEmail' | |
273 | test_email_body = 'RhodeCode Email test' |
|
273 | test_email_body = 'RhodeCode Email test' | |
274 |
|
274 | |||
275 | test_email_html_body = EmailNotificationModel()\ |
|
275 | test_email_html_body = EmailNotificationModel()\ | |
276 | .get_email_tmpl(EmailNotificationModel.TYPE_DEFAULT, |
|
276 | .get_email_tmpl(EmailNotificationModel.TYPE_DEFAULT, | |
277 | body=test_email_body) |
|
277 | body=test_email_body) | |
278 |
|
278 | |||
279 | recipients = [test_email] if [test_email] else None |
|
279 | recipients = [test_email] if [test_email] else None | |
280 |
|
280 | |||
281 | run_task(tasks.send_email, recipients, test_email_subj, |
|
281 | run_task(tasks.send_email, recipients, test_email_subj, | |
282 | test_email_body, test_email_html_body) |
|
282 | test_email_body, test_email_html_body) | |
283 |
|
283 | |||
284 | h.flash(_('Email task created'), category='success') |
|
284 | h.flash(_('Email task created'), category='success') | |
285 | return redirect(url('admin_settings')) |
|
285 | return redirect(url('admin_settings')) | |
286 |
|
286 | |||
287 | @HasPermissionAllDecorator('hg.admin') |
|
287 | @HasPermissionAllDecorator('hg.admin') | |
288 | def delete(self, setting_id): |
|
288 | def delete(self, setting_id): | |
289 | """DELETE /admin/settings/setting_id: Delete an existing item""" |
|
289 | """DELETE /admin/settings/setting_id: Delete an existing item""" | |
290 | # Forms posted to this method should contain a hidden field: |
|
290 | # Forms posted to this method should contain a hidden field: | |
291 | # <input type="hidden" name="_method" value="DELETE" /> |
|
291 | # <input type="hidden" name="_method" value="DELETE" /> | |
292 | # Or using helpers: |
|
292 | # Or using helpers: | |
293 | # h.form(url('admin_setting', setting_id=ID), |
|
293 | # h.form(url('admin_setting', setting_id=ID), | |
294 | # method='delete') |
|
294 | # method='delete') | |
295 | # url('admin_setting', setting_id=ID) |
|
295 | # url('admin_setting', setting_id=ID) | |
296 | if setting_id == 'hooks': |
|
296 | if setting_id == 'hooks': | |
297 | hook_id = request.POST.get('hook_id') |
|
297 | hook_id = request.POST.get('hook_id') | |
298 | RhodeCodeUi.delete(hook_id) |
|
298 | RhodeCodeUi.delete(hook_id) | |
299 | self.sa.commit() |
|
299 | self.sa.commit() | |
300 |
|
300 | |||
301 | @HasPermissionAllDecorator('hg.admin') |
|
301 | @HasPermissionAllDecorator('hg.admin') | |
302 | def show(self, setting_id, format='html'): |
|
302 | def show(self, setting_id, format='html'): | |
303 | """ |
|
303 | """ | |
304 | GET /admin/settings/setting_id: Show a specific item""" |
|
304 | GET /admin/settings/setting_id: Show a specific item""" | |
305 | # url('admin_setting', setting_id=ID) |
|
305 | # url('admin_setting', setting_id=ID) | |
306 |
|
306 | |||
307 | @HasPermissionAllDecorator('hg.admin') |
|
307 | @HasPermissionAllDecorator('hg.admin') | |
308 | def edit(self, setting_id, format='html'): |
|
308 | def edit(self, setting_id, format='html'): | |
309 | """ |
|
309 | """ | |
310 | GET /admin/settings/setting_id/edit: Form to |
|
310 | GET /admin/settings/setting_id/edit: Form to | |
311 | edit an existing item""" |
|
311 | edit an existing item""" | |
312 | # url('admin_edit_setting', setting_id=ID) |
|
312 | # url('admin_edit_setting', setting_id=ID) | |
313 |
|
313 | |||
314 | c.hooks = RhodeCodeUi.get_builtin_hooks() |
|
314 | c.hooks = RhodeCodeUi.get_builtin_hooks() | |
315 | c.custom_hooks = RhodeCodeUi.get_custom_hooks() |
|
315 | c.custom_hooks = RhodeCodeUi.get_custom_hooks() | |
316 |
|
316 | |||
317 | return htmlfill.render( |
|
317 | return htmlfill.render( | |
318 | render('admin/settings/hooks.html'), |
|
318 | render('admin/settings/hooks.html'), | |
319 | defaults={}, |
|
319 | defaults={}, | |
320 | encoding="UTF-8", |
|
320 | encoding="UTF-8", | |
321 | force_defaults=False |
|
321 | force_defaults=False | |
322 | ) |
|
322 | ) | |
323 |
|
323 | |||
324 | @NotAnonymous() |
|
324 | @NotAnonymous() | |
325 | def my_account(self): |
|
325 | def my_account(self): | |
326 | """ |
|
326 | """ | |
327 | GET /_admin/my_account Displays info about my account |
|
327 | GET /_admin/my_account Displays info about my account | |
328 | """ |
|
328 | """ | |
329 | # url('admin_settings_my_account') |
|
329 | # url('admin_settings_my_account') | |
330 |
|
330 | |||
331 | c.user = User.get(self.rhodecode_user.user_id) |
|
331 | c.user = User.get(self.rhodecode_user.user_id) | |
332 | all_repos = self.sa.query(Repository)\ |
|
332 | all_repos = self.sa.query(Repository)\ | |
333 | .filter(Repository.user_id == c.user.user_id)\ |
|
333 | .filter(Repository.user_id == c.user.user_id)\ | |
334 | .order_by(func.lower(Repository.repo_name)).all() |
|
334 | .order_by(func.lower(Repository.repo_name)).all() | |
335 |
|
335 | |||
336 | c.user_repos = ScmModel().get_repos(all_repos) |
|
336 | c.user_repos = ScmModel().get_repos(all_repos) | |
337 |
|
337 | |||
338 | if c.user.username == 'default': |
|
338 | if c.user.username == 'default': | |
339 | h.flash(_("You can't edit this user since it's" |
|
339 | h.flash(_("You can't edit this user since it's" | |
340 | " crucial for entire application"), category='warning') |
|
340 | " crucial for entire application"), category='warning') | |
341 | return redirect(url('users')) |
|
341 | return redirect(url('users')) | |
342 |
|
342 | |||
343 | defaults = c.user.get_dict() |
|
343 | defaults = c.user.get_dict() | |
344 |
|
344 | |||
345 | c.form = htmlfill.render( |
|
345 | c.form = htmlfill.render( | |
346 | render('admin/users/user_edit_my_account_form.html'), |
|
346 | render('admin/users/user_edit_my_account_form.html'), | |
347 | defaults=defaults, |
|
347 | defaults=defaults, | |
348 | encoding="UTF-8", |
|
348 | encoding="UTF-8", | |
349 | force_defaults=False |
|
349 | force_defaults=False | |
350 | ) |
|
350 | ) | |
351 | return render('admin/users/user_edit_my_account.html') |
|
351 | return render('admin/users/user_edit_my_account.html') | |
352 |
|
352 | |||
|
353 | @NotAnonymous() | |||
353 | def my_account_update(self): |
|
354 | def my_account_update(self): | |
354 | """PUT /_admin/my_account_update: Update an existing item""" |
|
355 | """PUT /_admin/my_account_update: Update an existing item""" | |
355 | # Forms posted to this method should contain a hidden field: |
|
356 | # Forms posted to this method should contain a hidden field: | |
356 | # <input type="hidden" name="_method" value="PUT" /> |
|
357 | # <input type="hidden" name="_method" value="PUT" /> | |
357 | # Or using helpers: |
|
358 | # Or using helpers: | |
358 | # h.form(url('admin_settings_my_account_update'), |
|
359 | # h.form(url('admin_settings_my_account_update'), | |
359 | # method='put') |
|
360 | # method='put') | |
360 | # url('admin_settings_my_account_update', id=ID) |
|
361 | # url('admin_settings_my_account_update', id=ID) | |
361 | uid = self.rhodecode_user.user_id |
|
362 | uid = self.rhodecode_user.user_id | |
362 | email = self.rhodecode_user.email |
|
363 | email = self.rhodecode_user.email | |
363 | _form = UserForm(edit=True, |
|
364 | _form = UserForm(edit=True, | |
364 | old_data={'user_id': uid, 'email': email})() |
|
365 | old_data={'user_id': uid, 'email': email})() | |
365 | form_result = {} |
|
366 | form_result = {} | |
366 | try: |
|
367 | try: | |
367 | form_result = _form.to_python(dict(request.POST)) |
|
368 | form_result = _form.to_python(dict(request.POST)) | |
368 | UserModel().update_my_account(uid, form_result) |
|
369 | UserModel().update_my_account(uid, form_result) | |
369 | h.flash(_('Your account was updated successfully'), |
|
370 | h.flash(_('Your account was updated successfully'), | |
370 | category='success') |
|
371 | category='success') | |
371 | Session.commit() |
|
372 | Session.commit() | |
372 | except formencode.Invalid, errors: |
|
373 | except formencode.Invalid, errors: | |
373 | c.user = User.get(self.rhodecode_user.user_id) |
|
374 | c.user = User.get(self.rhodecode_user.user_id) | |
374 |
|
375 | |||
375 | c.form = htmlfill.render( |
|
376 | c.form = htmlfill.render( | |
376 | render('admin/users/user_edit_my_account_form.html'), |
|
377 | render('admin/users/user_edit_my_account_form.html'), | |
377 | defaults=errors.value, |
|
378 | defaults=errors.value, | |
378 | errors=errors.error_dict or {}, |
|
379 | errors=errors.error_dict or {}, | |
379 | prefix_error=False, |
|
380 | prefix_error=False, | |
380 | encoding="UTF-8") |
|
381 | encoding="UTF-8") | |
381 | return render('admin/users/user_edit_my_account.html') |
|
382 | return render('admin/users/user_edit_my_account.html') | |
382 | except Exception: |
|
383 | except Exception: | |
383 | log.error(traceback.format_exc()) |
|
384 | log.error(traceback.format_exc()) | |
384 | h.flash(_('error occurred during update of user %s') \ |
|
385 | h.flash(_('error occurred during update of user %s') \ | |
385 | % form_result.get('username'), category='error') |
|
386 | % form_result.get('username'), category='error') | |
386 |
|
387 | |||
387 | return redirect(url('my_account')) |
|
388 | return redirect(url('my_account')) | |
388 |
|
389 | |||
|
390 | @NotAnonymous() | |||
389 | def my_account_my_repos(self): |
|
391 | def my_account_my_repos(self): | |
390 | all_repos = self.sa.query(Repository)\ |
|
392 | all_repos = self.sa.query(Repository)\ | |
391 | .filter(Repository.user_id == self.rhodecode_user.user_id)\ |
|
393 | .filter(Repository.user_id == self.rhodecode_user.user_id)\ | |
392 | .order_by(func.lower(Repository.repo_name))\ |
|
394 | .order_by(func.lower(Repository.repo_name))\ | |
393 | .all() |
|
395 | .all() | |
394 | c.user_repos = ScmModel().get_repos(all_repos) |
|
396 | c.user_repos = ScmModel().get_repos(all_repos) | |
395 | return render('admin/users/user_edit_my_account_repos.html') |
|
397 | return render('admin/users/user_edit_my_account_repos.html') | |
396 |
|
398 | |||
|
399 | @NotAnonymous() | |||
397 | def my_account_my_pullrequests(self): |
|
400 | def my_account_my_pullrequests(self): | |
398 | c.my_pull_requests = PullRequest.query()\ |
|
401 | c.my_pull_requests = PullRequest.query()\ | |
399 | .filter(PullRequest.user_id== |
|
402 | .filter(PullRequest.user_id== | |
400 | self.rhodecode_user.user_id)\ |
|
403 | self.rhodecode_user.user_id)\ | |
401 | .all() |
|
404 | .all() | |
402 | c.participate_in_pull_requests = \ |
|
405 | c.participate_in_pull_requests = \ | |
403 | [x.pull_request for x in PullRequestReviewers.query()\ |
|
406 | [x.pull_request for x in PullRequestReviewers.query()\ | |
404 | .filter(PullRequestReviewers.user_id== |
|
407 | .filter(PullRequestReviewers.user_id== | |
405 | self.rhodecode_user.user_id)\ |
|
408 | self.rhodecode_user.user_id)\ | |
406 | .all()] |
|
409 | .all()] | |
407 | return render('admin/users/user_edit_my_account_pullrequests.html') |
|
410 | return render('admin/users/user_edit_my_account_pullrequests.html') | |
408 |
|
411 | |||
409 | @NotAnonymous() |
|
412 | @NotAnonymous() | |
410 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') |
|
413 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') | |
411 | def create_repository(self): |
|
414 | def create_repository(self): | |
412 | """GET /_admin/create_repository: Form to create a new item""" |
|
415 | """GET /_admin/create_repository: Form to create a new item""" | |
413 |
|
416 | |||
414 | c.repo_groups = RepoGroup.groups_choices() |
|
417 | c.repo_groups = RepoGroup.groups_choices() | |
415 | c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) |
|
418 | c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) | |
416 | choices, c.landing_revs = ScmModel().get_repo_landing_revs() |
|
419 | choices, c.landing_revs = ScmModel().get_repo_landing_revs() | |
417 |
|
420 | |||
418 | new_repo = request.GET.get('repo', '') |
|
421 | new_repo = request.GET.get('repo', '') | |
419 | c.new_repo = repo_name_slug(new_repo) |
|
422 | c.new_repo = repo_name_slug(new_repo) | |
420 |
|
423 | |||
421 | return render('admin/repos/repo_add_create_repository.html') |
|
424 | return render('admin/repos/repo_add_create_repository.html') | |
422 |
|
425 | |||
|
426 | @NotAnonymous() | |||
423 | def get_hg_ui_settings(self): |
|
427 | def get_hg_ui_settings(self): | |
424 | ret = self.sa.query(RhodeCodeUi).all() |
|
428 | ret = self.sa.query(RhodeCodeUi).all() | |
425 |
|
429 | |||
426 | if not ret: |
|
430 | if not ret: | |
427 | raise Exception('Could not get application ui settings !') |
|
431 | raise Exception('Could not get application ui settings !') | |
428 | settings = {} |
|
432 | settings = {} | |
429 | for each in ret: |
|
433 | for each in ret: | |
430 | k = each.ui_key |
|
434 | k = each.ui_key | |
431 | v = each.ui_value |
|
435 | v = each.ui_value | |
432 | if k == '/': |
|
436 | if k == '/': | |
433 | k = 'root_path' |
|
437 | k = 'root_path' | |
434 |
|
438 | |||
435 | if k.find('.') != -1: |
|
439 | if k.find('.') != -1: | |
436 | k = k.replace('.', '_') |
|
440 | k = k.replace('.', '_') | |
437 |
|
441 | |||
438 | if each.ui_section == 'hooks': |
|
442 | if each.ui_section == 'hooks': | |
439 | v = each.ui_active |
|
443 | v = each.ui_active | |
440 |
|
444 | |||
441 | settings[each.ui_section + '_' + k] = v |
|
445 | settings[each.ui_section + '_' + k] = v | |
442 |
|
446 | |||
443 | return settings |
|
447 | return settings |
General Comments 0
You need to be logged in to leave comments.
Login now