def change_password(): '''Route to reset a password''' data = request.get_json() username = data.get('username') old_password = data.get('old_password') new_password = data.get('new_password') data = { "username": username, "old_password": old_password, "password": new_password } if validate.inputs(data): return jsonify(validate.inputs(data)), 406 if validate.pattern(data): return jsonify(validate.pattern(data)), 406 data = { "username": username, "password": old_password, "new_password": new_password } if validate.valid_user(data)['message'] == "Welcome, Log In Succesful!": Users.reset_password(username, new_password) return jsonify({'message': 'Password changed'}), 201 else: return jsonify(validate.valid_user(data)), 401
def login(): '''Route to login a User''' login_data = request.get_json() username = login_data.get('username') password = login_data.get('password') data = {"username": username, "password": password} if validate.inputs(data): return jsonify(validate.inputs(data)), 406 if validate.valid_user(data)['message'] == "Invalid Password!": return jsonify(validate.valid_user(data)['message']), 401 if validate.valid_user(data)['message'] == "Non-Existent User!": return jsonify(validate.valid_user(data)['message']), 404 else: return jsonify(validate.valid_user(data)), 200
def register_business(): '''Route to register a business''' current_user = get_jwt_identity() biz_data = request.get_json() business_name = biz_data.get('business_name') category = biz_data.get('category') location = biz_data.get('location') description = biz_data.get('description') data = { "business_name": business_name, "category": category, "location": location, "description": description } if validate.inputs(data): return jsonify(validate.inputs(data)), 406 owner = Users.query.filter_by(id=current_user).first() owner_id = owner.id existing_business = Businesses.query.filter_by( business_name=business_name).first() if existing_business: response = {'message': "Business Name Taken!"} return make_response(jsonify(response['message'])), 409 else: new_biz = Businesses(business_name, category, location, description, owner_id) new_biz.register_business() response = { 'message': new_biz.business_name + '. Business successfully registered by ' + new_biz.owner.username } return make_response(jsonify(response['message'])), 201
def register_user(): '''Route to register a user''' user_data = request.get_json() email = user_data.get('email') username = user_data.get('username') password = user_data.get('password') data = {"username": username, "email": email, "password": password} if validate.inputs(data): return jsonify(validate.inputs(data)), 406 if validate.pattern(data): return jsonify(validate.pattern(data)), 406 if validate.existing(data): return jsonify(validate.existing(data)), 409 else: new_user = Users(email, username, password) new_user.create_user() response = { 'message': "Account successfully registered. Log In to access." } return make_response(jsonify(response['message'])), 201
def reviews(business_id): current_user = get_jwt_identity() business = Businesses.get_one(business_id) if business: if current_user != business.owner_id: review_data = request.get_json() title = review_data.get('title') description = review_data.get('description') data = {"title": title, "description": description} if validate.inputs(data): return jsonify(validate.inputs(data)), 406 user_id = business.owner_id business_id = business.id new_review = Reviews(title, description, user_id, business_id) new_review.add_review() response = { 'message': 'Review Posted', } return make_response(jsonify(response)), 201 else: return jsonify({'message': 'Cannot Review your own Business'}), 401 else: return jsonify( {'message': 'Cannot Review. Resource(Business) Not Found'}), 404