comparison pylons_app/controllers/admin/settings.py @ 350:664a5b8c551a

Added application settings, are now customizable from database fixed all instances of sqlachemy to be removed() after execution.
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 14 Jul 2010 18:31:06 +0200
parents e8fc875467bd
children 339d1368c10d
comparison
equal deleted inserted replaced
349:031152a540c5 350:664a5b8c551a
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 #
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 from pylons.controllers.util import abort, redirect 28 from pylons.controllers.util import abort, redirect
28 from pylons.i18n.translation import _ 29 from pylons.i18n.translation import _
29 from pylons_app.lib import helpers as h 30 from pylons_app.lib import helpers as h
30 from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator 31 from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator
31 from pylons_app.lib.base import BaseController, render 32 from pylons_app.lib.base import BaseController, render
32 from pylons_app.lib.utils import repo2db_mapper, invalidate_cache 33 from pylons_app.lib.utils import repo2db_mapper, invalidate_cache, \
33 from pylons_app.model.db import User, UserLog 34 set_hg_app_config
34 from pylons_app.model.forms import UserForm 35 from pylons_app.model.db import User, UserLog, HgAppSettings
36 from pylons_app.model.forms import UserForm, ApplicationSettingsForm
35 from pylons_app.model.hg_model import HgModel 37 from pylons_app.model.hg_model import HgModel
36 from pylons_app.model.user_model import UserModel 38 from pylons_app.model.user_model import UserModel
37 import formencode 39 import formencode
38 import logging 40 import logging
39 41 import traceback
42
40 log = logging.getLogger(__name__) 43 log = logging.getLogger(__name__)
41 44
42 45
43 class SettingsController(BaseController): 46 class SettingsController(BaseController):
44 """REST Controller styled on the Atom Publishing Protocol""" 47 """REST Controller styled on the Atom Publishing Protocol"""
56 super(SettingsController, self).__before__() 59 super(SettingsController, self).__before__()
57 60
58 def index(self, format='html'): 61 def index(self, format='html'):
59 """GET /admin/settings: All items in the collection""" 62 """GET /admin/settings: All items in the collection"""
60 # url('admin_settings') 63 # url('admin_settings')
61 return render('admin/settings/settings.html') 64
65 hgsettings = self.sa.query(HgAppSettings).scalar()
66 defaults = hgsettings.__dict__ if hgsettings else {}
67 return htmlfill.render(
68 render('admin/settings/settings.html'),
69 defaults=defaults,
70 encoding="UTF-8",
71 force_defaults=False
72 )
62 73
63 def create(self): 74 def create(self):
64 """POST /admin/settings: Create a new item""" 75 """POST /admin/settings: Create a new item"""
65 # url('admin_settings') 76 # url('admin_settings')
66 77
81 log.debug('Rescanning directories with destroy=%s', rm_obsolete) 92 log.debug('Rescanning directories with destroy=%s', rm_obsolete)
82 93
83 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)
84 repo2db_mapper(initial, rm_obsolete) 95 repo2db_mapper(initial, rm_obsolete)
85 invalidate_cache('cached_repo_list') 96 invalidate_cache('cached_repo_list')
97
98 if id == 'global':
86 99
100 application_form = ApplicationSettingsForm()()
101 try:
102 form_result = application_form.to_python(dict(request.POST))
103 title = form_result['app_title']
104 realm = form_result['app_auth_realm']
105
106 try:
107 hgsettings = self.sa.query(HgAppSettings).get(1)
108 hgsettings.app_auth_realm = realm
109 hgsettings.app_title = title
110
111 self.sa.add(hgsettings)
112 self.sa.commit()
113 set_hg_app_config(config)
114 h.flash(_('Updated application settings'),
115 category='success')
116
117 except:
118 log.error(traceback.format_exc())
119 h.flash(_('error occured during chaning application settings'),
120 category='error')
121
122 self.sa.rollback()
123
124
125 except formencode.Invalid as errors:
126 c.form_errors = errors.error_dict
127 return htmlfill.render(
128 render('admin/settings/settings.html'),
129 defaults=errors.value,
130 encoding="UTF-8")
131
132
133
134
135
136
87 137
88 return redirect(url('admin_settings')) 138 return redirect(url('admin_settings'))
89 139
90 140
91 141