Beispiel #1
0
def item_edit_route(item_name):
    """
    Route to edit item
    """
    target_item = get_item(item_name)
    # It is possible that user change the Item Name so use the previous

    # checking access rights
    if target_item.owner != user_info()['id']:
        flash('Only owner can edit item')
        return redirect(url_for('item_route', item_name=item_name))

    if target_item is None:
        flash('Item with this name does not exist')
        abort(404)

    if request.method == 'POST':
        update_item(target_item.id)
        flash('Item updated')
        # sending user to item page after edit is done
        return redirect(url_for('item_route', item_name=target_item.name))

    if request.method == 'GET':
        return render_template('item_edit.html',
                               page={'title': 'Edit item'},
                               user=user_info(),
                               content={
                                   'is_edit': True,
                                   'item': target_item
                               })
Beispiel #2
0
def item_delete_route(item_name):
    """
    Route to delete item
    """
    target_item = get_item(item_name)

    # checking access rights
    if target_item.owner != user_info()['id']:
        flash('Only owner can delete item')
        return redirect(url_for('item_route', item_name=item_name))

    if target_item is None:
        abort(404)

    if request.method == 'POST':
        delete_item(item_name)
        flash('Item deleted')
        # sending user to categories page for he has done
        return redirect(url_for('categories_route'))

    if request.method == 'GET':
        return render_template('confirm.html',
                               page={'title': 'Delete item'},
                               user=user_info(),
                               content={
                                   'message':
                                   'Do you really want delete item ' +
                                   target_item.name + '?'
                               })
Beispiel #3
0
def category_delete_route(category_name):
    """
    Deleting category from DB
    """
    target_category = get_category(category_name)

    # checking access rights
    if target_category.owner != user_info()['id']:
        flash('Only owner can delete category')
        return redirect(url_for('categories_route'))

    if target_category is None:
        abort(404)

    if request.method == 'POST':
        delete_category(category_name)
        flash('Category deleted')
        # sending user to list of categories after all he has done
        return redirect(url_for('categories_route'))

    # as polite people we will ask some configmation first,
    # also we need it for CSRF check
    if request.method == 'GET':
        return render_template('confirm.html',
                               page={'title': 'Delete category'},
                               user=user_info(),
                               content={
                                   'message':
                                   'Do you really want delete category ' +
                                   target_category.name + '?'
                               })
Beispiel #4
0
def category_edit_route(category_name):
    """
    Updating category info
    """
    target_category = get_category(category_name)

    # checking access rights
    if target_category.owner != user_info()['id']:
        flash('Only owner can edit category')
        return redirect(url_for('categories_route'))

    if target_category is None:
        abort(404)

    if request.method == 'POST':
        update_category(category_name)
        flash('Category updated')
        return redirect(url_for('categories_route'))

    if request.method == 'GET':
        return render_template('category_edit.html',
                               page={'title': 'Add category'},
                               user=user_info(),
                               content={
                                   'is_edit': True,
                                   'category': target_category
                               })
Beispiel #5
0
def categories_route():
    """
    List of all categories
    """
    return render_template('categories.html',
                           page={'title': 'Categories'},
                           user=user_info(),
                           content={'categories': get_categories()})
Beispiel #6
0
def index_route():
    # Homepage for web interface
    return render_template('index.html',
                           page={
                               'title': 'Catalog Application Homepage',
                               'has_sidebar': True
                           },
                           user=user_info(),
                           content={'categories': get_categories()})
Beispiel #7
0
def profile_route():
    # Shows user information on the page
    user = user_info()

    if not user_is_authorized():
        return redirect(url_for('login_route'))

    return render_template('profile.html',
                           page={'title': user['name'] + ' profile'},
                           user=user,
                           content={'categories': get_categories()})
Beispiel #8
0
def category_add_route():
    """
    Add new category to data base
    """
    if request.method == 'POST':
        add_category()
        flash('Category added')
        return redirect(url_for('categories_route'))

    if request.method == 'GET':
        return render_template('category_edit.html',
                               page={'title': 'Add category'},
                               user=user_info(),
                               content={'is_edit': False})
Beispiel #9
0
def item_route(item_name):
    """
    Route that outputs item info
    """
    target_item = get_item(item_name)

    if target_item is None:
        flash('Item not found')
        # sending user to categories page for he has done
        return redirect(url_for('categories_route'))

    return render_template('item.html',
                           page={
                               'title': 'Item ' + target_item.name,
                               'has_sidebar': True
                           },
                           user=user_info(),
                           content={
                               'categories': get_categories(),
                               'item': target_item
                           })
Beispiel #10
0
def category_route(category_name):
    """
    Outputing category info
    """
    target_category = get_category(category_name)
    print target_category

    # ooops category not found
    if target_category is None:
        abort(404)

    return render_template('category.html',
                           page={
                               'title': 'Category ' + target_category.name,
                               'has_sidebar': True
                           },
                           user=user_info(),
                           content={
                               'categories': get_categories(),
                               'category': target_category
                           })
Beispiel #11
0
def item_add_route(category_name):
    """
    Route to add new item
    """
    target_category = get_category(category_name)

    if target_category is None:
        abort(404)

    if request.method == 'POST':

        add_item(target_category.name)
        flash('Item added')
        return redirect(url_for('category_route', category_name=category_name))

    if request.method == 'GET':
        return render_template('item_edit.html',
                               page={'title': 'Add category'},
                               user=user_info(),
                               content={
                                   'is_edit': False,
                                   'category': target_category
                               })