def delete(recipe_id): recipe_query = get_recipe(recipe_id) config = configparser_results(current_app.config['CONFIG_INI']) if recipe_query: if config.getboolean('general', 'backups_enabled'): backup_recipe_file(recipe_query['filename']) backup_database_file() delete_file(current_app.config['RECIPE_FILES'], recipe_query['filename']) if 'image' in recipe_query and recipe_query['image'] != None: if config.getboolean('general', 'backups_enabled'): backup_image_file(recipe_query['image']) delete_file(current_app.config['RECIPE_IMAGES'], recipe_query['image']) delete_recipe(recipe_id) flash(f"Recipe \"{recipe_query['title']}\" deleted.", 'success') return redirect(url_for('main.index')) flash("Recipe not deleted, ID not found.", 'danger') return redirect(url_for('main.index'))
def delete_recipe_json(recipe_id): recipe_query = get_recipe(recipe_id) config = configparser_results(current_app.config['CONFIG_INI']) if recipe_query: if config.getboolean('general', 'backups_enabled'): backup_recipe_file(recipe_query['filename']) backup_database_file() delete_file(current_app.config['RECIPE_FILES'], recipe_query['filename']) if 'image' in recipe_query and recipe_query['image'] != None: if config.getboolean('general', 'backups_enabled'): backup_image_file(recipe_query['image']) delete_file(current_app.config['RECIPE_IMAGES'], recipe_query['image']) delete_recipe(recipe_id) return jsonify({'success': 'recipe deleted'}) return jsonify({'error': 'recipe ID not found'})
def update(recipe_id): # Class to convert recipe dict to obj class Struct: def __init__(self, **entries): self.__dict__.update(entries) config = configparser_results(current_app.config['CONFIG_INI']) recipe_query = get_recipe(recipe_id) if not recipe_query: abort(404) recipe_query['tags'] = ", ".join(recipe_query['tags']) recipe_obj = Struct(**recipe_query) form = UpdateRecipeForm(obj=recipe_obj) if form.validate_on_submit(): title_formatted = form.title.data.replace(" ", "-").lower() filename = f"{title_formatted}.yaml" image_data = form.image.data tags = form.tags.data.split(',') while("" in tags) : tags.remove("") if hasattr(image_data, 'filename'): if config.getboolean('general', 'backups_enabled'): backup_image_file(recipe_query['image']) ext_new = image_data.filename.rsplit('.', 1)[1].lower() ext_old = recipe_query['image'].rsplit('.', 1)[1].lower if ext_new != ext_old: delete_file(current_app.config['RECIPE_IMAGES'], recipe_query['image']) image_data.filename = f'{title_formatted}.{ext_new}' image = secure_filename(image_data.filename) save_image(current_app.config['RECIPE_IMAGES'], image, image_data) else: image = recipe_query['image'] recipe = OrderedDict({ 'layout' : form.layout.data or recipe_query['layout'], 'title' : form.title.data, 'title_formatted' : title_formatted, 'image' : image, 'imagecredit' : form.imagecredit.data or None, 'tags' : [tag.strip(' ').lower() for tag in tags], 'source' : form.source.data or None, 'prep' : form.prep.data or None, 'cook' : form.cook.data or None, 'ready' : form.ready.data or None, 'servings' : form.servings.data or None, 'calories' : form.calories.data or None, 'description' : form.description.data or None, 'ingredients' : [x for x in form.ingredients.data if x != ''], 'directions' : [x for x in form.directions.data if x != ''], 'notes' : [x for x in form.notes.data if x != ''], 'filename' : filename }) if recipe['title'] != recipe_query['title']: if recipe['title_formatted'] != recipe_query['title_formatted']: # Rename image ext = image.rsplit('.', 1)[1].lower() updated_filename = f'{title_formatted}.{ext}' secure_file = secure_filename(updated_filename) rename_file(os.path.join(current_app.config['RECIPE_IMAGES'], image), os.path.join(current_app.config['RECIPE_IMAGES'], secure_file)) recipe['image'] = secure_file # Rename file rename_file(os.path.join(current_app.config['RECIPE_FILES'], recipe_query['filename']), os.path.join(current_app.config['RECIPE_FILES'], recipe['filename'])) if config.getboolean('general', 'backups_enabled'): backup_recipe_file(recipe_query['filename']) backup_database_file() create_recipe_file(current_app.config['RECIPE_FILES'], recipe) file = os.path.join(current_app.config['RECIPE_FILES'], recipe['filename']) recipe['file_hash'] = hash_file(file) update_recipe(recipe_query['filename'], recipe_query['title'], recipe) return redirect(url_for('main.recipe', title_formatted=recipe['title_formatted'])) return render_template("update.html", form=form, id=recipe_query['id'], image=recipe_query['image'])
def get_recipe_json(recipe_id): recipe = get_recipe(recipe_id) if recipe is False: return jsonify({'error': 'ID not found'}) else: return jsonify({'recipe': recipe})
def update_recipes_json(recipe_id): data = request.get_json() downloadImage = request.args.get('downloadImage') config = configparser_results(current_app.config['CONFIG_INI']) recipe_query = get_recipe(recipe_id) if not recipe_query: return jsonify({'error': 'recipe ID not found'}) if not data.get('layout'): return jsonify({'error': 'layout missing'}) if not data.get('title'): return jsonify({'error': 'title missing'}) title_formatted = data.get('title').replace(" ", "-").lower() filename = f'{title_formatted}.yaml' if check_for_duplicate_title_f(recipe_id, title_formatted): return jsonify({'error': 'recipe name must be unique'}) recipe = OrderedDict({ 'layout': data.get('layout'), 'title': data.get('title'), 'title_formatted': title_formatted, 'image': data.get('image'), 'imagecredit': data.get('imagecredit'), 'tags': data.get('tags'), 'source': data.get('source'), 'prep': data.get('prep'), 'cook': data.get('cook'), 'ready': data.get('ready'), 'servings': data.get('servings'), 'calories': data.get('calories'), 'description': data.get('description'), 'ingredients': data.get('ingredients'), 'directions': data.get('directions'), 'notes': data.get('notes'), 'filename': filename }) if downloadImage: if downloadImage != "true": return jsonify( {'error': 'downloadImage set but has invalid value'}) if not recipe['imagecredit']: return jsonify( {'error': 'downloadImage set but imagecredit is empty'}) if config.getboolean('general', 'backups_enabled'): backup_image_file(recipe_query['image']) image = download_image(recipe['imagecredit'], current_app.config['RECIPE_IMAGES'], recipe['title_formatted']) if not image: return jsonify({ 'error': 'image download failed', 'message': 'check imagecredit value' }) recipe['image'] = image if recipe['title'] != recipe_query['title']: if recipe['title_formatted'] != recipe_query['title_formatted']: # Rename image ext = recipe['image'].rsplit('.', 1)[1].lower() updated_filename = f'{title_formatted}.{ext}' secure_file = secure_filename(updated_filename) rename_file( os.path.join(current_app.config['RECIPE_IMAGES'], recipe_query['image']), os.path.join(current_app.config['RECIPE_IMAGES'], secure_file)) recipe['image'] = secure_file # Rename file rename_file( os.path.join(current_app.config['RECIPE_FILES'], recipe_query['filename']), os.path.join(current_app.config['RECIPE_FILES'], recipe['filename'])) if config.getboolean('general', 'backups_enabled'): backup_recipe_file(recipe_query['filename']) create_recipe_file(current_app.config['RECIPE_FILES'], recipe) file = os.path.join(current_app.config['RECIPE_FILES'], recipe['filename']) recipe['file_hash'] = hash_file(file) update_recipe(recipe_query['filename'], recipe_query['title'], recipe) return jsonify({'success': 'recipe updated'})