def patch(self, politician_id): json_data = request.get_json() data, errors = politician_schema.load(data=json_data, partial=True) if errors: return {'message': 'Validation errors', 'errors': errors}, 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 politician.name = data.get('name') or politician.name politician.position = data.get('position') or politician.position politician.description = data.get('description') or politician.description politician.age = data.get('age') or politician.age politician.gender = data.get('gender') or politician.gender politician.bio_data = data.get('bio_data') or politician.bio_data politician.c_vitae = data.get('c_vitae') or politician.c_vitae politician.county = data.get('county') or politician.county politician.constituency = data.get('constituency') or politician.constituency politician.ward = data.get('ward') or politician.ward politician.save() clear_cache('/politicians') return politician_schema.dump(politician).data, HTTPStatus.OK
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): # print(request.files) file = request.files.get("cover") if not file: return {"message": "Not a valid image"}, HTTPStatus.BAD_REQUEST if not 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 = os.path.join(os.environ.get("UPLOAD_RECIPES_FOLDER"), 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() clear_cache("/recipes") return recipe_cover_schema.dump(recipe), 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 patch(self, recipe_id): """This method has got the logic to update the recipe details""" json_data = request.get_json() data, errors = recipe_schema.load(data=json_data, partial=('name',)) if errors: return {'message': 'Validation errors', 'errors': errors}, HTTPStatus.BAD_REQUEST recipe = Recipe.get_by_id(recipe_id=recipe_id) # Check whether the recipe exists and whether the user has update privileges 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 # Update the recipe details and then save them in the database recipe.name = data.get('name') or recipe.name recipe.description = data.get('description') or recipe.description recipe.num_of_servings = data.get('num_of_servings') or recipe.num_of_servings recipe.cook_time = data.get('cook_time') or recipe.cook_time recipe.ingredients = data.get('ingredients') or recipe.ingredients recipe.directions = data.get('directions') or recipe.directions recipe.save() # Clear cache clear_cache('/recipes') # Finally, return the recipe in a JSON format and with status code HTTP 200 OK return recipe_schema.dump(recipe).data, HTTPStatus.OK
def patch(self, recipe_id): json_data = request.get_json() try: data = recipe_schema.load(data=json_data, partial=("name", )) except Exception as errors: return ( { "message": "Validation errors", "errors": errors.messages }, 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 # updates object with dicts attributes ! for key, value in data.items(): setattr(recipe, key, value) recipe.save() clear_cache("/recipes") return recipe_schema.dump(recipe), HTTPStatus.OK
def put(self): """This method has the logic to put the user avatar image file""" file = request.files.get('avatar') # Validate image if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST user = User.get_by_id(id=get_jwt_identity()) if user.avatar_image: avatar_path = image_set.path(folder='avatar', filename=user.avatar_image) # Check is avatar exist and removed before we replace it with our uploaded image if os.path.exists(avatar_path): os.remove(avatar_path) # Save image filename = save_image(image=file, folder='avatars') # Store the filename of image withing 'user.avatar_image' user.avatar_image = filename # Save image update to the database user.save() # Clear cache clear_cache('/recipes') # Finally, return the URL image in a JSON format and with status code HTTP 200 OK 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() clear_cache('/recipes') return recipe_cover_schema.dump(recipe).data, HTTPStatus.OK
def patch(self, recipe_id): json_data = request.get_json() data, errors = recipe_schema.load(data=json_data, partial=('name', )) if errors: return { 'message': 'Validation errors', 'errors': errors }, 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 recipe.name = data.get('name') or recipe.name recipe.description = data.get('description') or recipe.description recipe.num_of_servings = data.get( 'num_of_servings') or recipe.num_of_servings recipe.cook_time = data.get('cook_time') or recipe.cook_time recipe.ingredients = data.get('ingredients') or recipe.ingredients recipe.directions = data.get('directions') or recipe.directions recipe.save() clear_cache('/recipes') return recipe_schema.dump(recipe).data, HTTPStatus.OK
def delete(self, recipe_id): recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {"message": "recipe not found"}, HTTPStatus.NOT_FOUND recipe.is_publish = False recipe.save() clear_cache("/recipes") return {"message": "recipe will not published"}, HTTPStatus.OK
def delete(self, recipe_id): 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 not allowed"}, HTTPStatus.FORBIDDEN recipe.delete() clear_cache("/recipes") return {"recipe deleted": recipe.id}, HTTPStatus.OK
def delete(self, recipe_id): recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'recipe not found'}, HTTPStatus.NOT_FOUND if get_jwt_identity() != recipe.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN recipe.is_public = False recipe.save() clear_cache('/recipes') return {}, HTTPStatus.NO_CONTENT
def put(self, recipe_id): recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'recipe not found'}, HTTPStatus.NOT_FOUND if get_jwt_identity() != recipe.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN recipe.is_public = True recipe.save() clear_cache('/recipes') return recipe_schema.dump(recipe).data, HTTPStatus.OK
def put(self, recipe_id): recipe = Recipe.get_by_id(recipe_id=recipe_id) if not recipe: 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 recipe.is_publish = True recipe.save() clear_cache('/recipes') return {}, HTTPStatus.NO_CONTENT
def delete(self, recipe_id): # delete 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 is not allowed'}, HTTPStatus.FORBIDDEN recipe.delete() clear_cache('/recipes') return {}, HTTPStatus.NO_CONTENT
def merge(cls, index, sleep=5): ''' merge the Auxiliary index to the MainIndex. ''' objs = cls.objects.all() seg_length = 100 obj_lists = [objs[x:x + seg_length] for x in range(0, len(objs), seg_length)] for obj_list in obj_lists: time.sleep(sleep) for obj in obj_list: index.objects.add_postings(obj.token, obj.postings) objs.delete() clear_cache()
def cache(clean, output) -> None: """Cache command to control output or clean cache memory""" if clean: clear_cache() click.echo('removed cache') if output: keys = get_cache_keys() for key in keys: output, date = get_cache_value(key) search_term, world = key click.echo(f'\nSearch Term: {search_term}') click.echo(f'Cache date: {date}') click.echo(f'World set to: {world}') click.echo(output)
def worker_run_tests(ip, test_order, group_id): test_results = {} if not check_url_availability(ip): test_results['verdict'] = 'not_reachable' test_results['test_order'] = test_order[0] # TOF :( test_results['log'] = 'not reachable' test_results['trace'] = '' return test_results if test_order is not None: for test_id in test_order: test_name = 'test_{}'.format(test_id) test_function = getattr(tests, test_name) try: test_result, string_output, stack_trace = run_test( test_function, ip, group_id) except TimeoutError: test_result, string_output, stack_trace = False, 'timeout', 'timeout' test_results['verdict'] = 'ok' if test_result else 'ez_sag' test_results['test_order'] = test_id test_results['log'] = string_output test_results['trace'] = stack_trace else: for entry in dir(tests): if not entry.startswith('_'): test_function = getattr(tests, entry) try: options = webdriver.ChromeOptions() options.add_argument('headless') driver = webdriver.Chrome(chrome_options=options) test_result, string_output, stack_trace = run_test( test_function, ip, group_id) driver.delete_all_cookies() utils.clear_cache(driver) driver.close() except TimeoutError: test_result, string_output, stack_trace = False, 'timeout', 'timeout' test_results[entry] = test_result test_results['{}_log'.format(entry)] = string_output test_results['{}_trace'.format(entry)] = stack_trace return test_results
def delete(self, recipe_id): """ :param """ recipe = next( (recipe for recipe in recipe_list if recipe.id == recipe_id), None) if recipe is None: return {'message': 'recipe not found'}, HTTPStatus.NOT_FOUND recipe.is_pubish = False clear_cache('/recipes') # clears old cache data when updated return {}, HTTPStatus.NO_CONTENT
def delete(self, politician_id): 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 politician.delete() clear_cache('/politicians') return {}, HTTPStatus.NO_CONTENT
def put(self): file = request.files.get("avatar") if not file: return {"message": "Not a valid image."}, HTTPStatus.BAD_REQUEST if not allowed_file(file.filename): return {"message": "File type not allowed."}, HTTPStatus.BAD_REQUEST user = User.get_by_id(get_jwt_identity()) if user.avatar_image: # avatar_path = image_set.path(folder="avatars", filename=user.avatar_image) avatar_path = os.path.join( os.environ.get("UPLOAD_AVATARS_FOLDER"), 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("/recipes") return user_avatar_schema.dump(user), HTTPStatus.OK
def put(self, recipe_id): """ :param """ 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 recipe.is_publish = True #recipe.is_publish = True recipe.save() clear_cache('/recipes') # clears old cache data when updated return {}, HTTPStatus.NO_CONTENT
def delete(self, recipe_id): """ Delete a pre-existing recipe. :params: recipe_id : id of the recipe subject to deletion. """ 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 no allowed'}, HTTPStatus.FORBIDDEN recipe.delete() clear_cache('/recipes') # clears old cache data when updated return {}, HTTPStatus.NO_CONTENT
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 delete(self, recipe_id): """This method has got the logic to unpublish a previously published recipe.""" recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'Recipe not found'}, HTTPStatus.NOT_FOUND # Only an authenticated user can unpublished the recipe current_user = get_jwt_identity() if current_user != recipe.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN recipe.is_publish = False recipe.save() # Clear cache clear_cache('/recipes') # And return an empty JSON with status code HTTP NO_CONTENT return {}, HTTPStatus.NO_CONTENT
def put(self, recipe_id): """This method has got the logic to publish a recipe""" recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'Recipe not found'}, HTTPStatus.NOT_FOUND # Only users who have logged in can publish their own recipes current_user = get_jwt_identity() if current_user != recipe.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN recipe.is_publish = True recipe.save() # Clear cache clear_cache('/recipes') # And return an empty JSON with status code HTTP NO_CONTENT return {}, HTTPStatus.NO_CONTENT
def delete(self, recipe_id): """This method has the logic to delete a recipe that has been published""" recipe = Recipe.get_by_id(recipe_id=recipe_id) # Check whether the recipe exists and whether the user has privileges to delete it 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 # Delete recipe recipe.delete() # Clear cache clear_cache('/recipes') # And return an empty JSON with status code HTTP NO_CONTENT return {}, HTTPStatus.NO_CONTENT
def search_anime(keywords): url = search_t.substitute(name=keywords) soup = utils.get_soup(url) plist = soup.find("ul", {"class": "pagination-list"}) utils.clear_cache() def search_results(s): all_res = s.find("ul", {"class": "items"}) for list_item in all_res.find_all("li"): an = list_item.p.a["href"].split("/")[-1] utils.write_cache(an, append=True) outputs.normal_info(an, end=" \t") outputs.normal_info(list_item.p.a.text) search_results(soup) if plist: for list_item in plist.find_all("li", {"class": None}): url = search_page_t.substitute(name=keywords, page=list_item.a.text) soup = utils.get_soup(url) search_results(soup)
def search_anime(args): name = " ".join(args) url = config.search_t.substitute(name=name) soup = utils.get_soup(url) plist = soup.find('ul', {'class': 'pagination-list'}) utils.clear_cache() def search_results(s): all_res = s.find('ul', {'class': 'items'}) for list_item in all_res.find_all('li'): an = list_item.p.a['href'].split('/')[-1] utils.write_cache(an, append=True) outputs.normal_info(an, end=' \t') outputs.normal_info(list_item.p.a.text) search_results(soup) if plist: for list_item in plist.find_all('li', {'class': None}): url = config.search_page_t.substitute(name=name, page=list_item.a.text) soup = utils.get_soup(url) search_results(soup)
def patch(self, recipe_id): """ Makes modifications to a preexisting recipe. The JSON Web Token from the user of origin is required to make changes to the recipe. :params: recipe_id : The id of the recipe subject to change. """ json_data = request.get_json() data, errors = recipe_schema.load(data=json_data, partial=('name', )) if errors: return { 'message': 'Validation errors', 'errors': errors }, 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 recipe.name = data.get('name') or recipe.name recipe.description = data.get('description') or recipe.description recipe.num_of_servings = data.get( 'num_of_servings') or recipe.num_of_servings recipe.cook_time = data.get('cook_time') or recipe.cook_time recipe.directions = data.get('directions') or recipe.directions recipe.ingredients = data.get('ingredients') or recipe.ingredients recipe.save() clear_cache('/recipes') # clears old cache data when updated return recipe_schema.dump(recipe).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
def get(self): clear_cache(None) self.redirect("/")