def register(): json = request.json if json: username = json.get('username', '') password = json.get('password', '') email = json.get('email', '') else: return bad_request("Lack of required payload data") payload = {} is_email_valid, email_check_dict = validate_email(email) payload['email'] = {'valid': is_email_valid, 'checks': email_check_dict} is_username_valid, username_check_dict = validate_username(username) payload['username'] = { 'valid': is_username_valid, 'checks': username_check_dict } is_password_valid, password_check_dict = validate_password(password) payload['password'] = { 'valid': is_password_valid, 'checks': password_check_dict } if is_email_valid & is_password_valid & is_username_valid: create_user(username, email, password) status_code = 201 else: status_code = 422 return jsonify(payload), status_code
def fresh_login(): json = request.json if json: username = json.get('username', '') password = json.get('password', '') else: return bad_request("Lack of required payload data") payload = get_fresh_jwt_token(username, password, with_refresh_token=False) if payload: return jsonify(payload), 200 else: return error_response(401, "Bad username or password")
def reset_password_request(): json = request.json if json: email = json.get('email', '') else: return bad_request("Lack of required payload data") user = get_user_by_email(email) if user: send_password_reset_email(user) return jsonify({'message': 'Done!'}), 202 else: return error_response(422, "Email address not registered")
def validate(): json = request.json if json: email = json.get('email', None) username = json.get('username', None) else: return bad_request("Lack of required payload data") payload = {} if email is not None: is_valid, check_dict = validate_email(email) payload['email'] = {'valid': is_valid, 'checks': check_dict} if username is not None: is_valid, check_dict = validate_username(username) payload['username'] = {'valid': is_valid, 'checks': check_dict} return jsonify(payload), 200
def update_recipe(pk): json_data = request.get_json() if not json_data: return bad_request('No input data provided') recipe_model = get_recipe(pk) if current_user == recipe_model.author or current_user.admin: try: data = recipe_schema.load(json_data) except ValidationError as err: return jsonify(err.messages), 422 recipe_model = save_recipe_from_schema(data, recipe_model) result = recipe_schema.dump(recipe_model) return jsonify({ "message": "Changes will be seen for other users after administrator acceptance.", "recipe": result }), 200 else: return error_response(401)
def create_recipe(): if not current_user: return error_response(401, "Unauthorized") json_data = request.get_json() if not json_data: return bad_request('No input data provided') try: data = recipe_schema.load(json_data) except ValidationError as err: return jsonify(err.messages), 422 model = init_recipe(author=current_user) model = save_recipe_from_schema(data, model) result = recipe_schema.dump(model) return jsonify({ "message": "Recipe will be seen for other users after administrator acceptance.", "recipe": result }), 201
def reset_password(token): json = request.json if json: password = json.get('password', '') else: return bad_request("Lack of required payload data") user = verify_reset_password_token(token) if not user: return error_response(401, 'Invalid token') is_password_valid, password_check_dict = validate_password(password) if is_password_valid: set_new_password(user, password) return jsonify({'message': 'Done!'}), 200 else: payload = { 'password': { 'valid': is_password_valid, 'checks': password_check_dict } } return jsonify(payload), 422