Exemple #1
0
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()
Exemple #2
0
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 create_dish(name,
                description,
                image,
                price,
                quantity,
                category_id,
                show_usd=False):
    check_quantity = ''
    if quantity != check_quantity:
        check_quantity = quantity
    elif quantity == check_quantity:
        check_quantity = 0

    dish = Dish(name=str.strip(name),
                description=str.strip(description),
                price=price,
                quantity=check_quantity,
                category_id=category_id,
                show_usd=show_usd)
    if type(image) is str and image != '':
        file_path = os.path.join(Config.UPLOAD_DIRECTORY, image)
        dish.image_path = file_path
    elif image and image.filename != '':
        file_path = os.path.join(Config.UPLOAD_DIRECTORY, image.filename)
        files.save_file(image, file_path, recreate=True)
        dish.image_path = file_path
    db.session.add(dish)
    db.session.commit()
    return dish
Exemple #4
0
def create_dish(name_ru,
                name_uz,
                description_ru,
                description_uz,
                image,
                price,
                category_id,
                show_usd=False):
    dish = Dish(name=name_ru,
                name_uz=name_uz,
                description=description_ru,
                description_uz=description_uz,
                price=price,
                category_id=category_id,
                show_usd=show_usd)
    if type(image) is str:
        file_path = os.path.join(Config.UPLOAD_DIRECTORY, image)
        dish.image_path = file_path
    elif image and image.filename != '':
        file_path = os.path.join(Config.UPLOAD_DIRECTORY, image.filename)
        files.save_file(image, file_path, recreate=True)
        dish.image_path = file_path
    db.session.add(dish)
    db.session.commit()
    return dish
Exemple #5
0
 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
Exemple #6
0
def parse_excel():
    excel_file = request.files['file']
    if excel_file and excel_file.filename:
        file_path = os.path.join(Config.UPLOAD_DIRECTORY, excel_file.filename)
        files.save_file(excel_file, file_path, recreate=True)
        excelservice.parse_excel_file(file_path)
        flash('Файл {0} успешно сохранён в каталог!'.format(
            excel_file.filename),
              category='success')
    return redirect(url_for('admin.catalog'))
def create_category(name_ru: str, parent_id=0, image=None) -> DishCategory:
    if parent_id == 0:
        parent_id = None
    category = DishCategory(name=str.strip(name_ru), parent_id=parent_id)
    if image and image.filename != '':
        file_path = os.path.join(Config.UPLOAD_DIRECTORY, image.filename)
        files.save_file(image, file_path, recreate=True)
        category.image_path = file_path
    db.session.add(category)
    db.session.commit()
    return category
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
Exemple #9
0
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()
Exemple #10
0
def create_test(question: str, publish_date: str, publish_time: str,
                quiz_id: int, options: list, file):
    publish_date_time = datetime.strptime(publish_date + ' ' + publish_time,
                                          '%d.%m.%Y %H:%M')
    quiz = Quiz.get_by_id(quiz_id)
    channel_id = quiz.channel_id
    test = Test(question=question,
                quiz_id=quiz_id,
                channel_id=channel_id,
                publish_date=publish_date_time)
    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 != '':
        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.add(test)
    db.session.commit()
    return test