def test_get(): if not is_access_allowed_ip_addr() or not is_user_logged_in(): return make_resp() query = 'INSERT INTO test1(col01, col02, col03, col04, col05, col06, col07, col08, col09, col10, col11, ' \ 'col12) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW()) ' params = (255, 65535, 429496729, 1844674407370955161, 5.01, 6.01, 'param7', 'param8', 'param9', '{0:%Y-%m-%d %H:%M:%S}'.format(datetime.now()), None) connect_database('test1') g.cursor['test1'].execute(query, params) g.db['test1'].commit() last_row_id = g.cursor['test1'].lastrowid query = 'UPDATE test1 SET col01 = %s WHERE test1_id = %s' params = (254, last_row_id) g.cursor['test1'].execute(query, params) g.db['test1'].commit() row_count = g.cursor['test1'].rowcount g.res.err = Error.NONE g.res.dat = { 'test_get': 'OK', 'a': request.args.get('a'), 'b': request.args.get('b'), 'last_row_id': last_row_id, 'row_count': row_count } return make_resp()
def test_orm(): if not is_access_allowed_ip_addr() or not is_user_logged_in(): return make_resp() new_test1 = Test1( col01=255, col02=65535, col03=429496729, col04=1844674407370955161, col05=5.01, col06=6.01, col07='param7', col08='param8', col09='param9'.encode('utf-8'), col10='{0:%Y-%m-%d %H:%M:%S}'.format(datetime.now()), col12='{0:%Y-%m-%d %H:%M:%S}'.format(datetime.now()), ) db.session.add(new_test1) db.session.commit() rows = json_serializable( db.session.query(Test1).order_by( Test1.test1_id.desc()).limit(1).offset(0).all()) g.res.err = Error.NONE g.res.dat = {'test_orm': 'OK', 'rows': rows} return make_resp()
def login(): if not is_access_allowed_ip_addr(): return make_resp() req_json = request.get_json() user_id = req_json.get('user_id') md5_hash = req_json.get('md5_hash') return_url = req_json.get('return_url', '/') if user_id == 'user1' and md5_hash == md5hex(user_id + md5hex('password1')): g.sess['user_id'] = 'steve' g.res.err = Error.NONE g.res.url = return_url return make_resp() else: g.res.err = Error.USER_LOGIN_FAILED return make_resp() g.cursor['test1'].execute('SELECT VERSION() AS version') row = g.cursor['test1'].fetchone() g.res.err = Error.NONE g.res.dat = { 'test_get': 'OK', 'a': request.args.get('a'), 'b': request.args.get('b'), 'version': row['version'] } return make_resp()
def logout(): if not is_access_allowed_ip_addr(): return make_resp() g.sess['user_id'] = '' g.res.err = Error.NONE g.res.url = './users_login.html' return make_resp()
def test_aes(): if not is_access_allowed_ip_addr() or not is_user_logged_in(): return make_resp() a = AESCrypto(g.cfg['aes_crypto']['key']) e = a.encrypt('한글') d = a.decrypt(e) g.res.err = Error.NONE g.res.dat = {'test_aes': 'OK', 'e': e, 'd': d} return make_resp()
def test_json(): if not is_access_allowed_ip_addr() or not is_user_logged_in(): return make_resp() req_json = request.get_json() connect_database('test1') g.cursor['test1'].execute('SELECT VERSION() AS version') rows = g.cursor['test1'].fetchall() g.res.err = Error.NONE g.res.dat = { 'test_json': 'OK', 'a': req_json.get('a'), 'b': req_json.get('b'), 'rows': rows } return make_resp()
def test_post(): if not is_access_allowed_ip_addr() or not is_user_logged_in(): return make_resp() query = 'SELECT * FROM test1 ORDER BY test1_id DESC LIMIT %s OFFSET %s ' params = (1, 0) connect_database('test1') g.cursor['test1'].execute(query, params) row = g.cursor['test1'].fetchone() row['col09'] = row['col09'].decode('utf-8') g.res.err = Error.NONE g.res.dat = { 'test_post': 'OK', 'a': request.form.get('a'), 'b': request.form.get('b'), 'row': row } return make_resp()
def test_upload(): if not is_access_allowed_ip_addr() or not is_user_logged_in(): return make_resp() if 'file' not in request.files: g.res.err = Error.UNKNOWN g.res.dat = {'test_upload': 'not uploaded'} return make_resp() file = request.files['file'] if file and '.' in file.filename and file.filename.rsplit( '.', 1)[1].lower() in ['png', 'jpg', 'gif']: file_name = secure_filename(file.filename) file_path = os.path.join(g.cfg['upload']['directory'], file_name) file.save(file_path) g.res.err = Error.NONE g.res.dat = { 'test_upload': 'file saved', 'name': request.form.get('name') } return make_resp() s3 = boto3.client('s3') s3.upload_file( file_path, g.cfg['aws_s3']['bucket'], os.path.join('{0:%Y-%m-%d}'.format(datetime.now()), file_name)) g.res.err = Error.NONE g.res.dat = { 'test_upload': 'uploaded to s3', 'name': request.form.get('name') } return make_resp() g.res.err = Error.NONE g.res.dat = {'test_upload': 'not an image file'} return make_resp()