def try_add(): """ This function receives data from the create item page from ajax call Attempts add that item to database """ # Check that user is logged in if 'username' not in login_session: ret = {'html': "Not logged in", 'status': "ERROR"} return json.dumps(ret) # Check that values were posted if 'name' not in request.form or 'desc' not in request.form: ret = {'html': "No values given", 'status': "ERROR"} return json.dumps(ret) # needed variables t_name = request.form["name"] t_desc = request.form["desc"] # check if item exists already # does not make sense to have more than 1 item with same name if session.query(Item).filter(Item.item_name == t_name).count() != 0: ret_str = "Sorry. " ret_str += t_name ret_str += " is already in the database" ret = {'html': ret_str, 'status': "ERROR"} return json.dumps(ret) # get one and only one category id t_cat = return_one_category(request.form["category"]) if t_cat == "ERROR": ret = {'html': "Error getting category id", 'status': "ERROR"} return json.dumps(ret) # get one and only one user id t_user = return_one_user(login_session['email']) if t_user == "ERROR": ret = {'html': "Error getting user id", 'status': "ERROR"} return json.dumps(ret) # add to database t_itm = Item(item_name=t_name, description=t_desc, cat_id=t_cat, creator=t_user) session.add(t_itm) session.commit() # Return ret = {'html': "Item successfully added!", 'status': "SUCCESS"} return json.dumps(ret)
def try_edit(): """ Try to edit an item Called from AJAX """ # Check that user is logged in if 'username' not in login_session: ret = {'html': "Not logged in", 'status': "ERROR"} return json.dumps(ret) # make sure data was posted if ('name' not in request.form or 'desc' not in request.form or 'original' not in request.form or 'category' not in request.form): ret = {'html': "No values given", 'status': "ERROR"} return json.dumps(ret) # get data original_name = request.form["original"] new_name = request.form["name"] new_desc = request.form["desc"] new_cat = return_one_category(request.form["category"]) # update data item = session.query(Item).filter(Item.item_name == original_name).first() item.item_name = new_name item.description = new_desc item.cat_id = new_cat session.commit() # return to ajax call ret = {'status': "SUCCESS", 'html': "Successfully updated item"} return json.dumps(ret)
session.add(user_me) # commit to get access to ids session.commit() # Add 2 brushes and a pen from me # get id corresponding to email my_id = session.query(User).filter(User.email == "*****@*****.**") my_id = my_id.one().id # get category ids brush_id = return_one_category("Brushes") pen_id = return_one_category("Pens") itm_paintbrush = Item(item_name="paintbrush", description="for paint", cat_id=brush_id, creator=my_id) itm_dippen = Item(item_name="dippen", description="for oil", cat_id=pen_id, creator=my_id) itm_oilbrush = Item(item_name="oilbrush", description="for oil", cat_id=brush_id, creator=my_id) session.add_all([ itm_paintbrush, itm_dippen, itm_oilbrush]) session.commit()