def put(self): file = request.files.get('avatar') if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST user = User.get_by_id(id=get_jwt_identity()) if user.avatar_image: avatar_path = image_set.path(folder='avatars', filename=user.avatar_image) if os.path.exists(avatar_path): os.remove(avatar_path) filename = save_image(image=file, folder='avatars') user.avatar_image = filename user.save() clear_cache('/politicians') return user_avatar_schema.dump(user).data, HTTPStatus.OK
def put(self, recipe_id): file = request.files.get('cover') if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'Recipe not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != recipe.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN if recipe.cover_image: cover_path = image_set.path(folder='recipes', filename=recipe.cover_image) if os.path.exists(cover_path): os.remove(cover_path) filename = save_image(image=file, folder='recipes') recipe.cover_image = filename recipe.save() return recipe_cover_schema.dump(recipe).data, HTTPStatus.OK
def put(self, politician_id): file = request.files.get('cover') if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST politician = Politician.get_by_id(politician_id=politician_id) if politician is None: return {'message': 'Politician not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != politician.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN if politician.cover_image: cover_path = image_set.path(folder='politicians', filename=politician.cover_image) if os.path.exists(cover_path): os.remove(cover_path) filename = save_image(image=file, folder='politicians') politician.cover_image = filename politician.save() clear_cache('/politicians') return politician_cover_schema.dump(politician).data, HTTPStatus.OK
def put(self): file = request.files.get("profile_picture") if not file: return {"message": "Not a valid image"}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {"message": "File type not allowed"}, HTTPStatus.BAD_REQUEST user = User.get_by_id(id=get_jwt_identity()) if user.profile_picture: profile_picture_path = image_set.path( folder="profile-pictures", filename=user.profile_picture) if os.path.exists(profile_picture_path): os.remove(profile_picture_path) filename = save_image(image=file, folder="profile-pictures") user.profile_picture = filename user.save() return user_profile_picture_schema.dump(user).data, HTTPStatus.OK
def put(self, recipe_id): """This method has got the logic to put the cover image of the recipe.""" file = request.files.get('cover') # Check if cover image exists and whether the file extension is permitted if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST # Retrieved the Recipe object recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'Recipe not found'}, HTTPStatus.NOT_FOUND # Check right to modify the recipe current_user = get_jwt_identity() if current_user != recipe.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN if recipe.cover_image: cover_path = image_set.path(folder='recipes', filename=recipe.cover_image) if os.path.exists(cover_path): os.remove(cover_path) # Save the uploaded image filename = save_image(image=file, folder='recipes') recipe.cover_image = filename # Save the recipe recipe.save() # Clear cache clear_cache('/recipes') # Finally, return the URL image in a JSON format and with status code HTTP 200 OK return recipe_cover_schema.dump(recipe).data, HTTPStatus.OK
def put(self, item_id): file = request.files.get("picture") current_user = get_jwt_identity() item = Item.get_by_id(item_id=item_id) if item.user_id != current_user: return {"message": "Access not allowed"}, HTTPStatus.FORBIDDEN if not file: return {"message": "Not a valid image"}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {"message": "File type not allowed"}, HTTPStatus.BAD_REQUEST if item.picture: picture_path = image_set.path(folder="pictures", filename=item.picture) if os.path.exists(picture_path): os.remove(picture_path) filename = save_image(image=file, folder="pictures") item.picture = filename item.save() saveItemHistory(item.id) return item_picture_schema.dump(item).data, HTTPStatus.OK
def put(self, recipe_id): """ Gets the cover image in request.files and verifies whether it exists and if the file extension is permitted """ file = request.files.get('cover') if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST # Check whether user hs the right to modify the recipe recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'Recipe not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != recipe.user_id: return {'message': 'Access in not allowed'}, HTTPStatus.FORBIDDEN # If the user has the right to, we will go # ahead and modify the cover image of the recipe if recipe.cover_image: cover_path = image_set.path(folder='recipes', filename=recipe.cover_image) if os.path.exists(cover_path): os.remove(cover_path) # User the save_image function to save the uploaded image filename = save_image(image=file, folder='recipes') recipe.cover_image = filename recipe.save() clear_cache('/recipes') # clears old cache data when updated return recipe_cover_schema.dump(recipe).data, HTTPStatus.OK