def delete_user(id): user_service = UserService() result = user_service.delete(id) if not (result is None): return response_with_status({'message': result}, 404) else: return response_with_status({'message': 'User deleted'}, 204)
def get_brand(id): service = BrandService() e = service.get(id) if e is None: return response_with_status({"message": "Brand not found"}, 404) else: return response_with_status(e.to_dict())
def get_user(id): user_service = UserService() user = user_service.get(id) if user is None: return response_with_status({'message': 'user not found'}, 404) else: return response_with_status(user.serialize)
def delete_user(id): user_service = UserService() result = user_service.delete(id) if not(result is None): return response_with_status({'message': result}, 404) else: return response_with_status({'message': 'User deleted'}, 204)
def delete_brand(id): service = BrandService() result = service.delete(id) if not (result is None): return response_with_status({"message": result}, 404) else: return response_with_status({"message": "Brand deleted"}, 204)
def delete_brand(id): service = BrandService() result = service.delete(id) if not(result is None): return response_with_status({'message': result}, 404) else: return response_with_status({'message': 'Brand deleted'}, 204)
def get_brand(id): service = BrandService() e = service.get(id) if e is None: return response_with_status({'message': 'Brand not found'}, 404) else: return response_with_status(e.to_dict())
def login(): json_data = request.json service = UserService() try: auth = service.login(json_data.get('user'), json_data.get('password')) except Exception as e: logger.exception(e) if auth is None: return response_with_status({'message': 'invalid username or password'}, 403) else: session['user'] = json.dumps(auth.to_dict()) return response_with_status({'message': 'you are logged in'}, 200)
def get_account(): try: user_service = UserService() logger.info('session[user] = %s ' % str(session['user'])) sess_user = json.loads('{}' if session['user'] is None else str(session['user'])) user = user_service.get(int(sess_user.get('id'))) if user is None: return response_with_status({'message': 'user not found'}, 404) else: return response_with_status(user.to_dict()) except Exception as e: logger.exception(e) raise e
def login(): json_data = request.json service = UserService() try: auth = service.login(json_data.get('user'), json_data.get('password')) except Exception as e: logger.exception(e) if auth is None: return response_with_status( {'message': 'invalid username or password'}, 403) else: session['user'] = json.dumps(auth.to_dict()) return response_with_status({'message': 'you are logged in'}, 200)
def wrapped(*args, **kwargs): try: content = request.get_json() if content is None: return response_with_status({'message':'content is not json'}, 400) for field in fields: field_type = '' field_name = field allow_empty = '!' not in field if not allow_empty: field_data = field.split('!') field_name = field_data[1] has_type = '%' in field if has_type: field_data = field_name.split('%') field_name = field_data[0] field_type = field_data[1] if field_name not in content: return response_with_status({'message': ('missing field %s' % field_name)}, 400) if not allow_empty and content.get(field_name) == '': return response_with_status({'message': ('%s cannot be empty' % field_name)}, 406) if field_type != '': if field_type not in ['e', 'i', 'b']: return response_with_status({'message': ('invalid type %s' % field_type)}, 500) if field_type == 'e' and not is_email(content.get(field_name)): return response_with_status({'message': ('%s is not an email' % field_name)}, 406) elif field_type == 'i' and not is_number(content.get(field_name)): return response_with_status({'message': ('%s is not an integer' % field_name)}, 406) elif field_type == 'b' and not is_boolean(content.get(field_name)): return response_with_status({'message': ('%s is not an integer' % field_name)}, 406) except Exception as e: return response_with_status({'message': str(e)}, 400) return f(*args, **kwargs)
def get_account(): try: user_service = UserService() logger.info('session[user] = %s ' % str(session['user'])) sess_user = json.loads( '{}' if session['user'] is None else str(session['user'])) user = user_service.get(int(sess_user.get('id'))) if user is None: return response_with_status({'message': 'user not found'}, 404) else: return response_with_status(user.to_dict()) except Exception as e: logger.exception(e) raise e
def wrapped(*args, **kwargs): try: content = request.get_json() if content is None: return response_with_status( {'message': 'content is not json'}, 400) for field in fields: field_type = '' field_name = field allow_empty = '!' not in field if not allow_empty: field_data = field.split('!') field_name = field_data[1] has_type = '%' in field if has_type: field_data = field_name.split('%') field_name = field_data[0] field_type = field_data[1] if field_name not in content: return response_with_status( {'message': ('missing field %s' % field_name)}, 400) if not allow_empty and content.get(field_name) == '': return response_with_status( {'message': ('%s cannot be empty' % field_name)}, 406) if field_type != '': if field_type not in ['e', 'i', 'b']: return response_with_status( {'message': ('invalid type %s' % field_type)}, 500) if field_type == 'e' and not is_email( content.get(field_name)): return response_with_status( { 'message': ('%s is not an email' % field_name) }, 406) elif field_type == 'i' and not is_number( content.get(field_name)): return response_with_status( { 'message': ('%s is not an integer' % field_name) }, 406) elif field_type == 'b' and not is_boolean( content.get(field_name)): return response_with_status( { 'message': ('%s is not an integer' % field_name) }, 406) except Exception as e: return response_with_status({'message': str(e)}, 400) return f(*args, **kwargs)
def wrapped(*args, **kwargs): try: user = json.loads(str(session['user'] if 'user' in session else '{}')) user_roles = user.get('roles') logger.info('user_roles = %r' % user_roles) if (user_roles is None or len(user_roles) == 0 or str(user_roles[0]) not in roles): return response_with_status({ 'message':'you are not authorized' }, 403) return f(*args, **kwargs) except Exception as e: logger.exception(e) raise e
def wrapped(*args, **kwargs): try: user = json.loads( str(session['user'] if 'user' in session else '{}')) user_roles = user.get('roles') logger.info('user_roles = %r' % user_roles) if (user_roles is None or len(user_roles) == 0 or str(user_roles[0]) not in roles): return response_with_status( {'message': 'you are not authorized'}, 403) return f(*args, **kwargs) except Exception as e: logger.exception(e) raise e
def change_password(id): data = request.get_json() user_service = UserService() user_service.change_password(id, data) return response_with_status( {'message': 'account.changepassword.messages.success'}, 201)
def get_brands(): service = BrandService() list = service.get_collection() return response_with_status(list)
def logout(): session.pop('user', None) return response_with_status({'message': 'invalid username or password'}, 403)
def create_brand(): data = request.get_json() service = BrandService() service.create(data) return response_with_status({'message': 'success'}, 201)
def update_brand(id): data = request.get_json() service = BrandService() service.update(id, data) return response_with_status({'message': 'success'}, 200)
def get_users(): user_service = UserService() _list = user_service.get_collection() return response_with_status(_list)
def update_user(id): data = request.get_json() user_service = UserService() op = user_service.update(id, data) return response_with_status(op.to_dict(), 200)
def register(): data = request.get_json() user_service = UserService() op = user_service.create(data) return response_with_status({'message': 'registered successfully'}, 201)
def create_brand(): data = request.get_json() service = BrandService() service.create(data) return response_with_status({"message": "success"}, 201)
def change_password(id): data = request.get_json() user_service = UserService() user_service.change_password(id, data) return response_with_status({'message': 'account.changepassword.messages.success'}, 201)
def update_brand(id): data = request.get_json() service = BrandService() service.update(id, data) return response_with_status({"message": "success"}, 200)