Exemplo n.º 1
0
def editCuisine(cuisine_id):
    """Edit Cuisine page route"""
    # Redirects to the login page if the user is not logged in
    if 'username' not in login_session:
        return redirect('/login')

    # Fetches the cuisine to edit it
    editedCuisine = session.query(Cuisine).filter_by(id=cuisine_id).one()

    # Checking authorization of the user
    if editedCuisine.user_id != login_session['user_id']:
        return """<script>function myFunction() {
        msg = 'You are not authorized to edit this Cuisine. ';
        msg += 'Please create your own Cuisine in order to edit.';
        alert(msg);
        window.location.replace('""" + url_for('index') + """');}
        </script>
        <body onload='myFunction()''>"""
    # Creates the cuisine form
    form = CuisineForm()
    # POST method functionality
    if request.method == 'POST':
        # Checks if all the fields are valid
        if form.validate_on_submit():
            # Updates the cuisine and commits changes
            editedCuisine.name = form.name.data
            flash('Cuisine Successfully Edited To %s' % editedCuisine.name)
            return redirect(url_for('showRecipes',
                                    cuisine_id=editedCuisine.id))
    return render_template('editCuisine.html',
                           form=form,
                           cuisine=editedCuisine)
Exemplo n.º 2
0
def new_cuisine():
    """
    Renders the cuisine creation form.
    :return: The rendered page.
    """
    form = CuisineForm()

    # Form-WTF implements CSRF using the Flask SECRET_KEY
    if form.validate_on_submit():
        new_cuisine = models.Cuisine(name=form.name.data)

        db.session.add(new_cuisine)
        db.session.commit()
        flask.flash("New cuisine created successfully!")
        return flask.redirect(flask.url_for('index'))

    return flask.render_template(
        'form_new_cuisine.html', form=form, stats=get_stats())
Exemplo n.º 3
0
def newCuisine():
    """New Cuisine page route"""
    # Redirects to the login page if the user is not logged in
    if 'username' not in login_session:
        return redirect('/login')

    # Creates the new cuisine form
    form = CuisineForm()
    # POST method functionality
    if request.method == 'POST':
        # Checks if all the fields are valid
        if form.validate_on_submit():
            # Creates the new cuisine and commits the changes
            cuisine = Cuisine(name=form.name.data,
                              user_id=login_session['user_id'])
            session.add(cuisine)
            session.commit()
            flash('New Cuisine %s Successfully Created' % (cuisine.name))
            return redirect(url_for('showRecipes', cuisine_id=cuisine.id))
    return render_template("newCuisine.html", form=form)
Exemplo n.º 4
0
def edit_cuisine(cuisine_id):
    """
    Renders the cuisine editing form.
    :param cuisine_id: ID of the cuisine to edit.
    :return: The rendered page.
    """
    cuis = Cuisine.query.get_or_404(cuisine_id)

    form = CuisineForm(obj=cuis)

    # Form-WTF implements CSRF using the Flask SECRET_KEY
    if form.validate_on_submit():
        cuis.name = form.name.data
        db.session.commit()

        flask.flash("Cuisine updated successfully!")
        return flask.redirect(flask.url_for('cuisine', cuisine_id=cuis.id))

    return flask.render_template(
        'form_edit_cuisine.html', form=form, cuisine=cuis, stats=get_stats())