def add_item(): with makeSession() as dbSession: label = request.form['label'] amount = float(request.form['amount']) unit = request.form['unit'] expdate = datetime.strptime(request.form['expdate'], "%m/%d/%Y") picture_id = int(request.form['picture_id']) new_item = Item(label, amount, unit, expdate, picture_id) dbSession.add(new_item) dbSession.commit() return json.dumps({ 'status' : 'success' })
def item_endpoint(id): if request.method == 'GET': with makeSession() as dbSession: item = dbSession.query(Item).get(id) dbSession.commit() return item elif request.method == 'DELETE': with makeSession() as dbSession: item = dbSession.query(Item).get(id) dbSession.delete(item) dbSession.commit() return json.dumps({ 'status' : 'success' }) elif request.method == 'POST': with makeSession() as dbSession: item = dbSession.query(Item).get(id) amount = float(request.form['amount']) unit = request.form['unit'] item.amount = amount item.unit = unit dbSession.commit() return json.dumps({ 'status' : 'success' })
def list_items(): with makeSession() as dbSession: items = dbSession.query(Item).all() ret = [i.as_dict() for i in items] return json.dumps(ret)