From 6a59a390060e5678e0bbf85578d67e20776e099a Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Tue, 17 Sep 2013 11:42:45 +0530 Subject: [PATCH] [wsgi] [minor] --- webnotes/__init__.py | 8 ++++--- webnotes/app.py | 53 ++++++++++++++++++-------------------------- webnotes/auth.py | 4 +--- webnotes/handler.py | 6 ++--- webnotes/sessions.py | 2 +- webnotes/webutils.py | 2 +- 6 files changed, 32 insertions(+), 43 deletions(-) diff --git a/webnotes/__init__.py b/webnotes/__init__.py index 916d25a3c8..7a2995bcd5 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -189,9 +189,11 @@ def connect(db_name=None, password=None): local.user = webnotes.profile.Profile('Administrator') def get_request_header(key, default=None): - return webnotes.request.headers.get(key, default) - -remote_ip = get_request_header('REMOTE_ADDR') #Required for login from python shell + try: + return request.headers.get(key, default) + except Exception, e: + return None + logger = None def get_db_password(db_name): diff --git a/webnotes/app.py b/webnotes/app.py index 8958db9a45..14b7c1f7b4 100644 --- a/webnotes/app.py +++ b/webnotes/app.py @@ -15,39 +15,25 @@ local_manager = LocalManager([webnotes.local]) @Request.application def application(request): - path = os.path.join("public", request.path[1:]) - if os.path.exists(path) and not os.path.isdir(path) and not path.endswith(".py"): - with open(path, "r") as static: - content = static.read() - - response = Response() - response.data = content - response.headers["Content-type"] = mimetypes.guess_type(path)[0] - return response + webnotes.local.request = request + webnotes.init() + + 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() }) + + webnotes.local._response = Response() + + try: + webnotes.http_request = webnotes.auth.HTTPRequest() + except webnotes.AuthenticationError, e: + pass + if webnotes.form_dict.cmd: + webnotes.handler.handle() else: - webnotes.local.request = request - webnotes.init() - - 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() }) - - webnotes.local._response = Response() - - try: - webnotes.http_request = webnotes.auth.HTTPRequest() - except webnotes.AuthenticationError, e: - pass - - # cookies - print request.form - - if webnotes.form_dict.cmd: - webnotes.handler.handle() - else: - webnotes.webutils.render(webnotes.request.path[1:]) - - return webnotes._response + webnotes.webutils.render(webnotes.request.path[1:]) + + return webnotes._response application = local_manager.make_middleware(application) @@ -55,4 +41,7 @@ if __name__ == '__main__': import sys from werkzeug.serving import run_simple - run_simple('localhost', 8000, application, use_reloader=True, use_debugger=True, use_evalex=True) \ No newline at end of file + run_simple('localhost', 8000, application, use_reloader=True, + use_debugger=True, use_evalex=True, static_files = { + "/": os.path.join(os.path.dirname(__file__), "..", "..", "public") + }) \ No newline at end of file diff --git a/webnotes/auth.py b/webnotes/auth.py index 70b0e8df5f..7e5ee8df25 100644 --- a/webnotes/auth.py +++ b/webnotes/auth.py @@ -21,8 +21,6 @@ class HTTPRequest: # language self.set_lang(webnotes.get_request_header('HTTP_ACCEPT_LANGUAGE')) - webnotes.remote_ip = webnotes.get_request_header('REMOTE_ADDR') - # load cookies webnotes.local.cookie_manager = CookieManager() @@ -173,7 +171,7 @@ class LoginManager: ip_list = [i.strip() for i in ip_list] for ip in ip_list: - if webnotes.remote_ip.startswith(ip): + if webnotes.get_request_header('REMOTE_ADDR', '').startswith(ip): return webnotes.msgprint('Not allowed from this IP Address') diff --git a/webnotes/handler.py b/webnotes/handler.py index f7394677d8..d847a7c21f 100755 --- a/webnotes/handler.py +++ b/webnotes/handler.py @@ -165,14 +165,14 @@ def print_csv(): "text/csv; charset: utf-8" webnotes._response.headers["Content-Disposition"] = \ "attachment; filename=%s.csv" % webnotes.response['doctype'].replace(' ', '_') - webnotes._response.response = webnotes.response['result'] + webnotes._response.data = webnotes.response['result'] def print_raw(): webnotes._response.headers["Content-Type"] = \ mimetypes.guess_type(webnotes.response['filename'])[0] or "application/unknown" webnotes._response.headers["Content-Disposition"] = \ "filename=%s" % webnotes.response['filename'].replace(' ', '_') - webnotes._response.response = webnotes.response['filecontent'] + webnotes._response.data = webnotes.response['filecontent'] def make_logs(): """make strings for msgprint and errprint""" @@ -196,7 +196,7 @@ def print_zip(response): webnotes._response.headers["Content-Encoding"] = "gzip" webnotes._response.headers["Content-Length"] = str(len(response)) - webnotes._response.response = response + webnotes._response.data = response def json_handler(obj): """serialize non-serializable data for json""" diff --git a/webnotes/sessions.py b/webnotes/sessions.py index 5153cace54..b2a5c86543 100644 --- a/webnotes/sessions.py +++ b/webnotes/sessions.py @@ -109,7 +109,7 @@ class Session: # update profile webnotes.conn.sql("""UPDATE tabProfile SET last_login = '%s', last_ip = '%s' - where name='%s'""" % (webnotes.utils.now(), webnotes.remote_ip, self.data['user'])) + where name='%s'""" % (webnotes.utils.now(), webnotes.get_request_header('REMOTE_ADDR'), self.data['user'])) webnotes.conn.commit() # set cookies to write diff --git a/webnotes/webutils.py b/webnotes/webutils.py index ac965e66aa..5c812bf3ce 100644 --- a/webnotes/webutils.py +++ b/webnotes/webutils.py @@ -20,7 +20,7 @@ def render(page_name): html = render_page('error') webnotes._response.headers["Content-Type"] = "text/html; charset: utf-8" - webnotes._response.response = html + webnotes._response.data = html def render_page(page_name): """get page html"""