def post(cls): if not request.content_type == 'application/json': abort(400) sent_data = request.get_json() email = sent_data.get('email') password = sent_data.get('password') is_input_valid = v.validate_user_login(email=email, password=password) if not isinstance(is_input_valid, bool): return response(is_input_valid, 'unsuccessful', 401) existing_user = controller.check_if_user_exists(email) if existing_user is None: return response( 'user with email ' + email + ' doesnot exist', 'unsuccessful', 401) if not User.check_password(existing_user['password'], password): return response( 'provided password is incorrect', 'unsuccessful', 401) token = User.generate_auth_token(existing_user['user_id']) if not isinstance(token, bytes): return response(str(token), 'unsuccessful', 401) return auth_success_response( existing_user['username'] + ' is now logged in', token)
def post(cls): auth_header = request.headers.get('Authorization') if not auth_header: return response( 'Provide an authorization header', 'unsuccessful', 403) try: auth_token = auth_header.split(" ")[1] except IndexError: return response('unable to extract token', 'unsuccessful', 403) else: decoded_token_response = User.decode_auth_token(auth_token) if not isinstance(decoded_token_response, int): return response(decoded_token_response, 'unsuccessful', 401) is_token_invalid = TokenController.check_if_token_exists( auth_token) if isinstance(is_token_invalid, list): return response( 'token already invalidated', 'unsuccessful', 400) token = InvalidToken(auth_token) TokenController.save_invalid_token(token) return response('successfully logged out', 'success', 200)
def post(self): # check for a valid content type if not request.content_type == 'application/json': return response('request must be of type json', 'unsuccessful', 400) # extract request data request_data = request.get_json() name = request_data.get('name') category = request_data.get('category') quantity = request_data.get('quantity') price = request_data.get('price') # validate product object input valid_input = v.validate_product(name, category, quantity, price) if not isinstance(valid_input, bool): return response(valid_input, 'unsuccessful', 400) # create a new product new_product = Product(name=name, category=category, quantity=quantity, price=price) # check if a product with the same name already exists is_existing_product = controller.add_product(new_product) if not isinstance(is_existing_product, bool): return response(is_existing_product, 'unsuccessful', 400) # return response with a new product return create_resource_response( new_product, url_for('products.product', product_id=new_product.product_id))
def post(cls): if not request.content_type == 'application/json': abort(400) sent_data = request.get_json() username = sent_data.get('username') role = sent_data.get('role') email = sent_data.get('email') password = sent_data.get('password') is_input_valid = v.validate_user(username, email, password) if not isinstance(is_input_valid, bool): return response(is_input_valid, 'unsuccessful', 400) # Create a new user new_user = User(username=username, role=role, email=email, password=password) # Check the new_user already exists existing_user = controller.check_if_user_exists(email) if existing_user is not None: return response('user with email ' + existing_user['email'] + ' already exists', 'unsuccessful', 400) controller.create_user(new_user) return create_user_response( new_user, url_for('auth.register', email=new_user.email))
def post(cls): # check for a valid content type if not request.content_type == 'application/json': return response('request must be of type json', 'unsuccessful', 400) # extract request data request_data = request.get_json() product_name = request_data.get('name') product_quantity = request_data.get('quantity') # validate product object input valid_input = v.validate_shopping_cart_product( product_name, product_quantity) if not isinstance(valid_input, bool): return response(valid_input, 'unsuccessful', 400) # fetch and add product to shopping cart inserted = cart.insert(product_name, product_quantity) if inserted is None: return response( 'product ' + product_name + ' added to shopping cart', 'successful', 201) return response(inserted, 'unsuccessful', 400)
def decorated_function(*args, **kwargs): token = None # if the request contains an authorization header if 'Authorization' in request.headers: auth_header = request.headers['Authorization'] try: # try to extract the token token = auth_header.split(" ")[1] except IndexError: # if the token can't be extracted due to # an index out of range error return make_response(jsonify({ 'status': 'Failed to extract token', 'message': 'Please provide a valid token' })), 403 if not token: # if there is no token in the request header return make_response(jsonify({ 'message': 'Missing a token' })), 401 try: # call the token decode method of the user model # to get the initial user_id in order extract the # right user from the user table in the database blacklisted_token = TokenController.check_if_token_exists(token) if isinstance(blacklisted_token, list): return response( 'token blacklisted, please login again', 'unsuccessful', 401) decoded_response = User.decode_auth_token(token) user = controller.fetch_user_by_id(decoded_response) current_user = User( username=user['username'], role=user['role'], email=user['email'], password=user['password_hash'] ) except: # if the token has been already used to logout which # makes it invalid, the function will raise an error message = 'Invalid token' if isinstance(decoded_response, str): message = decoded_response return make_response(jsonify({ 'status': 'Failed to extract user_id', 'message': message })), 401 # extract the user from the # db and assign that user as an argument in the returned function return f(current_user, *args, **kwargs)
def post(cls): # check for a valid content type if not request.content_type == 'application/json': return response('request must be of type json', 'unsuccessful', 400) # extract request data request_data = request.get_json() attendant = request_data.get('attendant') # validate sale object input valid_sale_input = v.validate_sale(attendant) if not isinstance(valid_sale_input, bool): return response(valid_sale_input, 'unsuccessful', 400) # create a new sale new_sale = Sale(attendant=attendant) # update the new_sale's products list with new products products = cart.load_shopping_cart() new_sale.products += products # update product attributes cart.update_sale_attributes(new_sale) # save new sale object controller.add_sale_record(new_sale) # return response with a new sale return single_sale_response(new_sale, 201)
def get(self, product_id): # GET request to fetch a product by id product = controller.fetch_product_by_id(product_id) if not isinstance(product, Product): return response(product, 'unsuccessful', 400) return single_product_response(product)
def get(self): products = controller.fetch_all_products() if isinstance(products, str): return response(products, 'unsuccessful', 200) return fetch_all_response(convert_list_to_json(products), 'products')
def get(cls, sale_id): # GET request to fetch a sale by id sale = controller.fetch_sale_record(sale_id) if not isinstance(sale, Sale): return response(sale, 'unsuccessful', 400) return single_sale_response(sale, 200)
def get(cls): sales = controller.fetch_all_sale_records() if not isinstance(sales, list): return response(sales, 'unsuccessful', 400) return all_sales_response(convert_list_to_json(sales), 'successful', 200)