Exemplo n.º 1
0
def itemAddOrUpdate(request, item_id=None):
    """Adds an item if not present, otherwise updates with values from request
    instance.

    Arg:
        request: request instance from user interaction on page
        item_id: If present, the record for this id will be updated.

    Returns:
        If item is added/updated, redirects to parent category.
        If item add/update fails, page reloads with error message.
        If informatin is missing, page reloads with error message.
    """
    name, category_id, description = itemAdminFields(request)
    user = getUser()
    if name and description and category_id and user:
        item = Item.add_or_update(name, description, category_id, user.user_id,
                                  item_id)
        if item:
            msg = 'New Item Successfully Updated'
            flash(msg)
            return redirect('/category/' + category_id)
        else:
            msg = 'An Error Occurred, Item did not update or add'
            flash(msg)
            # Reload page with their values
            return render_template('item_admin.html',
                                   name=name,
                                   category_id=category_id,
                                   description=description,
                                   user_curr=getUser())
    else:
        msg = 'Please provide a value in all fields'
        flash(msg)
        return render_template('item_admin.html', user_curr=getUser())