def import_data(self, request):
     """Import the data for this recipe by either saving the image associated
     with this recipe or saving the metadata associated with the recipe. If
     the metadata is being processed, the title and description of the recipe
     must always be specified."""
     try:
         if 'recipe_image' in request.files:
             filename = images.save(request.files['recipe_image'])
             self.image_filename = filename
             self.image_url = images.url(filename)
         else:
             json_data = request.get_json()
             self.recipe_title = json_data['title']
             self.recipe_description = json_data['description']
             if 'recipe_type' in json_data:
                 self.recipe_type = json_data['recipe_type']
             if 'rating' in json_data:
                 self.rating = json_data['rating']
             if 'ingredients' in json_data:
                 self.ingredients = json_data['ingredients']
             if 'recipe_steps' in json_data:
                 self.recipe_steps = json_data['recipe_steps']
             if 'inspiration' in json_data:
                 self.inspiration = json_data['inspiration']
     except KeyError as e:
         raise ValidationError('Invalid recipe: missing ' + e.args[0])
     return self
Beispiel #2
0
def edit_recipe(recipe_id):
    form = EditRecipeForm(active=True)
    recipe_with_user = db.session.query(
        Recipe, User).join(User).filter(Recipe.id == recipe_id).first()
    if request.method == 'POST':
        if form.validate_on_submit():
            if form.recipe_image.data is not None:
                filename = images.save(request.files['recipe_image'])
                url = images.url(filename)

            else:
                filename = recipe_with_user.Recipe.image_filename
                url = recipe_with_user.Recipe.image_url

            uppdate = Recipe.query.filter_by(id=recipe_id).first()
            uppdate.recipe_title = form.recipe_title.data
            uppdate.recipe_description = form.recipe_description.data
            uppdate.is_public = form.is_public.data
            uppdate.image_filename = filename
            uppdate.image_url = url
            db.session.commit()
            flash('Recept, {}, har ändrats!'.format(uppdate.recipe_title),
                  'success')
            return redirect(url_for('recipes.user_recipes'))

        else:
            #flash_errors(form)
            flash('ERROR! Recipe was not added.', 'error')

    return render_template('edit_recipe.html',
                           form=form,
                           recipe=recipe_with_user,
                           recipe_id=recipe_id)
Beispiel #3
0
def add_recipe():
    form = AddRecipeForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            filename = images.save(request.files['recipe_image'])
            url = images.url(filename)
            new_recipe = Recipe(form.recipe_title.data,
                                form.recipe_description.data, current_user.id,
                                form.is_public.data, filename, url)
            db.session.add(new_recipe)
            db.session.commit()

            # Lägg in varje ingrediens för recepete genom att loopa igenom det som skickades från formuläret
            for ingredient in request.form.getlist("ingredient[]"):
                ing = ingredient.split("-")
                ingredient_id = ing[0]
                quantity = ing[1]
                new_recipe_ingredient = RecipeHasIngredient(
                    new_recipe.id, ingredient_id, quantity)
                db.session.add(new_recipe_ingredient)
                db.session.commit()

            flash('New recipe, {}, added!'.format(new_recipe.recipe_title),
                  'success')
            return redirect(url_for('recipes.user_recipes'))
        else:
            #flash_errors(form)
            flash('ERROR! Recipe was not added.', 'error')
    else:

        return render_template('add_recipe.html', form=form)
    def import_form_data(self, request, form):
        """Import the data for this recipe that was input via the EditRecipeForm
        class.  This can either be done by the user for the recipes that they own
        or by the administrator.  Additionally, it is assumed that the form has
        already been validated prior to being passed in here."""
        try:
            if form.recipe_title.data != self.recipe_title:
                self.recipe_title = form.recipe_title.data

            if form.recipe_description.data != self.recipe_description:
                self.recipe_description = form.recipe_description.data

            if form.recipe_public.data != self.is_public:
                self.is_public = form.recipe_public.data

            if form.recipe_dairy_free.data != self.dairy_free_recipe:
                self.dairy_free_recipe = form.recipe_dairy_free.data

            if form.recipe_soy_free.data != self.soy_free_recipe:
                self.soy_free_recipe = form.recipe_soy_free.data

            if form.recipe_type.data != self.recipe_type:
                self.recipe_type = form.recipe_type.data

            if form.recipe_rating.data != str(self.rating):
                self.rating = form.recipe_rating.data

            if form.recipe_image.has_file():
                filename = images.save(request.files['recipe_image'])
                self.image_filename = filename
                self.image_url = images.url(filename)

            if form.recipe_ingredients.data != self.ingredients:
                self.ingredients = form.recipe_ingredients.data

            if form.recipe_steps.data != self.recipe_steps:
                self.recipe_steps = form.recipe_steps.data

            if form.recipe_inspiration.data != self.inspiration:
                self.inspiration = form.recipe_inspiration.data

        except KeyError as e:
            raise ValidationError('Invalid recipe: missing ' + e.args[0])
        return self
Beispiel #5
0
def add_recipe():
    # Cannot pass in 'request.form' to AddRecipeForm constructor, as this will cause 'request.files' to not be
    # sent to the form.  This will cause AddRecipeForm to not see the file data.
    # Flask-WTF handles passing form data to the form, so not parameters need to be included.
    form = AddRecipeForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            filename = images.save(request.files['recipe_image'])
            url = images.url(filename)
            new_recipe = Recipe(form.recipe_title.data,
                                form.recipe_description.data, current_user.id,
                                True, filename, url)
            db.session.add(new_recipe)
            db.session.commit()
            flash("New Recipe, {}, added!".format(new_recipe.recipe_title),
                  'success')
            return redirect(url_for('recipes.user_recipes'))
        else:
            flash_errors(form)
            flash("ERROR! Recipe was not added.", 'error')
    return render_template('add_recipe.html', form=form)
def add_recipe():
    # Cannot pass in 'request.form' to AddRecipeForm constructor, as this will cause 'request.files' to not be
    # sent to the form.  This will cause AddRecipeForm to not see the file data.
    # Flask-WTF handles passing form data to the form, so not parameters need to be included.
    form = AddRecipeForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            filename = images.save(request.files['recipe_image'])
            url = images.url(filename)
            new_recipe = Recipe(
                form.recipe_title.data,
                form.recipe_description.data,
                current_user.id,
                form.recipe_public.data,
                filename,
                url,
                form.recipe_type.data,
                form.recipe_rating.data,  #  or None,
                form.recipe_ingredients.data,
                form.recipe_steps.data,
                form.recipe_inspiration.data,
                form.recipe_tested.data,
                form.recipe_verified_free.data)
            db.session.add(new_recipe)
            db.session.commit()
            if 'ACCOUNT_SID' in app.config and not app.config['TESTING']:
                new_user = User.query.filter_by(id=new_recipe.user_id).first()
                send_new_recipe_text_message(new_user.email,
                                             new_recipe.recipe_title)
            flash('New recipe, {}, added!'.format(new_recipe.recipe_title),
                  'success')
            return redirect(url_for('recipes.user_recipes', recipe_type='All'))
        else:
            flash_errors(form)
            flash('ERROR! Recipe was not added.', 'error')

    return render_template('add_recipe.html', form=form)
Beispiel #7
0
def go_images_upload():
    form = AddPaperForm()
    if request.method == 'POST':
        try:
            if form.validate_on_submit():
                # TODO: make this a secure filename
                filename = images.save(request.files['upload'])
                url = images.url(filename)
                new_paper = Paper(form.title.data, filename, url)
                db.session.add(new_paper)
                db.session.commit()
                flash('SUCCESS: Title {} added'.format(new_paper.title),
                      'success')
                return redirect(url_for('papers.go_images_upload'))

            else:
                flash('ERROR: try again', 'error')

        except IntegrityError:
            flash(
                'ERROR: Title {} is a duplicate, try again'.format(
                    new_paper.title), 'error')

    return render_template('images_upload.html', form=form)
def edit_recipe(recipe_id):
    # Cannot pass in 'request.form' to AddRecipeForm constructor, as this will cause 'request.files' to not be
    # sent to the form.  This will cause AddRecipeForm to not see the file data.
    # Flask-WTF handles passing form data to the form, so not parameters need to be included.
    form = EditRecipeForm()
    recipe = Recipe.query.filter_by(id=recipe_id).first_or_404()

    if not recipe.user_id == current_user.id:
        flash('Error! Incorrect permissions to edit this recipe.', 'error')
        return redirect(url_for('recipes.public_recipes'))

    if request.method == 'POST':
        if form.validate_on_submit():
            update_counter = 0

            if form.recipe_title.data is not None and form.recipe_title.data != recipe.recipe_title:
                flash(
                    'DEBUG: Updating recipe.recipe_title to {}.'.format(
                        form.recipe_title.data), 'debug')
                update_counter += 1
                recipe.recipe_title = form.recipe_title.data

            if form.recipe_description.data is not None and form.recipe_description.data != recipe.recipe_description:
                flash(
                    'DEBUG: Updating recipe.recipe_description to {}.'.format(
                        form.recipe_description.data), 'debug')
                update_counter += 1
                recipe.recipe_description = form.recipe_description.data

            if form.recipe_public.data != recipe.is_public:
                flash(
                    'DEBUG: Updating recipe.is_public to {}.'.format(
                        form.recipe_public.data), 'debug')
                update_counter += 1
                recipe.is_public = form.recipe_public.data

            if form.recipe_tested.data != recipe.tested_recipe:
                flash(
                    'DEBUG: Updating recipe.tested_recipe to {}.'.format(
                        form.recipe_tested.data), 'debug')
                update_counter += 1
                recipe.tested_recipe = form.recipe_tested.data

            if form.recipe_verified_free.data != recipe.verified_free_recipe:
                flash(
                    'DEBUG: Updating recipe.verified_free_recipe to {}.'.
                    format(form.recipe_verified_free.data), 'debug')
                update_counter += 1
                recipe.verified_free_recipe = form.recipe_verified_free.data

            if form.recipe_type.data != recipe.recipe_type:
                flash(
                    'DEBUG: Updating recipe.recipe_type to {}.'.format(
                        form.recipe_type.data), 'debug')
                update_counter += 1
                recipe.recipe_type = form.recipe_type.data

            if form.recipe_rating.data != str(recipe.rating):
                flash(
                    'DEBUG: Updating recipe.rating from {} to {}.'.format(
                        str(recipe.rating), form.recipe_rating.data), 'debug')
                update_counter += 1
                recipe.rating = form.recipe_rating.data

            if form.recipe_image.has_file():
                flash(
                    'DEBUG: Updating recipe.image_filename to {}.'.format(
                        form.recipe_image.data), 'debug')
                update_counter += 1
                filename = images.save(request.files['recipe_image'])
                recipe.image_filename = filename
                recipe.image_url = images.url(filename)

            if form.recipe_ingredients.data != recipe.ingredients:
                flash(
                    'DEBUG: Updating recipe.ingredients to {}.'.format(
                        form.recipe_ingredients.data), 'debug')
                update_counter += 1
                recipe.ingredients = form.recipe_ingredients.data

            if form.recipe_steps.data != recipe.recipe_steps:
                flash(
                    'DEBUG: Updating recipe.recipe_steps to {}.'.format(
                        form.recipe_steps.data), 'debug')
                update_counter += 1
                recipe.recipe_steps = form.recipe_steps.data

            if form.recipe_inspiration.data is not None and form.recipe_inspiration.data != recipe.inspiration:
                flash(
                    'DEBUG: Updating recipe.inspiration to {}.'.format(
                        form.recipe_inspiration.data), 'debug')
                update_counter += 1
                recipe.inspiration = form.recipe_inspiration.data

            if update_counter > 0:
                db.session.add(recipe)
                db.session.commit()
                flash(
                    'Recipe has been updated for {}.'.format(
                        recipe.recipe_title), 'success')
            else:
                flash(
                    'No updates made to the recipe ({}). Please update at least one field.'
                    .format(recipe.recipe_title), 'error')

            return redirect(
                url_for('recipes.recipe_details', recipe_id=recipe_id))
        else:
            flash_errors(form)
            flash('ERROR! Recipe was not edited.', 'error')

    return render_template('edit_recipe.html', form=form, recipe=recipe)