def editItem(item_id): item = Item.getItemById(item_id) if not item: flash("Item () note found").form(item_id) return redirect(url_for(main.homepage)) next = get_redirect_target() form = ItemForm(next=next) form.category.choices = [(category.id, category.name) for category in Category.getAll()] if request.method == 'GET': form.name.data = item.name form.description.data = item.description form.category.data = item.category_id form.category.choices = [(category.id, category.name) for category in Category.getAll()] return render_template('item_form.html', form=form, name=item.name, description=item.description, category=item.category) if form.validate_on_submit(): item.name = form.data['name'] item.description = form.data['description'] cat_id = form.data['category'] item.category_id = form.data['category'] db.session.commit() return redirect_back('main.homepage')
def newitem(): name = None description = None category = None next = get_redirect_target() form = ItemForm(next=next) form.category.choices = \ [(category.id, category.name) for category in Category.getAll()] if form.validate_on_submit(): name = form.name.data description = form.description.data category_id = form.category.data item = Item(name=name, description=description, category_id=category_id) db.session.add(item) db.session.commit() flash("Item {} created".format(name)) return redirect_back('main.homepage') return render_template('item_form.html', form=form, name=name, description=description, category=category)
def home(category_id=None): categories = Category.getAll() category = None # Category is not specified. Show latest items if not category_id: items = Item.getItemsByDate() # Show items for specified category else: category_id = int(category_id) for category in categories: if category.id == category_id: break items = Item.getItemsByCategory(category_id=category_id) return render_template("home.html", categories=categories, items=items, category=category)