when running plinth with default server_dir '/' some static files and redirects were wrong -- fixed that;

This commit is contained in:
fonfon 2014-07-09 00:58:20 +00:00
parent a8ed02b723
commit d4d6948eb9
4 changed files with 46 additions and 22 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
current-*.tar.gz
*.pyc
*.py.bak
*.swp
*.tiny.css
data/*.log
data/cherrypy_sessions
@ -28,4 +29,4 @@ data/plinth.sqlite3
predepend
build/
*.pid
.emacs.desktop*
.emacs.desktop*

View File

@ -101,7 +101,7 @@ def setup_server():
'/': {'tools.staticdir.root': '%s/static' % cfg.file_root,
'tools.staticdir.on': True,
'tools.staticdir.dir': '.'}}
cherrypy.tree.mount(None, cfg.server_dir + '/static', config)
cherrypy.tree.mount(None, django.conf.settings.STATIC_URL, config)
if not cfg.no_daemon:
Daemonizer(cherrypy.engine).subscribe()
@ -185,7 +185,7 @@ def configure_django():
ROOT_URLCONF='urls',
SESSION_ENGINE='django.contrib.sessions.backends.file',
SESSION_FILE_PATH=sessions_directory,
STATIC_URL=cfg.server_dir + '/static/',
STATIC_URL=os.path.join(cfg.server_dir, 'static/'),
TEMPLATE_CONTEXT_PROCESSORS=context_processors,
TEMPLATE_DIRS=template_directories)

50
util.py
View File

@ -1,24 +1,48 @@
import os
from django.http.response import HttpResponseRedirect
import cfg
class PlinthRedirect(HttpResponseRedirect):
"""
We do not fully use django and thus cannot use its named URLs to construct
links/redirects, so we have to take care of cfg.server_dir manually.
This temporary helper class makes sure that plinth-internal redirects
have the correct server_dir prefix.
"""
def __init__(self, redirect_to, *args, **kwargs):
if not redirect_to.startswith(cfg.server_dir):
redirect_to = urljoin([cfg.server_dir, redirect_to])
return super(PlinthRedirect, self).__init__(redirect_to,
*args, **kwargs)
def urljoin(parts):
"""
urllibs' urljoin joins ("foo", "/bar") to "/bar".
Instead, just concatenate the parts with "/" to i.e. /foo/bar
"""
return '/' + '/'.join(s.strip('/') for s in parts)
def mkdir(newdir):
"""works the way a good mkdir should :)
"""works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make them as well
"""
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired " \
"""
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired " \
"dir, '%s', already exists." % newdir)
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
mkdir(head)
#print "mkdir %s" % repr(newdir)
if tail:
os.mkdir(newdir)
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
mkdir(head)
#print "mkdir %s" % repr(newdir)
if tail:
os.mkdir(newdir)
def slurp(filespec):

View File

@ -19,7 +19,7 @@
Main Plinth views
"""
from django.http.response import HttpResponseRedirect
from util import PlinthRedirect
import logging
import cfg
@ -37,14 +37,13 @@ def index(request):
# Permanent redirect causes the browser to cache the redirect,
# preventing the user from navigating to /plinth until the
# browser is restarted.
return HttpResponseRedirect(cfg.server_dir + '/firstboot')
return PlinthRedirect('/firstboot')
if database['state'] < 5:
LOGGER.info('First boot state - %d', database['state'])
return HttpResponseRedirect(
cfg.server_dir + '/firstboot/state%d' % database['state'])
return PlinthRedirect('/firstboot/state%d' % database['state'])
if request.user.is_authenticated():
return HttpResponseRedirect(cfg.server_dir + '/apps')
return PlinthRedirect('/apps')
return HttpResponseRedirect(cfg.server_dir + '/help/about')
return PlinthRedirect('/help/about')