def wrapped(*args, **kwargs): # put all parameters into kwargs kwargs = kwargs if kwargs else {'testkey': 'testvalue'} if request.args: # get param kwargs.update(request.args.to_dict()) if request.form: # post param kwargs.update(request.form.to_dict()) # todo: request.files & request.data if request.headers.get('Authorization'): encrypted_token = request.headers.get('Authorization') is_valid, user_id = logic_user.is_token_valid(encrypted_token) if not is_valid: raise errors.AuthTokenInvalid() user = logic_user.get_user_by_id(user_id) if not user: # token is valid, but maybe user is deleted raise errors.UserNotFound() try: result = func(**kwargs) response = { 'rc': 0, 'data': result } except Exception as e: rc = getattr(e, 'rc', errors.UnknownError.rc) err_msg = getattr(e, 'msg', errors.UnknownError.msg) response = { 'rc': rc, 'msg': err_msg, } return jsonify(response)
def create_diary(user_id, uuid, title, content): """Create new diary with title & content for user_id. Return: new_diary_id """ user = logic_user.get_user_by_id(user_id) if user is None: raise errors.UserNotFound() return db_diary.create_diary(user_id, uuid, title, content)
def test_signup_login(): base_test.reinit_table() email = "*****@*****.**" password = "******" user = logic_user.signup(email, password) assert user is not None user_id = user["id"] user_from_db = logic_user.get_user_by_id(user_id) assert user_from_db is not None assert user_from_db["email"] == user["email"] login_result, login_user = logic_user.login(email, password) assert login_result assert login_user is not None assert login_user["email"] == user["email"]
def test_signup_login(): base_test.reinit_table() email = '*****@*****.**' password = '******' user = logic_user.signup(email, password) assert user is not None user_id = user['id'] user_from_db = logic_user.get_user_by_id(user_id) assert user_from_db is not None assert user_from_db['email'] == user['email'] login_result, login_user = logic_user.login(email, password) assert login_result assert login_user is not None assert login_user['email'] == user['email']
def wrapped(*args, **kwargs): # put all parameters into kwargs kwargs = kwargs if kwargs else {'testkey': 'testvalue'} if request.args: # get param kwargs.update(request.args.to_dict()) if request.form: # post param kwargs.update(request.form.to_dict()) #todo: request.files & request.data if request.headers.get('Authorization'): encrypted_token = request.headers.get('Authorization') isValid, user_id = logic_user.is_token_valid(encrypted_token) if not isValid: raise errors.AuthTokenInvalid() user = logic_user.get_user_by_id(user_id) if not user: # token is valid, but maybe user is deleted raise errors.UserNotFound() return func(**kwargs)
def wrapped(*args, **kwargs): # put all parameters into kwargs kwargs = kwargs if kwargs else {'testkey': 'testvalue'} if request.args: # get param kwargs.update(request.args.to_dict()) if request.form: # post param kwargs.update(request.form.to_dict()) if request.data: # request body kwargs.update(json.loads(request.data)) # todo: request.files if request.headers.get('Authorization'): encrypted_token = request.headers.get('Authorization') is_valid, user_id = logic_user.is_token_valid(encrypted_token) if not is_valid: # AuthTokenInvalid abort(401) user = logic_user.get_user_by_id(user_id) if not user: # token is valid, but maybe user is deleted.UserNotFound abort(401) kwargs["user_id"] = user_id try: log_mobile_request(func.__name__, args, kwargs) result = func(*args, **kwargs) response = { 'rc': 0, 'data': result } except Exception as e: logger.exception(e.message) # logger will send email with this exception rc = getattr(e, 'rc', errors.UnknownError.rc) err_msg = getattr(e, 'msg', errors.UnknownError.msg) response = { 'rc': rc, 'msg': err_msg, } log_mobile_response(response) return jsonify(response)
def wrapped(*args, **kwargs): # put all parameters into kwargs kwargs = kwargs if kwargs else {'testkey': 'testvalue'} if request.args: # get param kwargs.update(request.args.to_dict()) if request.form: # post param kwargs.update(request.form.to_dict()) if request.data: # request body kwargs.update(json.loads(request.data)) # todo: request.files if request.headers.get('Authorization'): encrypted_token = request.headers.get('Authorization') is_valid, user_id = logic_user.is_token_valid(encrypted_token) if not is_valid: # AuthTokenInvalid abort(401) user = logic_user.get_user_by_id(user_id) if not user: # token is valid, but maybe user is deleted.UserNotFound abort(401) kwargs["user_id"] = user_id try: log_mobile_request(func.__name__, args, kwargs) result = func(*args, **kwargs) response = {'rc': 0, 'data': result} except Exception as e: logger.exception( e.message) # logger will send email with this exception rc = getattr(e, 'rc', errors.UnknownError.rc) err_msg = getattr(e, 'msg', errors.UnknownError.msg) response = { 'rc': rc, 'msg': err_msg, } log_mobile_response(response) return jsonify(response)
def sync_data(user_id, sync_token, sync_items, need_pull): """Upsert client data, sync_items, into server db; fetch changed data from server and return to client. 1. Decrypt sync_token and fetch last_sync_time; 2. Push: Traverse sync_items, execute upsert/delete action for specified table. Record sync_count for client usage; 3. If need_pull: Get changed data since last_sync_time, comparing with time_modified(upsert) & time_removed(delete); 4. Pull: return changed data to user. sync_items = [ { 'Diary': { 'create': { 'uuid': "2F69DEB5-B631-40DD-A65E-AFE9A0882275", 'time': 1477139399, 'title': 'this is a new diary', 'content': 'today is a good day', }, }, }, { 'Diary': { 'update': { 'uuid': "b8f4428a-98e1-11e6-8155-a45e60dcd7ed", 'time': 1477139400, 'title': 'I update this title', 'content': 'I\'m updated content', } } }, { 'Diary': { 'delete': { 'uuid': "b9000ff0-98e1-11e6-91f7-a45e60dcd7ed", 'time': 1477139401, } } } ] Return: { 'synced_count': 2, 'sync_token': 'jym0JTE-svI8iDOPp-6e_UMe6dYOVVNSVes8pzZCXDd_I4xn3CYT-oyGVjaCgKgtHO' (based on new last_sync_time), 'Diary': { 'delete': [ { 'uuid': "04B977C7-6F7F-4D36-BFDA-FE98C5241DB0", 'title': 'I'm created by other client', 'content': 'I'm created by other client', 'time': 1477139340, } { 'uuid': "04B977C7-6F7F-4D36-BFDA-FE98C5241ABC", 'title': 'I'm created by other client', 'content': 'I'm created by other client', 'time': 1477139340, } ], 'upsert': [ { 'uuid': "04B977C7-6F7F-4D36-BFDC-FE98C5241DB0", 'time': 1477139399, 'title': 'I'm created by other client', 'content': 'I'm created by other client', }, { 'uuid': "04B977C7-6F7F-4D36-BFDC-FE98C5241DB0", 'time': 1477139399, 'title': 'I'm created by other client', 'content': 'I'm created by other client', } ] } } """ if user_id is None or logic_user.get_user_by_id(user_id) is None: return {} last_sync_info = safetyutils.decrypt_sync_token(sync_token) last_sync_time = last_sync_info.get("last_sync_time", 0) synced_count = _push(user_id, sync_items) result = {'synced_count': synced_count} if need_pull: pull_data = _pull(user_id, last_sync_time) result.update(pull_data) return result
def sync_data(user_id, sync_token, sync_items, need_pull): """Upsert client data, sync_items, into server db; fetch changed data from server and return to client. 1. Decrypt sync_token and fetch last_sync_time; 2. Push: Traverse sync_items, execute upsert/delete action for specified table. Record sync_count for client usage; 3. If need_pull: Get changed data since last_sync_time, comparing with time_modified(upsert) & time_removed(delete); 4. Pull: return changed data to user. sync_items = [ { 'Diary': { 'create': { 'uuid': "2F69DEB5-B631-40DD-A65E-AFE9A0882275", 'time': 1477139399, 'title': 'this is a new diary', 'content': 'today is a good day', }, }, }, { 'Diary': { 'update': { 'uuid': "b8f4428a-98e1-11e6-8155-a45e60dcd7ed", 'time': 1477139400, 'title': 'I update this title', 'content': 'I\'m updated content', } } }, { 'Diary': { 'delete': { 'uuid': "b9000ff0-98e1-11e6-91f7-a45e60dcd7ed", 'time': 1477139401, } } } ] Return: { 'synced_count': 2, 'sync_token': 'jym0JTE-svI8iDOPp-6e_UMe6dYOVVNSVes8pzZCXDd_I4xn3CYT-oyGVjaCgKgtHO' (based on new last_sync_time), 'Diary': { 'delete': [ { 'uuid': "04B977C7-6F7F-4D36-BFDA-FE98C5241DB0", 'title': 'I'm created by other client', 'content': 'I'm created by other client', 'time': 1477139340, } { 'uuid': "04B977C7-6F7F-4D36-BFDA-FE98C5241ABC", 'title': 'I'm created by other client', 'content': 'I'm created by other client', 'time': 1477139340, } ], 'upsert': [ { 'uuid': "04B977C7-6F7F-4D36-BFDC-FE98C5241DB0", 'time': 1477139399, 'title': 'I'm created by other client', 'content': 'I'm created by other client', }, { 'uuid': "04B977C7-6F7F-4D36-BFDC-FE98C5241DB0", 'time': 1477139399, 'title': 'I'm created by other client', 'content': 'I'm created by other client', } ] } } """ if user_id is None or logic_user.get_user_by_id(user_id) is None: return {} last_sync_info = safetyutils.decrypt_sync_token(sync_token) last_sync_time = last_sync_info.get("last_sync_time", 0) synced_count = _push(user_id, sync_items) result = { 'synced_count': synced_count } if need_pull: pull_data = _pull(user_id, last_sync_time) result.update(pull_data) return result