コード例 #1
0
ファイル: __init__.py プロジェクト: torston/item-catalog
def item_details_add():
    """
    item_details_add: add item to selected category
    :return: html create item or redirect in case of POST (item creation)
    """
    if request.method == 'POST':
        item = CatalogItem()
        item.name = request.form['name']
        item.description = request.form['description']
        item.category = session.query(Category).filter(
            func.lower(Category.name) == func.lower(
                request.form['category_name'])).first()

        item.user = session.query(CatalogUser).filter_by(
            id=login_session['user_id']).one()

        session.add(item)

        session.commit()

        return redirect(url_for('home'), code=301)

    else:
        categories = session.query(Category).all()
        item = CatalogItem()

        item.name = ''
        item.description = ''

        return render_template(
            'item_details_add.html',
            item=item,
            categories=categories,
            username=login_session.get("username", None),
        )
コード例 #2
0
def newMenuItem(categories_id):
    categories = session.query(Categories).filter_by(id=categories_id).one()
    if login_session['user_id'] != categories.user_id:
        return """
        <script>
        function myFunction() {alert(
            'You are not authorized to add menu items to this restaurant.
            Please create your own restaurant in order to add items.')}
            </script> < body onload = 'myFunction()''>"""
    if request.method == 'POST':
        newItem = CatalogItem(categories_id=categories_id,
                              user_id=categories.user_id)
        if request.form['name'] == "" or request.form[
                'description'] == "" or request.form['price'] == "":
            flash('All input fields required')
        else:
            newItem.name = request.form['name']
            newItem.description = request.form['description']
            newItem.price = request.form['price']
            session.add(newItem)
            session.commit()
            flash('New Catalog %s Item Successfully Created' % (newItem.name))
        return redirect(url_for('showMenu', categories_id=categories_id))
    else:
        return render_template('newmenuitem.html', categories_id=categories_id)