def update_test(test_id: int, question: str, publish_date: str, publish_time: str, options: list = None, file=None): test = Test.query.get_or_404(test_id) test.question = question publish_date_time = datetime.strptime(publish_date + ' ' + publish_time, '%d.%m.%Y %H:%M') test.publish_date = publish_date_time if options: current_options = test.options.all() for option in current_options: db.session.delete(option) for option in options: option = Option(value=option.data['value'], is_answer=option.data['is_answer']) test.options.append(option) db.session.add(option) if file and file.filename != '': if test.file_path: files.remove_file(test.file_path) file_path = os.path.join(Config.UPLOAD_FOLDER, file.filename) files.save_file(file, file_path, recreate=True) test.file_path = file_path db.session.commit() return test
def update_dish(dish_id, name, name_uz, description, description_uz, image, price, category_id, delete_image, show_usd, quantity, is_sale, sale_price): dish = get_dish_by_id(dish_id) dish.name = name dish.name_uz = name_uz dish.description = description dish.description_uz = description_uz dish.price = price dish.show_usd = show_usd dish.category_id = category_id dish.quantity = quantity dish.is_sale = is_sale dish.sale_price = sale_price if (image and image.filename != '') and not delete_image: if dish.image_path: files.remove_file(dish.image_path) file_path = os.path.join(Config.UPLOAD_DIRECTORY, image.filename) files.save_file(image, file_path) dish.image_id = None dish.image_path = file_path if delete_image: if dish.image_path: files.remove_file(dish.image_path) dish.image_path = None dish.image_id = None db.session.commit()
def save_file(test_id: int, file): test = Test.get_by_id(test_id) if test.file_path: files.remove_file(test.file_path) file_path = os.path.join(Config.UPLOAD_FOLDER, secure_filename(file.filename)) files.save_file(file, file_path, recreate=True) test.file_path = file_path db.session.commit() return file_path
def update_category(category_id: int, name_ru: str, parent_id=0, image=None): if parent_id == 0: parent_id = None category = DishCategory.query.get_or_404(category_id) category.name = str.strip(name_ru) category.parent_id = parent_id if image and image.filename != '': if category.image_path: files.remove_file(category.image_path) file_path = os.path.join(Config.UPLOAD_DIRECTORY, image.filename) files.save_file(image, file_path) category.image_id = None category.image_path = file_path db.session.commit() return category
def update_dish(dish_id, name_ru, name_uz, description_ru, description_uz, image, price, category_id, delete_image): dish = get_dish_by_id(dish_id) dish.name = name_ru dish.name_uz = name_uz dish.description = description_ru dish.description_uz = description_uz dish.price = price dish.category_id = category_id if (image and image.filename != '') and not delete_image: if dish.image_path: files.remove_file(dish.image_path) file_path = os.path.join(Config.UPLOAD_DIRECTORY, image.filename) files.save_file(image, file_path) dish.image_id = None dish.image_path = file_path if delete_image: if dish.image_path: files.remove_file(dish.image_path) dish.image_path = None dish.image_id = None db.session.commit()
def remove_test(test_id): test = Test.query.get_or_404(test_id) if test.file_path: files.remove_file(test.file_path) db.session.delete(test) db.session.commit()