def grocery():
    form = NewItemForm()
    if form.validate_on_submit():
        shopping_list.create(name=request.form['name'],
                             quantity=request.form['quantity'])
    return render_template('grocery.html',
                           form=form,
                           shopping_list=shopping_list.getall())
def grocery():
    form = NewItemForm()  # form we made
    if form.validate_on_submit():  # runs for POST requests from new item form
        shopping_list.append({
            'name': request.form['name'],
            'quantity': request.form['quantity']
        })
    return render_template('grocery.html',
                           form=form,
                           shopping_list=shopping_list)
Exemplo n.º 3
0
def item_add():
    form = NewItemForm()

    if form.validate_on_submit():
        item = Item(description=form.description.data,
                    sku=form.sku.data,
                    item_number=form.item_number.data,
                    quantity=form.quantity.data,
                    cost=form.cost.data,
                    price=form.price.data)
        db.session.add(item)
        db.session.commit()
        flash('Item added')
        return redirect(url_for('item_add'))
    return render_template('item_add.html', title='Add new Item', form=form)
Exemplo n.º 4
0
 def save_category_item(self, form: NewItemForm) -> CategoryItem:
     session = self.dbsetup.create_session()
     item = form.build_item()
     session.add(item)
     session.commit()
     session.close()
     return item
Exemplo n.º 5
0
    def post(self):
        categories = self.catalog_service.get_categories()
        form = NewItemForm(request.form)
        form.set_categories(categories)
        category_lookup = dict(form.category.choices)

        if form.validate_on_submit():
            try:
                self.catalog_service.save_category_item(form)
                selected_category = category_lookup[form.category.data]
                url = url_for('category_view', category_name=selected_category)
                return redirect(url)
            except Exception as e:
                return render_template('500.html', error=str(e))

        # Invalid form so redirect to the new item page so the user can
        # try again
        return redirect(url_for('new_catalog_item_view'))
Exemplo n.º 6
0
def list(id):
    if not current_user.is_authenticated:
        if request.method == 'POST' and request.headers['api-token'] == API_TOKEN:
            name = request.values['name']
            list = List.objects.get(id=id)
            i = Item(name=name, list=list)
            i.save()
            return i.name
        else:
            return redirect(url_for('login'))
    list = List.objects.get(id=id)
    form = NewItemForm()
    if form.validate_on_submit():
        name = form.name.data
        i = Item(name=name, list=list)
        i.save()
        return redirect(url_for('list', id=id))
    items = Item.objects(list=list)
    return render_template("list.html", form=form, list=list, items=items)
Exemplo n.º 7
0
def edit_item(id, item_id):
    """Edits an item on a given ShoppingList to the string passed."""
    form = NewItemForm()

    if form.validate_on_submit():
        ed_item = Item.query.filter_by(list_id=id, item_id=item_id).first()
        if ed_item is not None:
            if form.name.data:
                ed_item.name = form.name.data
                db.session.commit()
        else:
            response = jsonify({'ERR': 'Item does not exist.'})
            response.status_code = 404
            return response
        response = jsonify({'MSG': 'Edited item.'})
        response.status_code = 201
    else:
        response = jsonify({'MSG': form.errors})
        response.status_code = 422
    return response
Exemplo n.º 8
0
def add_item(id):
    """Adds an item to a given ShoppingList."""
    form = NewItemForm()
    if form.validate_on_submit():
        new_item = Item.query.filter_by(name=(form.name.data).lower(),
                                        list_id=id).first()
        if not new_item:
            new_item = Item(form.name.data, id)
            if new_item:
                db.session.add(new_item)
                db.session.commit()
                response = jsonify({'MSG': 'Item added to list'})
                response.status_code = 201
            else:
                response = jsonify(
                    {'ERR': 'Oops, something went wrong. Try again!'})
                response.status_code = 400
        else:
            response = jsonify({'ERR': 'Item already exists!'})
            response.status_code = 409
    else:
        response = jsonify({'ERR': form.errors})
        response.status_code = 422
    return response
Exemplo n.º 9
0
 def get(self):
     categories = self.catalog_service.get_categories()
     form = NewItemForm()
     form.set_categories(categories)
     return render_template('add_item.html', form=form)