def category_view(name): with abort_if_raised(404, exc=db_exc.NoResultFound, msg="Category not found: {}".format(name)): category = Session.query(models.Category).filter_by(name=name).one() categories = Session.query(models.Category).all() return render_template("category.html", category=category, categories=categories)
def item_delete(name): with abort_if_raised(404, db_exc.NoResultFound): item = Session.query(models.Item).filter_by(name=name).one() if request.method == "GET": return render_template("item_delete.html", item=item) elif request.method == "POST": Session.delete(item) Session.commit() return redirect(url_for("index"), 303)
def item_view(category_name, name): with abort_if_raised(404, exc=db_exc.NoResultFound, msg="Item not found: {}".format(name)): item = Session.query(models.Item)\ .filter(models.Item.name == name and models.Item.category == category_name)\ .one() return render_template("item.html", item=item)
def db_session(database): from catalog.database import Session try: session = Session() yield session finally: session.rollback() session.close()
def item_add(): if request.method == "GET": categories = Session.query(models.Category).all() return render_template("item_add.html", categories=categories) elif request.method == "POST": form = request.form with abort_if_raised(404, db_exc.NoResultFound): category = Session.query( models.Category).filter_by(name=form["category"]).one() new_item = models.Item(name=form["name"], description=form["description"], category=category) Session.add(new_item) Session.commit() return redirect(url_for("index"), 303)
def item_edit(name): with abort_if_raised(404, db_exc.NoResultFound): item = Session.query(models.Item).filter_by(name=name).one() if request.method == "GET": categories = Session.query(models.Category).all() return render_template("item_edit.html", item=item, categories=categories) elif request.method == "POST": form = request.form item.name = form["name"] item.description = form["description"] with abort_if_raised(404, db_exc.NoResultFound): category = Session.query( models.Category).filter_by(name=form["category"]).one() item.category = category Session.add(item) Session.commit() return redirect(url_for("index"), 303)
def index(): categories = Session.query(models.Category).all() items = Session.query(models.Item).order_by( models.Item.timestamp.desc()).limit(10) return render_template("index.html", categories=categories, items=items)
def seeded_session(database): from catalog.database import Session try: session = Session() session.add(models.User(username='******', email='*****@*****.**')) session.add( models.User(username='******', email='*****@*****.**')) session.add( models.Category(name='Football', description='The real one from Europe.', owner_id=2)) session.add( models.Item(name='Football ball', description='Rather hard to play without one init?', category_id=1)) session.add( models.Item(name='Arsenal Jersey', description='You still there Arsene?', category_id=1)) session.add( models.Item(name='Drama Classes', description= 'Falling on the grass is an art. It takes practice to ' 'master it.', category_id=1)) session.add( models.Category(name='Boxing', description='If you' 're up for a good fight.', owner_id=2)) session.add( models.Item(name='Gloves', description='We' 're fighters here, not brute.', category_id=2)) session.add( models.Item(name='Jumping Rope', description='Just like Rocky.', category_id=2)) session.commit() yield session except: session.rollback() raise finally: session.close()