def __init__(self, request): self.request = request self.session = request['Session'] self.form_data = request['Post'] self.setting = request['Setting'] self.set_title(u'管理画面') self.dba = DBAccess.order()
def __init__(self,request): self.request = request self.session = request['Session'] self.form_data = request['Post'] self.setting = request['Setting'] self.set_title(u'ログイン') self.dba = DBAccess.order()
def get_problems(): problems = [] dba = DBAccess.order() res = dba.select('problem_tbl', '*') for data in res: p = Problem() for k,v in data.items(): p.setvalue(k,v) problems.append(p) return problems
def get_user_by_username(username): if username is None: return None dba = DBAccess.order() data = dba.select('user_tbl', '*', {'username': username}) user = None if len(data) == 1: user = User() for k, v in data[0].items(): user[k] = v return user
def get_user_by_hash(key): if key is None: return None dba = DBAccess.order() data = dba.select('user_tbl', '*', {'hash': key}) user = None if len(data) == 1: user = User() for k, v in data[0].items(): user[k] = v return user
def get_user_by_username(username): if username is None: return None dba = DBAccess.order() data = dba.select('user_tbl', '*', {'username':username}) user = None if len(data) == 1: user = User() for k,v in data[0].items(): user[k] = v return user
def get_user_by_hash(key): if key is None: return None dba = DBAccess.order() data = dba.select('user_tbl', '*', {'hash':key}) user = None if len(data) == 1: user = User() for k,v in data[0].items(): user[k] = v return user
def wsgi(self, environ, start_response): """ レスポンスのボディを返す。 environ HTTP リクエストが格納された辞書。 start_response 呼び出し可能オブジェクト。 start_response を使って、ステータスコードとレスポンスヘッダを出力する。 """ self.error_info = [] # 設定 setting = self.setting() # ルーティング情報取得 route = environ.get('PATH_INFO', '').strip('/').split('/') # POSTデータ取出し """ post = MyStrage() if environ.get('REQUEST_METHOD') == 'POST': content_length = environ.get('CONTENT_LENGTH', '') if not content_length.isalnum(): content_length = 0 else: wsgi_input = environ['wsgi.input'] input_parse = cgi.parse_qsl(wsgi_input.read(int(content_length))) for value in input_parse: post.setvalue(value[0], value[1]) """ if environ['REQUEST_METHOD'].upper() == 'POST': post = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ, keep_blank_values=1) else: post = cgi.FieldStorage() # データベース dba = DBAccess.create(setting['database']) if setting['database']['create'].upper() == 'ON': self.create_table(dba) # クッキー cookie=SimpleCookie(environ.get('HTTP_COOKIE','')) # セッション session_id = None if setting['session']['id'] in cookie: session_id = cookie[setting['session']['id']].value session = Session.create(setting['session'], session_id) # リクエスト情報を設定 request = MyStrage() request.setvalue('Setting', setting) request.setvalue('Session', session) request.setvalue('DBA', dba) request.setvalue('Route', route) request.setvalue('Post', post) request.setvalue('Cookie', cookie) request.setvalue('Environ', environ) self.request = request # レスポンスを作成 response = self.routing(request) #デバッグ出力 debug_output = '' if setting['debug']['enable'].upper() == 'ON': debug_output = self.debug() session.close() response_header =[('Content-type', 'text/html;charset=utf-8')] if session.get_session_id(): # クッキーにセッションIDをセット response_header.append(('Set-Cookie', '%s=%s'%(setting['session']['id'],session.get_session_id()))) # レスポンス print(response_header) print(u'%s'%debug_output) print(u'%s\n'%response) if response is not None: start_response("200 OK", response_header) return (u'%s%s'%(debug_output,response)).encode('utf-8') else: start_response("404 Not Found", response_header) return (u'%sページが見つかりません'%(debug_output)).encode('utf-8')
def __init__(self, user_id=None, username=None, password=None, salt=''): self.dba = DBAccess.order() self.data = {} self.select(user_id, username, password, salt)
def __init__(self, setting): self.setting = setting self.dba = DBAccess.order()
# self.dba.close() def gc(self, lifetime): """ ガーベジコレクション """ self.dba.execute_sql( "DELETE FROM session_tbl WHERE update_time+:lifetime > :now", {"lifetime": lifetime, "now": time.mktime(gmtime())}, ) self.dba.commit() if __name__ == "__main__": import uuid setting = {"sql": "sqlite", "host": "", "db": ":memory:"} lifetime = 1440 dba = DBAccess.create(setting) id = None for i in range(10): data = handler.read(id, lifetime) if data is None: data = "" print(data) handler.delete(id) if id is None: id = "%s" % uuid.uuid4() data += "A" handler.write(id, data)
def wsgi(self, environ, start_response): """ レスポンスのボディを返す。 environ HTTP リクエストが格納された辞書。 start_response 呼び出し可能オブジェクト。 start_response を使って、ステータスコードとレスポンスヘッダを出力する。 """ self.error_info = [] # 設定 setting = self.setting() # ルーティング情報取得 route = environ.get('PATH_INFO', '').strip('/').split('/') # POSTデータ取出し """ post = MyStrage() if environ.get('REQUEST_METHOD') == 'POST': content_length = environ.get('CONTENT_LENGTH', '') if not content_length.isalnum(): content_length = 0 else: wsgi_input = environ['wsgi.input'] input_parse = cgi.parse_qsl(wsgi_input.read(int(content_length))) for value in input_parse: post.setvalue(value[0], value[1]) """ if environ['REQUEST_METHOD'].upper() == 'POST': post = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ, keep_blank_values=1) else: post = cgi.FieldStorage() # データベース dba = DBAccess.create(setting['database']) if setting['database']['create'].upper() == 'ON': self.create_table(dba) # クッキー cookie = SimpleCookie(environ.get('HTTP_COOKIE', '')) # セッション session_id = None if setting['session']['id'] in cookie: session_id = cookie[setting['session']['id']].value session = Session.create(setting['session'], session_id) # リクエスト情報を設定 request = MyStrage() request.setvalue('Setting', setting) request.setvalue('Session', session) request.setvalue('DBA', dba) request.setvalue('Route', route) request.setvalue('Post', post) request.setvalue('Cookie', cookie) request.setvalue('Environ', environ) self.request = request # レスポンスを作成 response = self.routing(request) #デバッグ出力 debug_output = '' if setting['debug']['enable'].upper() == 'ON': debug_output = self.debug() session.close() response_header = [('Content-type', 'text/html;charset=utf-8')] if session.get_session_id(): # クッキーにセッションIDをセット response_header.append( ('Set-Cookie', '%s=%s' % (setting['session']['id'], session.get_session_id()))) # レスポンス print(response_header) print(u'%s' % debug_output) print(u'%s\n' % response) if response is not None: start_response("200 OK", response_header) return (u'%s%s' % (debug_output, response)).encode('utf-8') else: start_response("404 Not Found", response_header) return (u'%sページが見つかりません' % (debug_output)).encode('utf-8')
""" ガーベジコレクション """ self.dba.execute_sql( 'DELETE FROM session_tbl WHERE update_time+:lifetime > :now', { 'lifetime': lifetime, 'now': time.mktime(gmtime()) }) self.dba.commit() if __name__ == '__main__': import uuid setting = { 'sql': 'sqlite', 'host': '', 'db': ':memory:', } lifetime = 1440 dba = DBAccess.create(setting) id = None for i in range(10): data = handler.read(id, lifetime) if data is None: data = '' print(data) handler.delete(id) if id is None: id = '%s' % uuid.uuid4() data += 'A' handler.write(id, data)
def __init__(self, problem_id=None): self.dba = DBAccess.order() self.data = {} if problem_id != None: self.select(problem_id)