def add_inventory(user_id, thing_id): setup_session() with session_scope() as session: inv = Inventory(inventory_user_id=user_id, inventory_thing_id=thing_id) session.add(inv) session.commit() inv_dict = inv.get_class_table_vals() return jsonify(inv_dict)
def create_thing(thing_name, thing_description): setup_session() with session_scope() as session: thng = Thing(thing_name=thing_name) thng.description = Description(description=thing_description) session.add(thng) session.commit() thng_dict = thng.get_class_table_vals() return jsonify(thng_dict)
def create_user(name): setup_session() print(f'{name}') with session_scope() as session: usr = User.get_unique(session=session, user_name=name) session.commit() usr_dict = usr.get_class_table_vals() return jsonify(usr_dict)
def app(): print('app') """Create and configure a new app instance for each test.""" # create a temporary file to isolate the database for each test db_fd, db_path = tempfile.mkstemp() setup_session(f'sqlite:///{db_path}') # create the app with common test config yield flaskapp # close and remove the temporary database os.close(db_fd) os.unlink(db_path)
def add_group_thing(group_id, thing_id): setup_session() with session_scope() as session: grp = Group.get_unique(session, group_id=group_id) thng = Thing.get_unique(session, thing_id=thing_id) grp_thng = GroupThing() grp_thng.thing = thng grp_thng.group = grp session.add(grp_thng) session.commit() grp_thing_dict = grp_thng.get_class_table_vals() return jsonify(grp_thing_dict)
def get_inventory(user_id): setup_session() #TODO: Can we use an association table relationship to make this query easier (inventory is association table)? with session_scope() as session: inventory = session.query(Thing.thing_name, Description.description).\ join(Inventory, Inventory.inventory_thing_id == Thing.thing_id).\ join(User, User.user_id == Inventory.inventory_user_id).\ join(Description, Description.description_id == Thing.thing_description_id).\ filter(User.user_id == user_id).all() inventory = [{'thing': x, 'description': y} for x, y in inventory] return jsonify(inventory)
def add_group_user(group_id, user_id): setup_session() with session_scope() as session: grp = Group.get_unique(session, group_id=group_id) usr = User.get_unique(session, user_id=user_id) grp_usr = GroupUser() grp_usr.user_owner = False grp_usr.user = usr grp_usr.group = grp session.add(grp_usr) session.commit() grp_usr_dict = grp_usr.get_class_table_vals() return jsonify(grp_usr_dict)
def add_group(group_name, user_id): setup_session() with session_scope() as session: grp = Group(group_name=group_name) if user_id == -1: # Allow to pass -1 if do not want to setup as a user owned group grp.user_owned_group = False else: grp.user_owned_group = True usr = User.get_unique(session=session, user_id=user_id) grp_usr = GroupUser() grp_usr.user = usr grp_usr.group = grp session.add(grp) session.add(grp_usr) session.commit() grp_dict = grp.get_class_table_vals() return jsonify(grp_dict)