Exemplo n.º 1
0
def newCategoryItem(category_id):
    """ Allow the category creator to create a new item. """
    category = session.query(Category).filter_by(id=category_id).one()
    # Prevent non-creator users adding new items.
    if login_session['user_id'] != category.user_id:
        flash(
            'You are not authorized to add category items to this category. Please create your own category in order to add items.'
        )
        return redirect(url_for('categoryItems', category_id=category_id))
    if request.method == 'POST':
        newItem = CategoryItem(name=request.form['name'],
                               description=request.form['description'],
                               price=request.form['price'],
                               category_id=category_id,
                               user_id=category.user_id)
        # Check new item formatting, and reformat as default dollars if none given.
        if not newItem.price.startswith(
                str('$')) or not newItem.price.startswith(str('£')):
            newItem.price = '$' + newItem.price
        session.add(newItem)
        # Flush and obtain default created item id.
        session.flush()
        createdItemID = newItem.id
        flash('New Category %s Item Successfully Created' % (newItem.name))
        # If upload selected, proceed to upload photo page for item.
        if request.form['button'] == 'upload-image':
            return redirect(
                url_for('editCategoryItemImage',
                        category_id=category.id,
                        item_id=createdItemID))
        # Otherwise create item without image and return to category items page.
        else:
            return redirect(url_for('categoryItems', category_id=category_id))
    else:
        return render_template('newCategoryItem.html', category=category)
Exemplo n.º 2
0
def newCategoryItem(category_id):
    """ Allow the category creator to create a new item. """
    category = session.query(Category).filter_by(id=category_id).one()
    # Prevent non-creator users adding new items.
    if login_session['user_id'] != category.user_id:
        flash('You are not authorized to add category items to this category. Please create your own category in order to add items.')
        return redirect(url_for('categoryItems', category_id=category_id))
    if request.method == 'POST':
        newItem = CategoryItem(name=request.form['name'], description=request.form['description'], price=request.form[
                           'price'], category_id=category_id, user_id=category.user_id)
        # Check new item formatting, and reformat as default dollars if none given.
        if not newItem.price.startswith(str('$')) or not newItem.price.startswith(str('£')):
            newItem.price = '$' + newItem.price
        session.add(newItem)
        # Flush and obtain default created item id.
        session.flush()
        createdItemID = newItem.id
        flash('New Category %s Item Successfully Created' % (newItem.name))
        # If upload selected, proceed to upload photo page for item.
        if request.form['button'] == 'upload-image':
            return redirect(url_for('editCategoryItemImage', category_id=category.id, item_id=createdItemID))
        # Otherwise create item without image and return to category items page.
        else:
            return redirect(url_for('categoryItems', category_id=category_id))
    else:
        return render_template('newCategoryItem.html', category=category)