Пример #1
0
def save_recipe(form, valid=True):
    """ Save newly created recipe. """
    recipe = Recipe(
        name=form.name.data,
        url=utils.get_url_from_name(form.name.data),
        id_user=current_user.id,
        id_state=form.state.data if valid else 1,
        id_season=form.season.data,
        time_prep=form.time_prep.data,
        time_cook=form.time_cook.data,
        intro=form.intro.data,
        text=form.text.data,
        health=form.health.data,
        link_video=form.link_video.data
    )
    db.session.add(recipe)
    db.session.commit()
    if recipe.id_state == 3:
        recipe.published_at = dt.now()
    update.update_steps(recipe, form)
    update.update_ingredients(recipe, form)
    db.session.commit()
    if form.image.data:
        store_image(form.image.data, 'recipes', recipe.url)
    return recipe
Пример #2
0
def update_ingredient(ingredient, form):
    """ Updates name, ingredients and steps of ingredient. """
    ingredient.changed_at = dt.now()
    if ingredient.name != form.name.data:
        new_name = form.name.data
        ingredient.name = new_name
        ingredient.url = get_url_from_name(new_name)
    if ingredient.health != form.health.data:
        ingredient.health = form.health.data
    db.session.commit()
Пример #3
0
def save_ingredient(form):
    """ Save the newly created ingredient. """
    db.session.add(
        Ingredient(
            name=form.name.data,
            url=get_url_from_name(form.name.data),
            health=form.health.data,
            created_by=current_user.id,
        ))
    db.session.commit()
Пример #4
0
def update_subrecipe(subrecipe, form):
    """ Updates name, ingredients and steps of subrecipe. """
    subrecipe.changed_at = dt.now()
    if subrecipe.name != form.name.data:
        new_name = form.name.data
        subrecipe.name = new_name
        subrecipe.url = get_url_from_name(new_name)
    if form.case.data != subrecipe.is_feminine:
        subrecipe.is_feminine = form.case.data
    update_ingredients(subrecipe, form)
    update_steps(subrecipe, form)
    db.session.commit()
Пример #5
0
def save_subrecipe(form):
    """ Save the newly created subrecipe. """
    subrecipe = Subrecipe(
        name=form.name.data,
        url=get_url_from_name(form.name.data),
        is_feminine=form.case.data,
        id_user=current_user.id,
    )
    db.session.add(subrecipe)
    db.session.commit()
    save_steps(form, subrecipe.id)
    save_ingredients(form, subrecipe.id)
    db.session.commit()
Пример #6
0
def validate(form):
    """ Validate the name and URL by checking if they are unique
    and call a function to validate the ingredients. """
    errors = list()
    name = form.name.data  # name of the subrecipe.
    url = get_url_from_name(name)
    query = Subrecipe.query.filter_by(id_user=current_user.id)
    if name == 'Nueva subreceta':
        errors.append(
            'Cambia el nombre de la subreceta donde pone "Nueva subreceta"')
    elif query.filter_by(name=name).first() is not None:
        errors.append(
            f'Ya existe una subreceta llamada "{name}". ¡Cambia el nombre!')
    elif query.filter_by(url=url).first() is not None:
        errors.append(
            f'Ya existe una subreceta cuya URL es "{url}". ¡Cambia el nombre!')
    return errors + validate_ingredients(form)
Пример #7
0
def validate_name(form, recipe=None):
    """ Validate the uniqueness of the name and URL. """
    name = form.name.data
    if recipe is not None and recipe.name == name:
        return list()  # It has already been validated.
    errors = list()
    url = get_url_from_name(name)
    query = Recipe.query.filter_by(id_user=current_user.id)
    if name == 'Nueva receta':
        errors.append(
            'Cambia el nombre de la receta donde pone "Nueva receta"'
        )
    elif query.filter_by(name=name).count() != 0:
        errors.append(
            f'Ya tienes una receta llamada "{name}". ¡Cambia el nombre!'
        )
    elif query.filter_by(url=url).count() != 0:
        errors.append(
            f'Ya tienes una receta cuya URL es "{url}". ¡Cambia el nombre!'
        )
    return errors
Пример #8
0
def update_recipe(recipe, form, valid=True):
    """ Find changes and update the appropriate elements. 
    Calls functions to update ingredients, steps, subrecipes and images. """
    recipe.changed_at = dt.now()
    if recipe.name != form.name.data:
        old_url = recipe.url
        new_name = form.name.data
        recipe.name = new_name
        recipe.url = get_url_from_name(new_name)
        if form.image.data:  # new image,  new name
            images.change_image(form.image.data, 'recipes', recipe.url,
                                old_url)
        else:  # same image, new name
            images.change_image(None, 'recipes', recipe.url, old_url)
    elif form.image.data:  # new image, same name
        images.change_image(form.image.data, 'recipes', None, recipe.url)
    if recipe.intro != form.intro.data:
        recipe.intro = form.intro.data
    if recipe.text != form.text.data:
        recipe.text = form.text.data
    if recipe.health != form.health.data:
        recipe.health = form.health.data
    if recipe.link_video != form.link_video.data:
        recipe.link_video = form.link_video.data
    if recipe.time_cook != form.time_cook.data:
        recipe.time_cook = form.time_cook.data
    if recipe.time_prep != form.time_prep.data:
        recipe.time_prep = form.time_prep.data
    if recipe.id_season != form.season.data:
        recipe.id_season = form.season.data
    if recipe.id_state != form.state.data:
        if valid or form.state.data == 1:
            recipe.id_state = form.state.data
            if recipe.id_state == 3:
                recipe.published_at = dt.now()
    update_ingredients(recipe, form)
    update_steps(recipe, form)
    db.session.commit()
    return recipe.url