Ejemplo n.º 1
0
def newItem(category_id):
    ''' This allows the user to create a new item from the category page'''
    # First we check to see if the user is actaully logged in
    # if 'username' not in login_session:
    #     return redirect('/login')
    print 'open'
    form = newItemForm()

    cat = session.query(Category).filter_by(id=category_id).one()

    if form.validate_on_submit():
        newItem = Item(name=request.form['name'],
                       description=request.form['description'],
                       picture=request.form['picture'])
        session.add(newItem)
        newItem.user_id = login_session['user_id']
        newItem.category_id = category_id
        session.add(newItem)

        flash('Item has been created!')
        return redirect(url_for('itemList', category_id=cat.id))

    if form_errors(form) == False:
        return redirect(url_for('itemList', category_id=cat.id))

    # if form.errors:
    #     for error in form.errors:
    #         flash(error)
    #     flash('There was an error in your input')
    #     return redirect(url_for('itemList', category_id=cat.id))

    return render_template('newItem.html', category_id=category_id, form=form)
Ejemplo n.º 2
0
def item_create(cid = None):
  if request.method == 'POST':
    print 'To process item creation.'
    item = Item()
    item.category_id = request.form['item-category_id']
    item.name = request.form['item-name']
    item.longtext = request.form["item-longtext"]
    file = request.files["item-image"]
    print type(file)
    item.image = file.read()
    item.user_id = get_user_id_from_session()
    result = item.add()
    if result[1] == 'error':
      flash(result[2], result[1])
    else:
      item.commit()
      return redirect(url_for('item.item_display', item_id = item.id))
  else:
    if cid == None:
      flash('Please select a category.', 'error')
      return redirect_common(url_for('category.home'))

  return render_template('item.html', title = 'Create item',\
                         item = Item(category_id = cid),\
                         display = False
                        )
  
Ejemplo n.º 3
0
def createItem():
    if 'username' not in login_session:
        return redirect(url_for('showLogin'))
    if request.method == 'POST':
        category = session.query(Category).filter_by(
            name=request.form['item-category']).first()
        newItem = Item()
        newItem.name = request.form['item-name']
        newItem.description = request.form['description']
        newItem.category = category
        newItem.user_id = getUserID(login_session['email'])
        try:
            with store_context(fs_store):
                if request.files['item_photo']:
                    newItem.picture.from_file(request.files['item_photo'])
                else:
                    newItem.picture.from_file(urlopen(dummy_item_photo))
                session.add(newItem)
                session.commit()
        except Exception:
            session.rollback()
            raise
        return redirect(url_for('showCategories'))
    else:
        categories = session.query(Category).order_by(asc(Category.name))
        return render_template('create_item.html', categories=categories,
                               login_session=login_session)
Ejemplo n.º 4
0
def createItem():
    if 'username' not in login_session:
        return redirect(url_for('showLogin'))
    if request.method == 'POST':
        category = session.query(Category).filter_by(
            name=request.form['item-category']).first()
        newItem = Item()
        newItem.name = request.form['item-name']
        newItem.description = request.form['description']
        newItem.category = category
        newItem.user_id = getUserID(login_session['email'])
        try:
            with store_context(fs_store):
                if request.files['item_photo']:
                    newItem.picture.from_file(request.files['item_photo'])
                else:
                    newItem.picture.from_file(urlopen(dummy_item_photo))
                session.add(newItem)
                session.commit()
        except Exception:
            session.rollback()
            raise
        return redirect(url_for('showCategories'))
    else:
        categories = session.query(Category).order_by(asc(Category.name))
        return render_template('create_item.html',
                               categories=categories,
                               login_session=login_session)
Ejemplo n.º 5
0
def create_item():
    """Handle request to create a new item."""

    item = Item()
    item.name = request.form["name"]
    item.category_id = request.form["category_id"]
    item.user_id = login_session["user_id"]

    if request.form["description"] != "":
        item.description = request.form["description"]

    item = item_dao.create_item(item)

    return jsonify(item.serialize)
Ejemplo n.º 6
0
def addItem(category_name):
    """Page to display for adding an item"""

    category = session.query(Category).filter_by(name=category_name).first()
    if request.method == 'POST':
        print request.form
        new_item = Item(category_id=category.id)
        new_item.description = request.form['description']
        new_item.name = firstThreeWords(new_item.description)
        new_item.author = request.form['author']
        new_item.user_id = login_session['user_id']

        session.add(new_item)
        session.commit()
        flash('"%s" item successfully added to "%s" category' %
              (new_item.name, category_name))
        return redirect(url_for('showCategory', category_name=category_name))
    else:
        return render_template('addItem.html', category_name=category_name)
Ejemplo n.º 7
0
def add_item():
    categories = session.query(Category).all()
    if request.method == 'POST':
        item = Item()
        if not request.form['title'] or not \
                request.form['description'] or \
                request.form['category_select'] == '0':
            return redirect('/catalog/item/new')
        else:
            item.name = request.form['title']
            item.description = request.form['description']
            item.category_id = request.form['category_select']
            item.user_id = login_session['username']
            session.add(item)
            session.commit()
            flash('Item {} added'.format(item.name))
        return redirect(url_for('show_catalog'))
    else:
        return render_template('add_item.html', categories=categories)
Ejemplo n.º 8
0
def addItem(category_name):
    """Page to display for adding an item"""

    category = session.query(Category).filter_by(name=category_name).first()
    if request.method == 'POST':
        print request.form
        new_item = Item(category_id=category.id)
        new_item.description = request.form['description']
        new_item.name = firstThreeWords(new_item.description)
        new_item.author = request.form['author']
        new_item.user_id = login_session['user_id']

        session.add(new_item)
        session.commit()
        flash('"%s" item successfully added to "%s" category' % (
                new_item.name, category_name))
        return redirect(url_for('showCategory', category_name=category_name))
    else:
        return render_template('addItem.html', category_name=category_name)
Ejemplo n.º 9
0
def newItem(category_id=''):
    if 'username' not in login_session:
        return redirect('/login')
    
    item = Item()
    item.category_id = category_id
    
    categories = session.query(Category).order_by(asc(Category.name))
   
    if request.method == 'POST':
        errors = []
        # check blank
        if request.form['name'] == "" or request.form['category_id'] == "" or request.form['description'] == "" or request.form['price'] == "":
            errors.append('Name, Category, Description, and Price are required.')
        # check duplicate
        if session.query(Item).filter(Item.name == request.form['name'], Item.category_id == request.form['category_id']).count()>0:
            errors.append('That item already exists in that category.')
        # too long - not pretty
        if len(request.form['name'])>36:
            errors.append('Please limit item name to 32 characters or less.')
 
        # set these values so we can show them what they tried to enter without passing as separate params
        item.name = request.form['name']
        item.category_id = request.form['category_id']
        item.description = request.form['description']
        item.price = request.form['price']
        item.picture = request.form['picture']
        item.user_id = login_session['user_id']
        
        if len(errors)>0:
            return render_template('newItem.html', category_id = category_id, categories = categories, item = item, errors = errors)
        else:
            session.add(item)
            session.commit()
            flash('Item %s has been added to the catalog.' % item.name)
            return redirect(url_for('showCategory', category_id = item.category.id))
    else:
        return render_template('newItem.html', category_id = category_id, categories = categories, item = item)
Ejemplo n.º 10
0
def ItemAdd():
    """
    Add Item - any user can add an item as long as they are logged in
    """
    if "username" not in login_session:
        loggedIn = False
        return redirect("/login")
    else:
        loggedIn = True

    categories = session.query(Category).all()
    if request.method == "POST":
        itemToAdd = Item()
        itemToAdd.item_name = request.form["item_name"]
        itemToAdd.item_description = request.form["item_description"]
        itemToAdd.category_id = request.form["category"]
        itemToAdd.user_id = login_session["user_id"]
        session.add(itemToAdd)
        session.commit()
        flash("Item Added")
        return redirect(url_for("HomePage"))
    else:
        return render_template("item_add.html", categories=categories, loggedIn=loggedIn)
Ejemplo n.º 11
0
def newCategoryItem(category_id):
    if not userIsLoggedIn():
        return redirect(url_for('showLogin'))

    #Find requested category
    category = session.query(Category).filter_by(id=category_id).one()

    #Handle form submit
    if request.method == 'POST':
        newItem = Item(name=request.form['name'], description=request.form[
            'description'], price=request.form['price'], category_id=category_id)

        #Assign creator id
        newItem.user_id = login_session['user_id']

        #Store new item
        session.add(newItem)
        session.commit()

        flash('New Menu %s Item Successfully Created' % (newItem.name))
        return redirect(url_for('showItems', category_id=category_id))
    else:
        return render_template('newitem.html', category_id=category_id)