예제 #1
0
def new_category():
    """ Route that renders the page to add a new category.

    This method validate that the user is logged in.
    The category is associated with the current logged in user.

    Raises:
        If an error occurs the application will redirect to index page and a flash message
        will be displayed with the proper Exception message.
    """
    try:
        logged_in = 'username' in login_session
        if not logged_in:
            flash("You must be logged to perform this operation", category="error")
            return redirect(url_for('index'))
        form = CategoryForm()
        category = Category()
        category.name = "New item"
        if form.validate_on_submit():
            form.populate_obj(category)
            category.user_id = login_session["user_id"]
            db_session.add(category)
            db_session.commit()
            flash("Category '{}' successfully added".format(category.name))
            return redirect(url_for('get_category', category_id=category.id))
        else:
            categories = db_session.query(Category).order_by(Category.name).all()
            return render_template('new_category.html', categories=categories,
                                   active_category=-1, form=form, logged_in=logged_in,
                                   login_session=login_session)
    except Exception as e:
        flash('An error has occurred: {}'.format(str(e)), 'error')
        return redirect(url_for('index'))
예제 #2
0
def edit_category(category_id):
    """ Route that renders the page to edit an category.

    This method validate that the user is logged in, and the category were created by him, to avoid
     malicious behaviors in the url.

    Args:
        category_id: The id of the category to be edited.

    Raises:
        If an error occurs the application will redirect to index page and a flash message
        will be displayed with the proper Exception message.
    """
    try:
        logged_in = 'username' in login_session
        if not logged_in:
            flash("You must be logged to perform this operation",
                  category="error")
            return redirect(url_for('index'))
        category = db_session.query(Category).filter_by(id=category_id).one()
        if login_session['user_id'] != category.user_id:
            flash("You can only modify categories created by you",
                  category="error")
            return redirect(url_for('get_category', category_id=category_id))
        form = CategoryForm(request.form, category)
        if form.validate_on_submit():
            form.populate_obj(category)
            db_session.add(category)
            db_session.commit()
            flash("Category '{}' successfully updated".format(category.name))
            return redirect(url_for('get_category', category_id=category.id))
        else:
            categories = db_session.query(Category).order_by(
                Category.name).all()
            return render_template('edit_category.html',
                                   categories=categories,
                                   active_category=category_id,
                                   form=form,
                                   logged_in=logged_in,
                                   login_session=login_session)
    except Exception as e:
        flash('An error has occurred: {}'.format(str(e)), 'error')
        return redirect(url_for('index'))
예제 #3
0
def new_category():
    """ Route that renders the page to add a new category.

    This method validate that the user is logged in.
    The category is associated with the current logged in user.

    Raises:
        If an error occurs the application will redirect to index page and a flash message
        will be displayed with the proper Exception message.
    """
    try:
        logged_in = 'username' in login_session
        if not logged_in:
            flash("You must be logged to perform this operation",
                  category="error")
            return redirect(url_for('index'))
        form = CategoryForm()
        category = Category()
        category.name = "New item"
        if form.validate_on_submit():
            form.populate_obj(category)
            category.user_id = login_session["user_id"]
            db_session.add(category)
            db_session.commit()
            flash("Category '{}' successfully added".format(category.name))
            return redirect(url_for('get_category', category_id=category.id))
        else:
            categories = db_session.query(Category).order_by(
                Category.name).all()
            return render_template('new_category.html',
                                   categories=categories,
                                   active_category=-1,
                                   form=form,
                                   logged_in=logged_in,
                                   login_session=login_session)
    except Exception as e:
        flash('An error has occurred: {}'.format(str(e)), 'error')
        return redirect(url_for('index'))
예제 #4
0
def edit_category(category_id):
    """ Route that renders the page to edit an category.

    This method validate that the user is logged in, and the category were created by him, to avoid
     malicious behaviors in the url.

    Args:
        category_id: The id of the category to be edited.

    Raises:
        If an error occurs the application will redirect to index page and a flash message
        will be displayed with the proper Exception message.
    """
    try:
        logged_in = 'username' in login_session
        if not logged_in:
            flash("You must be logged to perform this operation", category="error")
            return redirect(url_for('index'))
        category = db_session.query(Category).filter_by(id=category_id).one()
        if login_session['user_id'] != category.user_id:
            flash("You can only modify categories created by you", category="error")
            return redirect(url_for('get_category', category_id=category_id))
        form = CategoryForm(request.form, category)
        if form.validate_on_submit():
            form.populate_obj(category)
            db_session.add(category)
            db_session.commit()
            flash("Category '{}' successfully updated".format(category.name))
            return redirect(url_for('get_category', category_id=category.id))
        else:
            categories = db_session.query(Category).order_by(Category.name).all()
            return render_template('edit_category.html', categories=categories,
                                   active_category=category_id, form=form, logged_in=logged_in,
                                   login_session=login_session)
    except Exception as e:
        flash('An error has occurred: {}'.format(str(e)), 'error')
        return redirect(url_for('index'))