From c8820de5b13139f7d168adb8861ba7fa64bc3961 Mon Sep 17 00:00:00 2001 From: Pratik Vyas Date: Tue, 31 Dec 2013 16:23:38 +0530 Subject: [PATCH] fix per site request handling with no asset symlinking --- webnotes/app.py | 31 ++++++++++++++++++++----------- webnotes/middlewares.py | 22 ---------------------- 2 files changed, 20 insertions(+), 33 deletions(-) delete mode 100644 webnotes/middlewares.py diff --git a/webnotes/app.py b/webnotes/app.py index 4e38a8764d..317111e3c4 100644 --- a/webnotes/app.py +++ b/webnotes/app.py @@ -10,7 +10,7 @@ sys.path.insert(0, 'lib') from werkzeug.wrappers import Request, Response from werkzeug.local import LocalManager -from webnotes.middlewares import StaticDataMiddleware +from werkzeug.wsgi import SharedDataMiddleware from werkzeug.exceptions import HTTPException, NotFound from werkzeug.contrib.profiler import ProfilerMiddleware @@ -19,9 +19,11 @@ import webnotes import webnotes.handler import webnotes.auth import webnotes.webutils +from webnotes.utils import get_site_name local_manager = LocalManager([webnotes.local]) -site = None + +_site = None def handle_session_stopped(): res = Response(""" @@ -41,7 +43,12 @@ def application(request): webnotes.local.request = request try: - webnotes.init(site=site or request.host) + site = _site or get_site_name(request.host) + webnotes.init(site=site) + + if not webnotes.local.conf: + # site does not exist + raise NotFound webnotes.local.form_dict = webnotes._dict({ k:v[0] if isinstance(v, (list, tuple)) else v \ for k, v in (request.form or request.args).iteritems() }) @@ -74,10 +81,9 @@ def application(request): application = local_manager.make_middleware(application) -def serve(port=8000, profile=False): - global application, site - - site = webnotes.local.site +def serve(port=8000, profile=False, site=None): + global application, _site + _site = site from werkzeug.serving import run_simple @@ -85,11 +91,14 @@ def serve(port=8000, profile=False): application = ProfilerMiddleware(application) if not os.environ.get('NO_STATICS'): - application = StaticDataMiddleware(application, { - '/': 'public', + application = SharedDataMiddleware(application, { + '/assets': 'assets', + }) + + if site: + application = SharedDataMiddleware(application, { + '/files': os.path.join(site, 'public', 'files') }) - application.site = site - run_simple('0.0.0.0', int(port), application, use_reloader=True, use_debugger=True, use_evalex=True) diff --git a/webnotes/middlewares.py b/webnotes/middlewares.py deleted file mode 100644 index e8d1cda030..0000000000 --- a/webnotes/middlewares.py +++ /dev/null @@ -1,22 +0,0 @@ -from __future__ import unicode_literals - -import webnotes -import os - -from werkzeug.wsgi import SharedDataMiddleware -from webnotes.utils import get_site_name, get_site_path, get_site_base_path, get_path, cstr - -class StaticDataMiddleware(SharedDataMiddleware): - def __call__(self, environ, start_response): - self.environ = environ - return super(StaticDataMiddleware, self).__call__(environ, start_response) - - def get_directory_loader(self, directory): - def loader(path): - filepath = os.path.join(os.path.join(".", self.site), directory, path) - if os.path.isfile(filepath): - return os.path.basename(filepath), self._opener(filepath) - else: - return None, None - - return loader