Show More
@@ -1,159 +1,151 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | # settings controller for pylons |
|
3 | # settings controller for pylons | |
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
5 |
|
5 | # | ||
6 | # This program is free software; you can redistribute it and/or |
|
6 | # This program is free software; you can redistribute it and/or | |
7 | # modify it under the terms of the GNU General Public License |
|
7 | # modify it under the terms of the GNU General Public License | |
8 | # as published by the Free Software Foundation; version 2 |
|
8 | # as published by the Free Software Foundation; version 2 | |
9 | # of the License or (at your opinion) any later version of the license. |
|
9 | # of the License or (at your opinion) any later version of the license. | |
10 | # |
|
10 | # | |
11 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
15 | # |
|
15 | # | |
16 | # You should have received a copy of the GNU General Public License |
|
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with this program; if not, write to the Free Software |
|
17 | # along with this program; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
19 | # MA 02110-1301, USA. |
|
19 | # MA 02110-1301, USA. | |
20 | """ |
|
20 | """ | |
21 | Created on July 14, 2010 |
|
21 | Created on July 14, 2010 | |
22 | settings controller for pylons |
|
22 | settings controller for pylons | |
23 | @author: marcink |
|
23 | @author: marcink | |
24 | """ |
|
24 | """ | |
25 | from formencode import htmlfill |
|
25 | from formencode import htmlfill | |
26 | from pylons import request, session, tmpl_context as c, url, app_globals as g, \ |
|
26 | from pylons import request, session, tmpl_context as c, url, app_globals as g, \ | |
27 | config |
|
27 | config | |
28 | from pylons.controllers.util import abort, redirect |
|
28 | from pylons.controllers.util import abort, redirect | |
29 | from pylons.i18n.translation import _ |
|
29 | from pylons.i18n.translation import _ | |
30 | from pylons_app.lib import helpers as h |
|
30 | from pylons_app.lib import helpers as h | |
31 | from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator |
|
31 | from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator | |
32 | from pylons_app.lib.base import BaseController, render |
|
32 | from pylons_app.lib.base import BaseController, render | |
33 | from pylons_app.lib.utils import repo2db_mapper, invalidate_cache, \ |
|
33 | from pylons_app.lib.utils import repo2db_mapper, invalidate_cache, \ | |
34 | set_hg_app_config |
|
34 | set_hg_app_config | |
35 | from pylons_app.model.db import User, UserLog, HgAppSettings |
|
35 | from pylons_app.model.db import User, UserLog, HgAppSettings | |
36 | from pylons_app.model.forms import UserForm, ApplicationSettingsForm |
|
36 | from pylons_app.model.forms import UserForm, ApplicationSettingsForm | |
37 | from pylons_app.model.hg_model import HgModel |
|
37 | from pylons_app.model.hg_model import HgModel | |
38 | from pylons_app.model.user_model import UserModel |
|
38 | from pylons_app.model.user_model import UserModel | |
39 | import formencode |
|
39 | import formencode | |
40 | import logging |
|
40 | import logging | |
41 | import traceback |
|
41 | import traceback | |
42 |
|
42 | |||
43 | log = logging.getLogger(__name__) |
|
43 | log = logging.getLogger(__name__) | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | class SettingsController(BaseController): |
|
46 | class SettingsController(BaseController): | |
47 | """REST Controller styled on the Atom Publishing Protocol""" |
|
47 | """REST Controller styled on the Atom Publishing Protocol""" | |
48 | # To properly map this controller, ensure your config/routing.py |
|
48 | # To properly map this controller, ensure your config/routing.py | |
49 | # file has a resource setup: |
|
49 | # file has a resource setup: | |
50 | # map.resource('setting', 'settings', controller='admin/settings', |
|
50 | # map.resource('setting', 'settings', controller='admin/settings', | |
51 | # path_prefix='/admin', name_prefix='admin_') |
|
51 | # path_prefix='/admin', name_prefix='admin_') | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | @LoginRequired() |
|
54 | @LoginRequired() | |
55 | #@HasPermissionAllDecorator('hg.admin') |
|
55 | #@HasPermissionAllDecorator('hg.admin') | |
56 | def __before__(self): |
|
56 | def __before__(self): | |
57 | c.admin_user = session.get('admin_user') |
|
57 | c.admin_user = session.get('admin_user') | |
58 | c.admin_username = session.get('admin_username') |
|
58 | c.admin_username = session.get('admin_username') | |
59 | super(SettingsController, self).__before__() |
|
59 | super(SettingsController, self).__before__() | |
60 |
|
60 | |||
61 | def index(self, format='html'): |
|
61 | def index(self, format='html'): | |
62 | """GET /admin/settings: All items in the collection""" |
|
62 | """GET /admin/settings: All items in the collection""" | |
63 | # url('admin_settings') |
|
63 | # url('admin_settings') | |
64 |
|
64 | |||
65 | hgsettings = self.sa.query(HgAppSettings).scalar() |
|
65 | hgsettings = self.sa.query(HgAppSettings).scalar() | |
66 | defaults = hgsettings.__dict__ if hgsettings else {} |
|
66 | defaults = hgsettings.__dict__ if hgsettings else {} | |
67 | return htmlfill.render( |
|
67 | return htmlfill.render( | |
68 | render('admin/settings/settings.html'), |
|
68 | render('admin/settings/settings.html'), | |
69 | defaults=defaults, |
|
69 | defaults=defaults, | |
70 | encoding="UTF-8", |
|
70 | encoding="UTF-8", | |
71 | force_defaults=False |
|
71 | force_defaults=False | |
72 | ) |
|
72 | ) | |
73 |
|
73 | |||
74 | def create(self): |
|
74 | def create(self): | |
75 | """POST /admin/settings: Create a new item""" |
|
75 | """POST /admin/settings: Create a new item""" | |
76 | # url('admin_settings') |
|
76 | # url('admin_settings') | |
77 |
|
77 | |||
78 | def new(self, format='html'): |
|
78 | def new(self, format='html'): | |
79 | """GET /admin/settings/new: Form to create a new item""" |
|
79 | """GET /admin/settings/new: Form to create a new item""" | |
80 | # url('admin_new_setting') |
|
80 | # url('admin_new_setting') | |
81 |
|
81 | |||
82 | def update(self, id): |
|
82 | def update(self, id): | |
83 | """PUT /admin/settings/id: Update an existing item""" |
|
83 | """PUT /admin/settings/id: Update an existing item""" | |
84 | # Forms posted to this method should contain a hidden field: |
|
84 | # Forms posted to this method should contain a hidden field: | |
85 | # <input type="hidden" name="_method" value="PUT" /> |
|
85 | # <input type="hidden" name="_method" value="PUT" /> | |
86 | # Or using helpers: |
|
86 | # Or using helpers: | |
87 | # h.form(url('admin_setting', id=ID), |
|
87 | # h.form(url('admin_setting', id=ID), | |
88 | # method='put') |
|
88 | # method='put') | |
89 | # url('admin_setting', id=ID) |
|
89 | # url('admin_setting', id=ID) | |
90 | if id == 'mapping': |
|
90 | if id == 'mapping': | |
91 | rm_obsolete = request.POST.get('destroy', False) |
|
91 | rm_obsolete = request.POST.get('destroy', False) | |
92 | log.debug('Rescanning directories with destroy=%s', rm_obsolete) |
|
92 | log.debug('Rescanning directories with destroy=%s', rm_obsolete) | |
93 |
|
93 | |||
94 | initial = HgModel.repo_scan(g.paths[0][0], g.paths[0][1], g.baseui) |
|
94 | initial = HgModel.repo_scan(g.paths[0][0], g.paths[0][1], g.baseui) | |
95 | repo2db_mapper(initial, rm_obsolete) |
|
95 | repo2db_mapper(initial, rm_obsolete) | |
96 | invalidate_cache('cached_repo_list') |
|
96 | invalidate_cache('cached_repo_list') | |
|
97 | h.flash(_('Repositories sucessfully rescanned'), category='success') | |||
97 |
|
98 | |||
98 | if id == 'global': |
|
99 | if id == 'global': | |
99 |
|
100 | |||
100 | application_form = ApplicationSettingsForm()() |
|
101 | application_form = ApplicationSettingsForm()() | |
101 | try: |
|
102 | try: | |
102 | form_result = application_form.to_python(dict(request.POST)) |
|
103 | form_result = application_form.to_python(dict(request.POST)) | |
103 | title = form_result['app_title'] |
|
104 | title = form_result['app_title'] | |
104 | realm = form_result['app_auth_realm'] |
|
105 | realm = form_result['app_auth_realm'] | |
105 |
|
106 | |||
106 | try: |
|
107 | try: | |
107 | hgsettings = self.sa.query(HgAppSettings).get(1) |
|
108 | hgsettings = self.sa.query(HgAppSettings).get(1) | |
108 | hgsettings.app_auth_realm = realm |
|
109 | hgsettings.app_auth_realm = realm | |
109 | hgsettings.app_title = title |
|
110 | hgsettings.app_title = title | |
110 |
|
111 | |||
111 | self.sa.add(hgsettings) |
|
112 | self.sa.add(hgsettings) | |
112 | self.sa.commit() |
|
113 | self.sa.commit() | |
113 | set_hg_app_config(config) |
|
114 | set_hg_app_config(config) | |
114 | h.flash(_('Updated application settings'), |
|
115 | h.flash(_('Updated application settings'), | |
115 | category='success') |
|
116 | category='success') | |
116 |
|
117 | |||
117 | except: |
|
118 | except: | |
118 | log.error(traceback.format_exc()) |
|
119 | log.error(traceback.format_exc()) | |
119 | h.flash(_('error occured during chaning application settings'), |
|
120 | h.flash(_('error occured during chaning application settings'), | |
120 | category='error') |
|
121 | category='error') | |
121 |
|
122 | |||
122 | self.sa.rollback() |
|
123 | self.sa.rollback() | |
123 |
|
124 | |||
124 |
|
125 | |||
125 | except formencode.Invalid as errors: |
|
126 | except formencode.Invalid as errors: | |
126 | c.form_errors = errors.error_dict |
|
|||
127 | return htmlfill.render( |
|
127 | return htmlfill.render( | |
128 | render('admin/settings/settings.html'), |
|
128 | render('admin/settings/settings.html'), | |
129 | defaults=errors.value, |
|
129 | defaults=errors.value, | |
|
130 | errors=errors.error_dict or {}, | |||
|
131 | prefix_error=False, | |||
130 | encoding="UTF-8") |
|
132 | encoding="UTF-8") | |
131 |
|
133 | |||
132 |
|
||||
133 |
|
||||
134 |
|
||||
135 |
|
||||
136 |
|
||||
137 |
|
||||
138 | return redirect(url('admin_settings')) |
|
134 | return redirect(url('admin_settings')) | |
139 |
|
135 | |||
140 |
|
||||
141 |
|
||||
142 |
|
||||
143 |
|
||||
144 | def delete(self, id): |
|
136 | def delete(self, id): | |
145 | """DELETE /admin/settings/id: Delete an existing item""" |
|
137 | """DELETE /admin/settings/id: Delete an existing item""" | |
146 | # Forms posted to this method should contain a hidden field: |
|
138 | # Forms posted to this method should contain a hidden field: | |
147 | # <input type="hidden" name="_method" value="DELETE" /> |
|
139 | # <input type="hidden" name="_method" value="DELETE" /> | |
148 | # Or using helpers: |
|
140 | # Or using helpers: | |
149 | # h.form(url('admin_setting', id=ID), |
|
141 | # h.form(url('admin_setting', id=ID), | |
150 | # method='delete') |
|
142 | # method='delete') | |
151 | # url('admin_setting', id=ID) |
|
143 | # url('admin_setting', id=ID) | |
152 |
|
144 | |||
153 | def show(self, id, format='html'): |
|
145 | def show(self, id, format='html'): | |
154 | """GET /admin/settings/id: Show a specific item""" |
|
146 | """GET /admin/settings/id: Show a specific item""" | |
155 | # url('admin_setting', id=ID) |
|
147 | # url('admin_setting', id=ID) | |
156 |
|
148 | |||
157 | def edit(self, id, format='html'): |
|
149 | def edit(self, id, format='html'): | |
158 | """GET /admin/settings/id/edit: Form to edit an existing item""" |
|
150 | """GET /admin/settings/id/edit: Form to edit an existing item""" | |
159 | # url('admin_edit_setting', id=ID) |
|
151 | # url('admin_edit_setting', id=ID) |
General Comments 0
You need to be logged in to leave comments.
Login now