def parse_data(fromUserId): # userId from_user = User.get_or_none(User.userId == fromUserId) # to_userId json_data = flask.request.json toUserId = json_data.get('userId') to_user = User.get_or_none(User.userId == toUserId) msg = "Success" code = 200 if not to_user: msg = 'UserId \'%s\' does not exists' % toUserId code = 400 return (from_user, to_user), msg, code
def parse_data(userId): json_data = flask.request.json user = User.get_or_none(User.userId == userId) recipeId = json_data.get('recipeId') recipe = Recipe.get_or_none(Recipe.id == recipeId) msg = "Success" code = 200 if not recipe: msg = 'RecipeId \'%s\' does not exists' % recipeId code = 400 return (user, recipe), msg, code
def add_recipe(): json_data = flask.request.json # userId userId = json_data.get('userId') user = User.get_or_none(User.userId == userId) if not user: return flask.jsonify({'msg': 'Must provide valid userId'}), 400 # name name = json_data.get('name') if not name: return flask.jsonify({'msg': 'Must provide non-empty name'}), 400 # name serving = json_data.get('serving') if not serving: return flask.jsonify({'msg': 'Must provide non-empty serving'}), 400 # name preparation_time = json_data.get('preparation_time') if not preparation_time: return flask.jsonify( {'msg': 'Must provide non-empty preparation_time'}), 400 # name cooking_time = json_data.get('cooking_time') if not cooking_time: return flask.jsonify({'msg': 'Must provide non-empty cooking_time'}), 400 recipe = Recipe(user=user, name=name, serving=serving, preparation_time=preparation_time, cooking_time=cooking_time) if not recipe.save(): return flask.jsonify({'msg': 'Error in saving data'}), 400 return flask.jsonify({'msg': 'Success'}), 200
def login(): data = flask.request.json if not data['email']: return flask.jsonify({"msg": "Missing email"}), 400 if not data['password']: return flask.jsonify({"msg": "Missing password"}), 400 user = User.get_or_none(User.email == data['email']) successful_login = check_password_hash(user.pw_hash, data['password']) if user else False if successful_login: access_token = create_access_token(identity=user.email) user_dict = user.as_dict() user_dict['access_token'] = access_token return flask.jsonify(user_dict), 200 else: if user is None: msg = "Email does not exists in database" elif successful_login is False: msg = "Wrong password is provided" else: msg = '' return flask.jsonify({'msg': msg}), 400
def add_comment(): json_data = flask.request.json # userId userId = json_data.get('userId') user = User.get_or_none(User.userId == userId) if not user: return flask.jsonify({'msg': 'Must provide valid userId'}), 400 # recipeId recipeId = json_data.get('recipeId') recipe = Recipe.get_or_none(Recipe.id == recipeId) if not recipe: return flask.jsonify({'msg': 'Must provide valid recipeId'}), 400 # text text = json_data.get('text') if not text: return flask.jsonify({'msg': 'Must provide non-empty text'}), 400 comment = Comment(user=user, recipe=recipe, text=text) if not comment.save(): return flask.jsonify({'msg': 'Error in saving data'}), 400 return flask.jsonify({'msg': 'Success'}), 200
data = dict(name=name, email=email, password=password) return test_url(url, method="POST", data=data, auth=False) def test_login(email='', password=''): url = 'http://*****:*****@gmail.com' password = "******" existing_user = User.get_or_none(User.email == email) if existing_user: existing_user.delete_instance() # Must Fail (Sign Up) assert_msg(test_signup(name="", email=email, password=password), "Missing name") # Empty Name assert_msg(test_signup(name=name, email="", password=password), "Missing email") # Empty Email assert_msg(test_signup(name=name, email=email, password=''), "Missing password") # Empty Password assert_msg( test_signup(name=name, email='*****@*****.**', password=password), "This email has been registered! Try another email!") # Taken Email
def userExists(): userId = flask.request.args.get('userId') user = User.get_or_none(User.userId == userId) return flask.jsonify(user is not None), 200
def get_user(userId: str): user = User.get_or_none(User.userId == userId) return flask.jsonify({'msg': 'Success', 'data': user.as_dict()}), 200
def wrapper(userId, *args, **kwargs): user = User.get_or_none(User.userId == userId) if user: return func(userId, *args, **kwargs) else: return flask.jsonify({'msg': 'User does not exists'}), 400