Config API, session is used only when filesystem is not available for storing data
This commit is contained in:
@@ -11,10 +11,11 @@ import re
|
|||||||
from gluon.admin import *
|
from gluon.admin import *
|
||||||
from gluon.fileutils import abspath, read_file, write_file
|
from gluon.fileutils import abspath, read_file, write_file
|
||||||
from gluon.utils import web2py_uuid
|
from gluon.utils import web2py_uuid
|
||||||
|
from gluon.tools import Config
|
||||||
from glob import glob
|
from glob import glob
|
||||||
import shutil
|
import shutil
|
||||||
import platform
|
import platform
|
||||||
import ConfigParser
|
|
||||||
try:
|
try:
|
||||||
import git
|
import git
|
||||||
if git.__version__ < '0.3.1':
|
if git.__version__ < '0.3.1':
|
||||||
@@ -561,16 +562,10 @@ def edit():
|
|||||||
""" File edit handler """
|
""" File edit handler """
|
||||||
# Load json only if it is ajax edited...
|
# Load json only if it is ajax edited...
|
||||||
app = get_app(request.vars.app)
|
app = get_app(request.vars.app)
|
||||||
admin_path = apath("admin", r=request)
|
app_path = apath(app, r=request)
|
||||||
editor_defaults={'theme':'web2py'}
|
editor_defaults={'theme':'web2py'}
|
||||||
config = ConfigParser.ConfigParser(editor_defaults)
|
config = Config(os.path.join(app_path, 'settings.cfg'), section='editor', default_values=editor_defaults)
|
||||||
config.read( os.path.join(admin_path, 'settings.cfg') )
|
preferences = config.read()
|
||||||
if not config.has_section('editor'):
|
|
||||||
config.add_section('editor')
|
|
||||||
if not( isinstance(session.editor_settings, dict) ):
|
|
||||||
preferences = dict(config.items('editor'))
|
|
||||||
else:
|
|
||||||
preferences = session.editor_settings
|
|
||||||
|
|
||||||
if not(request.ajax):
|
if not(request.ajax):
|
||||||
# return the scaffolding, the rest will be through ajax requests
|
# return the scaffolding, the rest will be through ajax requests
|
||||||
@@ -586,18 +581,14 @@ def edit():
|
|||||||
editarea_preferences[key] = globals()[key]
|
editarea_preferences[key] = globals()[key]
|
||||||
return response.render ('default/edit.html', dict(app=request.args[0], editor_settings=preferences, editarea_preferences=editarea_preferences))
|
return response.render ('default/edit.html', dict(app=request.args[0], editor_settings=preferences, editarea_preferences=editarea_preferences))
|
||||||
|
|
||||||
# show settings tab and save prefernces
|
# show settings tab and save prefernces
|
||||||
if 'settings' in request.vars:
|
if 'settings' in request.vars:
|
||||||
if request.post_vars: #save new preferences
|
if request.post_vars: #save new preferences
|
||||||
for option, value in request.post_vars.items():
|
if config.save(request.post_vars.items()):
|
||||||
config.set('editor', option, value)
|
|
||||||
try:
|
|
||||||
config.write(open(os.path.join(admin_path, 'settings.cfg'), 'w'))
|
|
||||||
response.headers["web2py-component-flash"] = T('Preferences saved correctly')
|
response.headers["web2py-component-flash"] = T('Preferences saved correctly')
|
||||||
except:
|
else:
|
||||||
response.headers["web2py-component-flash"] = T('Preferences saved on session only')
|
response.headers["web2py-component-flash"] = T('Preferences saved on session only')
|
||||||
session.editor_settings = dict(config.items('editor'))
|
response.headers["web2py-component-command"] = "update_theme('%s'); jQuery('a[href=#editor_settings] button.close').click();" % config.read()['theme']
|
||||||
response.headers["web2py-component-command"] = "update_theme('%s'); jQuery('a[href=#editor_settings] button.close').click();" % config.get('editor', 'theme')
|
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
details = {'filename':'settings', 'id':'editor_settings', 'force': False}
|
details = {'filename':'settings', 'id':'editor_settings', 'force': False}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import urllib
|
|||||||
import urllib2
|
import urllib2
|
||||||
import Cookie
|
import Cookie
|
||||||
import cStringIO
|
import cStringIO
|
||||||
|
import ConfigParser
|
||||||
from email import MIMEBase, MIMEMultipart, MIMEText, Encoders, Header, message_from_string, Charset
|
from email import MIMEBase, MIMEMultipart, MIMEText, Encoders, Header, message_from_string, Charset
|
||||||
|
|
||||||
from gluon.contenttype import contenttype
|
from gluon.contenttype import contenttype
|
||||||
@@ -5643,6 +5644,37 @@ class Wiki(object):
|
|||||||
request = current.request
|
request = current.request
|
||||||
return render(request.post_vars)
|
return render(request.post_vars)
|
||||||
|
|
||||||
|
class Config(object):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
filename,
|
||||||
|
section,
|
||||||
|
default_values={}
|
||||||
|
):
|
||||||
|
self.config = ConfigParser.ConfigParser(default_values)
|
||||||
|
self.config.read(filename)
|
||||||
|
if not self.config.has_section(section):
|
||||||
|
self.config.add_section(section)
|
||||||
|
self.section = section
|
||||||
|
self.filename = filename
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
if not( isinstance(current.session['settings_%s' % self.section], dict) ):
|
||||||
|
settings = dict(self.config.items(self.section))
|
||||||
|
else:
|
||||||
|
settings = current.session['settings_%s' % self.section]
|
||||||
|
return settings
|
||||||
|
|
||||||
|
def save(self, options):
|
||||||
|
for option, value in options:
|
||||||
|
self.config.set(self.section, option, value)
|
||||||
|
try:
|
||||||
|
self.config.write(open(self.filename, 'w'))
|
||||||
|
result = True
|
||||||
|
except:
|
||||||
|
current.session['settings_%s' % self.section] = dict(self.config.items(self.section))
|
||||||
|
result = False
|
||||||
|
return result
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import doctest
|
import doctest
|
||||||
|
|||||||
Reference in New Issue
Block a user