0
0
mirror of https://github.com/hexparrot/mineos synced 2025-10-06 00:02:56 +02:00

cleaned up mounting code

mounts are no longer passed paths, instead the variables are used
directly from the cherrypy.config
localization removed as instance variable; referenced as cherrypy.config
attribute instead
sanitization added to misc.localization
This commit is contained in:
William Dizon
2014-02-18 16:16:18 -07:00
parent 51245e1948
commit 83c7bfb48c
3 changed files with 23 additions and 20 deletions

13
auth.py
View File

@@ -77,10 +77,9 @@ def require(*conditions):
# Controller to provide login and logout actions
class AuthController(object):
def __init__(self, html_directory, localization):
self.html_directory = html_directory
self.localization = localization
def __init__(self):
self.html_directory = cherrypy.config['misc.html_directory']
def on_login(self, username):
"""Called on successful login"""
@@ -92,9 +91,11 @@ class AuthController(object):
from cherrypy.lib.static import serve_file
try:
return serve_file(os.path.join(self.html_directory, 'login_%s.html' % self.localization))
return serve_file(os.path.join(self.html_directory,
'login_%s.html' % cherrypy.config['misc.localization']))
except cherrypy.NotFound:
return serve_file(os.path.join(self.html_directory, 'login_en.html'))
return serve_file(os.path.join(self.html_directory,
'login_en.html'))
@cherrypy.expose
def login(self, username=None, password=None, hide=None, from_page='/'):

View File

@@ -26,8 +26,8 @@ def to_jsonable_type(retval):
return retval
class ViewModel(object):
def __init__(self, base_directory):
self.base_directory = base_directory
def __init__(self):
self.base_directory = cherrypy.config['misc.base_directory']
@property
def login(self):
@@ -206,10 +206,9 @@ class Root(object):
PROPERTIES = list(m for m in dir(mc) if not callable(getattr(mc,m)) \
and not m.startswith('_'))
def __init__(self, html_directory, base_directory, localization='en'):
self.html_directory = html_directory
self.base_directory = base_directory
self.localization = localization
def __init__(self):
self.html_directory = cherrypy.config['misc.html_directory']
self.base_directory = cherrypy.config['misc.base_directory']
@property
def login(self):
@@ -221,9 +220,11 @@ class Root(object):
from cherrypy.lib.static import serve_file
try:
return serve_file(os.path.join(self.html_directory, 'index_%s.html' % self.localization))
return serve_file(os.path.join(self.html_directory,
'index_%s.html' % cherrypy.config['misc.localization']))
except cherrypy.NotFound:
return serve_file(os.path.join(self.html_directory, 'index_en.html'))
return serve_file(os.path.join(self.html_directory,
'index_en.html'))
@cherrypy.expose
@require()

View File

@@ -133,6 +133,7 @@ if __name__ == "__main__":
################
html_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'html')
cherrypy.config['misc.html_directory'] = html_dir
cherrypy.config.update({
'tools.sessions.on': True,
@@ -256,12 +257,12 @@ if __name__ == "__main__":
pass
try:
localization = cherrypy.config['misc.localization']
except KeyError:
localization = 'en'
cherrypy.config['misc.localization'] = str(cherrypy.config['misc.localization']).lower()
except (KeyError, TypeError, AttributeError):
cherrypy.config['misc.localization'] = 'en'
cherrypy.tree.mount(mounts.Root(html_dir, base_dir, localization), "/", config=root_conf)
cherrypy.tree.mount(mounts.ViewModel(base_dir), "/vm", config=empty_conf)
cherrypy.tree.mount(auth.AuthController(html_dir, localization), '/auth', config=empty_conf)
cherrypy.tree.mount(mounts.Root(), "/", config=root_conf)
cherrypy.tree.mount(mounts.ViewModel(), "/vm", config=empty_conf)
cherrypy.tree.mount(auth.AuthController(), '/auth', config=empty_conf)
cherrypy.engine.start()
cherrypy.engine.block()