@@ -189,9 +189,11 @@ def connect(db_name=None, password=None): | |||||
local.user = webnotes.profile.Profile('Administrator') | local.user = webnotes.profile.Profile('Administrator') | ||||
def get_request_header(key, default=None): | 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 | logger = None | ||||
def get_db_password(db_name): | def get_db_password(db_name): | ||||
@@ -15,39 +15,25 @@ local_manager = LocalManager([webnotes.local]) | |||||
@Request.application | @Request.application | ||||
def application(request): | 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: | 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) | application = local_manager.make_middleware(application) | ||||
@@ -55,4 +41,7 @@ if __name__ == '__main__': | |||||
import sys | import sys | ||||
from werkzeug.serving import run_simple | from werkzeug.serving import run_simple | ||||
run_simple('localhost', 8000, application, use_reloader=True, use_debugger=True, use_evalex=True) | |||||
run_simple('localhost', 8000, application, use_reloader=True, | |||||
use_debugger=True, use_evalex=True, static_files = { | |||||
"/": os.path.join(os.path.dirname(__file__), "..", "..", "public") | |||||
}) |
@@ -21,8 +21,6 @@ class HTTPRequest: | |||||
# language | # language | ||||
self.set_lang(webnotes.get_request_header('HTTP_ACCEPT_LANGUAGE')) | self.set_lang(webnotes.get_request_header('HTTP_ACCEPT_LANGUAGE')) | ||||
webnotes.remote_ip = webnotes.get_request_header('REMOTE_ADDR') | |||||
# load cookies | # load cookies | ||||
webnotes.local.cookie_manager = CookieManager() | webnotes.local.cookie_manager = CookieManager() | ||||
@@ -173,7 +171,7 @@ class LoginManager: | |||||
ip_list = [i.strip() for i in ip_list] | ip_list = [i.strip() for i in ip_list] | ||||
for ip in ip_list: | for ip in ip_list: | ||||
if webnotes.remote_ip.startswith(ip): | |||||
if webnotes.get_request_header('REMOTE_ADDR', '').startswith(ip): | |||||
return | return | ||||
webnotes.msgprint('Not allowed from this IP Address') | webnotes.msgprint('Not allowed from this IP Address') | ||||
@@ -165,14 +165,14 @@ def print_csv(): | |||||
"text/csv; charset: utf-8" | "text/csv; charset: utf-8" | ||||
webnotes._response.headers["Content-Disposition"] = \ | webnotes._response.headers["Content-Disposition"] = \ | ||||
"attachment; filename=%s.csv" % webnotes.response['doctype'].replace(' ', '_') | "attachment; filename=%s.csv" % webnotes.response['doctype'].replace(' ', '_') | ||||
webnotes._response.response = webnotes.response['result'] | |||||
webnotes._response.data = webnotes.response['result'] | |||||
def print_raw(): | def print_raw(): | ||||
webnotes._response.headers["Content-Type"] = \ | webnotes._response.headers["Content-Type"] = \ | ||||
mimetypes.guess_type(webnotes.response['filename'])[0] or "application/unknown" | mimetypes.guess_type(webnotes.response['filename'])[0] or "application/unknown" | ||||
webnotes._response.headers["Content-Disposition"] = \ | webnotes._response.headers["Content-Disposition"] = \ | ||||
"filename=%s" % webnotes.response['filename'].replace(' ', '_') | "filename=%s" % webnotes.response['filename'].replace(' ', '_') | ||||
webnotes._response.response = webnotes.response['filecontent'] | |||||
webnotes._response.data = webnotes.response['filecontent'] | |||||
def make_logs(): | def make_logs(): | ||||
"""make strings for msgprint and errprint""" | """make strings for msgprint and errprint""" | ||||
@@ -196,7 +196,7 @@ def print_zip(response): | |||||
webnotes._response.headers["Content-Encoding"] = "gzip" | webnotes._response.headers["Content-Encoding"] = "gzip" | ||||
webnotes._response.headers["Content-Length"] = str(len(response)) | webnotes._response.headers["Content-Length"] = str(len(response)) | ||||
webnotes._response.response = response | |||||
webnotes._response.data = response | |||||
def json_handler(obj): | def json_handler(obj): | ||||
"""serialize non-serializable data for json""" | """serialize non-serializable data for json""" | ||||
@@ -109,7 +109,7 @@ class Session: | |||||
# update profile | # update profile | ||||
webnotes.conn.sql("""UPDATE tabProfile SET last_login = '%s', last_ip = '%s' | 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() | webnotes.conn.commit() | ||||
# set cookies to write | # set cookies to write | ||||
@@ -20,7 +20,7 @@ def render(page_name): | |||||
html = render_page('error') | html = render_page('error') | ||||
webnotes._response.headers["Content-Type"] = "text/html; charset: utf-8" | webnotes._response.headers["Content-Type"] = "text/html; charset: utf-8" | ||||
webnotes._response.response = html | |||||
webnotes._response.data = html | |||||
def render_page(page_name): | def render_page(page_name): | ||||
"""get page html""" | """get page html""" | ||||