diff --git a/.gitignore b/.gitignore index ba9c727d6..3b9739d04 100644 --- a/.gitignore +++ b/.gitignore @@ -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* \ No newline at end of file +.emacs.desktop* diff --git a/plinth.py b/plinth.py index 674835196..b689f0e3b 100755 --- a/plinth.py +++ b/plinth.py @@ -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) diff --git a/util.py b/util.py index f922fa51f..2bc550ca1 100644 --- a/util.py +++ b/util.py @@ -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): diff --git a/views.py b/views.py index 9bcad38d9..0c4cef271 100644 --- a/views.py +++ b/views.py @@ -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')