def attach_picture(table, item_id, location): """ A helper function used in populator.py to upload picture to the db Args: table: The category which the picture belongs to item_id: The category's id number which the picture should be uploaded to location: local directory of where the picture is found Returns: None """ try: item = session.query(table).filter_by(id=item_id).one() with store_context(fs_store): with open(location, "rb") as f: item.picture.from_file(f) session.commit() except Exception: session.rollback() raise
def attach_picture(table, item_id, location): """ A helper function used in populator.py to upload picture to the db Args: table: The category which the picture belongs to item_id: The category's id number which the picture should be uploaded to location: local directory of where the picture is found Returns: None """ try: item = session.query(table).filter_by(id=item_id).one() with store_context(fs_store): with open(location, 'rb') as f: item.picture.from_file(f) session.commit() except Exception: session.rollback() raise
def delete(dbtype, item_id): """Delete either the item or category in the database""" # check login status if 'email' not in login_session: flash('Sorry, the page you tried to access is for members only. ' 'Please sign in first.') abort(401) # query the item user wants to delete delete_item = (session.query(eval(dbtype[0].upper()+dbtype[1:])) .filter_by(id=item_id).one()) # make sure user is authorized to delete this item if login_session['user_id'] != delete_item.user_id: flash('You are not authorized to modify items you did not create. ' 'Please create your own item in order to modify it.') return redirect(url_for(dbtype)) if request.method == 'POST': try: session.delete(delete_item) session.commit() # handling legacy error when delete invovled cascade-delete except IntegrityError as detail: print 'Handling run-time error: ', detail session.rollback() flash('Delete Operation Failed') return redirect(url_for('home')) if dbtype.endswith('Lot'): flash('%s Lot Deleted' % dbtype[:-3].capitalize()) return redirect(url_for(dbtype[:-3])) else: flash('%s Deleted' % dbtype.capitalize()) return redirect(url_for(dbtype)) else: pass
def delete(dbtype, item_id): """Delete either the item or category in the database""" # check login status if 'email' not in login_session: flash('Sorry, the page you tried to access is for members only. ' 'Please sign in first.') abort(401) # query the item user wants to delete delete_item = (session.query(eval(dbtype[0].upper() + dbtype[1:])).filter_by(id=item_id).one()) # make sure user is authorized to delete this item if login_session['user_id'] != delete_item.user_id: flash('You are not authorized to modify items you did not create. ' 'Please create your own item in order to modify it.') return redirect(url_for(dbtype)) if request.method == 'POST': try: session.delete(delete_item) session.commit() # handling legacy error when delete invovled cascade-delete except IntegrityError as detail: print 'Handling run-time error: ', detail session.rollback() flash('Delete Operation Failed') return redirect(url_for('home')) if dbtype.endswith('Lot'): flash('%s Lot Deleted' % dbtype[:-3].capitalize()) return redirect(url_for(dbtype[:-3])) else: flash('%s Deleted' % dbtype.capitalize()) return redirect(url_for(dbtype)) else: pass