def recipe_add(): """Page showing the possibility to add the recipe.""" # checking form validation form = RecipeAddForm() if form.validate_on_submit(): try: if form.image.data != DEFAULT_RECIPE_IMAGE_PATH: # if the user has uploaded a picture file image = images.save(form.image.data) image_path = f'app/media/recipe_images/{image}' else: image_path = DEFAULT_RECIPE_IMAGE_PATH # form.image.data by default is 'app/media/default.png' except UploadNotAllowed: # if the user uploaded a file that is not a picture flash('Incorrect picture format', 'error') else: # if there is no exception add new recipe recipe_manager = RecipeManager(api_token=g.user_token) recipe_data, recipe_files = recipe_manager.get_form_data( form, image_path) recipe_manager.add(recipe_data, recipe_files) # adds new recipe image_file = recipe_files['image'][1] delete_image_file( image_file, image_path ) # deleting image file from local storage when it's uploaded return redirect('/recipes/') return render_template('recipe_add.html', form=form)
def recipe_detail(pk): """Page showing selected recipe.""" recipe_manager = RecipeManager() response = recipe_manager.get_recipe_response(pk) recipe = response.json() # shows 404 if there is no recipe or response status code is 404 if not recipe or response.status_code == 404: abort(404) return render_template('recipe_detail.html', recipe=recipe)
def recipe_delete(username, pk): """Page confirming the removal of the recipe.""" recipe_manager = RecipeManager(api_token=g.user_token) response = recipe_manager.get_user_recipe_response(username, pk) recipes = response.json() # shows 404 if there is no recipe to delete, api status code is 404 or username is not the author if not recipes or response.status_code == 404 or username != g.username: abort(404) # POST method handler if request.method == 'POST': recipe_manager.delete(pk, username) flash('Successfully deleted recipe.') return redirect(f'/recipes/{username}/') return render_template('recipe_delete.html')
def recipe_list_all(): """Page listing all recipes.""" recipe_manager_handler = RecipeManager() recipes = recipe_manager_handler.get_all_recipes_response().json() # paginating list of recipes paginate = Paginate(recipes, 'bootstrap4') pagination_recipes = paginate.get_data(offset=paginate.offset, per_page=paginate.per_page) pagination = paginate.pagination() return render_template('recipe_list_all.html', recipes=pagination_recipes, page=paginate.page, per_page=paginate.per_page, pagination=pagination)
def recipe_list_user(username): """Page listing user recipes.""" recipe_manager = RecipeManager() recipes = recipe_manager.get_user_recipes_response(username).json() # if the user is not the author and there are no recipes displays the information if not recipes and g.username != username: flash('The user does not exist or has no recipes.') # paginating list of recipes paginate = Paginate(recipes, 'bootstrap4') pagination_recipes = paginate.get_data() pagination = paginate.pagination() return render_template('recipe_list_user.html', recipes=pagination_recipes, page=paginate.page, per_page=paginate.per_page, pagination=pagination)
def recipe_edit(username, pk): """Page showing the possibility to edit the recipe.""" recipe_manager = RecipeManager(api_token=g.user_token) response = recipe_manager.get_recipe_response(pk) recipe = response.json() # shows 404 if there is no recipe, response status code is 404 or user is not the author if not recipe or response.status_code == 404 or username != g.username: abort(404) # checking form validation form = RecipeAddForm(data=recipe) if form.validate_on_submit(): try: if form.image.data != DEFAULT_RECIPE_IMAGE_PATH: # if the user has uploaded a picture file image = images.save(form.image.data) image_path = f'app/media/recipe_images/{image}' else: image_path = None # set image_path to None so as not to alter the image except UploadNotAllowed: # if the user uploaded a file that is not a picture flash('Incorrect picture format', 'error') else: # if there is no exception edit recipe data and image recipe_data, recipe_files = recipe_manager.get_form_data( form, image_path) recipe_manager.edit(recipe_data, recipe_files, pk, username) return redirect('/recipes/') return render_template('recipe_edit.html', form=form)
def setUp(self): self.recipe_manager = RecipeManager(api_token=None) self.recipe_data = { 'id': 48, 'ingredients': [{ 'id': 50, 'food': { 'id': 10, 'name': 'sfsgsg', 'recipe': None }, 'unit': 'PIECE', 'amount': 6 }], 'steps': [{ 'id': 48, 'instruction': 'Pokroi', 'order': 0 }], 'name': 'HIHI', 'image': 'https://recipes-cookbook-api.herokuapp.com/media/recipes_images/default.png', 'description': 'asfasg', 'portions': 1, 'preparation_time': 15, 'difficulty': 'MEDIUM', 'rating': '0', 'date_posted': '2020-09-21T20:06:21.561247Z' } self.form_data = { 'name': 'New recipe', 'description': 'Nice recipe', 'ingredients': [{ 'food': { 'name': 'Food name' }, 'unit': 'GRAM', 'amount': 5 }], 'steps': [{ 'instruction': 'Dice food' }], 'portions': 1, 'preparation_time': 5, 'difficulty': 'EASY' } self.payload = { 'ingredients': [{ 'food': { 'name': 'Food name' }, 'unit': 'GRAM', 'amount': 5 }], 'steps': [{ 'instruction': 'Dice food' }], 'name': 'New recipe', 'description': 'Nice recipe', 'portions': 2, 'preparation_time': 5, 'difficulty': 'EASY' }
class TestRecipeManager(unittest.TestCase): def setUp(self): self.recipe_manager = RecipeManager(api_token=None) self.recipe_data = { 'id': 48, 'ingredients': [{ 'id': 50, 'food': { 'id': 10, 'name': 'sfsgsg', 'recipe': None }, 'unit': 'PIECE', 'amount': 6 }], 'steps': [{ 'id': 48, 'instruction': 'Pokroi', 'order': 0 }], 'name': 'HIHI', 'image': 'https://recipes-cookbook-api.herokuapp.com/media/recipes_images/default.png', 'description': 'asfasg', 'portions': 1, 'preparation_time': 15, 'difficulty': 'MEDIUM', 'rating': '0', 'date_posted': '2020-09-21T20:06:21.561247Z' } self.form_data = { 'name': 'New recipe', 'description': 'Nice recipe', 'ingredients': [{ 'food': { 'name': 'Food name' }, 'unit': 'GRAM', 'amount': 5 }], 'steps': [{ 'instruction': 'Dice food' }], 'portions': 1, 'preparation_time': 5, 'difficulty': 'EASY' } self.payload = { 'ingredients': [{ 'food': { 'name': 'Food name' }, 'unit': 'GRAM', 'amount': 5 }], 'steps': [{ 'instruction': 'Dice food' }], 'name': 'New recipe', 'description': 'Nice recipe', 'portions': 2, 'preparation_time': 5, 'difficulty': 'EASY' } @patch('requests.post') def test_posted_recipe_id(self, mock_post): mock_post.return_value = Mock(status_code=200) mock_post.return_value.json.return_value = self.recipe_data response = requests.post(url='test_url') post_id = self.recipe_manager.posted_recipe_id(response) self.assertEqual(post_id, self.recipe_data['id']) @patch('recipes.requests.get') def test_get_all_recipes_response(self, mock_get): recipes = [self.recipe_data] mock_get.return_value = Mock(status_code=200) mock_get.return_value.json.return_value = recipes response = self.recipe_manager.get_all_recipes_response() self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), recipes) @patch('recipes.requests.get') def test_get_recipe_response(self, mock_get): recipe = self.recipe_data mock_get.return_value = Mock(status_code=200) mock_get.return_value.json.return_value = recipe response = self.recipe_manager.get_recipe_response(48) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), recipe) @patch('recipes.requests.get') def test_get_user_recipe_response(self, mock_get): recipe = self.recipe_data mock_get.return_value = Mock(status_code=200) mock_get.return_value.json.return_value = recipe response = self.recipe_manager.get_user_recipe_response( 'test_user', 48) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), recipe) @patch('recipes.requests.get') def test_get_user_recipes_response(self, mock_get): recipes = [self.recipe_data] mock_get.return_value = Mock(status_code=200) mock_get.return_value.json.return_value = recipes response = self.recipe_manager.get_user_recipes_response('test_user') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), recipes) @patch('recipes.requests.post') @patch('recipes.requests.patch') def test_add(self, mock_post, mock_patch): form = RecipeAddForm(data=self.form_data) self.recipe_form = RecipeFormDataHandler() image_path = '../app/media/default.png' mock_post.return_value.json.return_value = self.recipe_data mock_patch.return_value = Mock(status_code=200) mock_post.return_value = Mock(status_code=200) data, files = self.recipe_form.get_recipe_form_data(form, image_path) files['image'][1].close() response_post, response_patch = self.recipe_manager.add(data=data, files=files) self.assertEqual(response_post.status_code, 200) self.assertEqual(response_patch.status_code, 200) @patch('recipes.requests.patch') def test_edit(self, mock_patch): form = RecipeAddForm(data=self.form_data) self.recipe_form = RecipeFormDataHandler() image_path = '../app/media/default.png' mock_patch.return_value = Mock(status_code=200) data, files = self.recipe_form.get_recipe_form_data(form, image_path) files['image'][1].close() response_patch_recipe, response_patch_image = self.recipe_manager.edit( data=self.payload, files=files, pk=1, username='******') self.assertEqual(response_patch_recipe.status_code, 200) self.assertEqual(response_patch_image.status_code, 200) @patch('recipes.requests.delete') def test_delete(self, mock_delete): mock_delete.return_value = Mock(status_code=200) response = self.recipe_manager.delete(pk=1, username='******') self.assertEqual(response.status_code, 200) def test_get_form_data(self): form = RecipeAddForm(data=self.form_data) image_path = '../app/media/default.png' payload, files = self.recipe_manager.get_form_data(form, image_path) files['image'][1].close() self.assertEqual(payload, self.form_data) self.assertEqual(files['image'][0], 'default.png')