def get_sister_items(params, _id, conn, logger, config, session): try: # NOTE: find user_id user_id = params.get("user_id", session.get("user_id", None)) # CHECK: was a user_id found? if user_id is None: return rpc.make_error_resp(0, "PROBLEM: There was no `user_id` argument provided and no user_id could be " "located in the session.\n" "SUGGESTION: Either either try again with a `user_id` argument, call " "login() then try again, or use put_sess() to manually add your user_id to " "your session then try again.", _id) with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # NOTE: load the target location target = db.load_item(conn, params["item_uuid"], _id) # NOTE: load the target's items items = [db.load_item(conn, uuid, _id) for uuid in target.sister_items] return rpc.make_success_resp([i.one_hot_jsonify() for i in items], _id) except WrappedErrorResponse as e: file_logger.log_error({ "method": "get_sister_items" + str(e.methods), "params": params, "error": str(e.exception) }) return e.response_obj except Exception as e: file_logger.log_error({ "method": "get_sister_items", "params": params, "error": str(e), }) return rpc.make_error_resp(const.INTERNAL_ERROR_CODE, const.INTERNAL_ERROR, _id)
def get_user_items(params, _id, conn, logger, config, session): try: # NOTE: find user_id user_id = params.get("user_id", session.get("user_id", None)) # CHECK: was a user_id found? if user_id is None: return rpc.make_error_resp(0, "PROBLEM: There was no `user_id` argument provided and no user_id could be " "located in the session.\n" "SUGGESTION: Either either try again with a `user_id` argument, call " "login() then try again, or use put_sess() to manually add your user_id to " "your session then try again.", _id) with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # NOTE: load the target user if "uuid" in params: target = db.load_user_w_uuid(conn, params["uuid"], _id) else: target = db.load_user_w_user_id(conn, user_id, _id) # NOTE: load the target's items items = [db.load_item(conn, uuid, _id) for uuid in (target.item_uuids | target.incoming_item_uuids | target.outgoing_item_uuids)] # NOTE: form the predicate function for the filter stages = [target.incoming_item_uuids, target.item_uuids, target.outgoing_item_uuids] p = lambda item: ( ("status_filter" not in params or item.status == params.get("status_filter")) and ("ownership_filter" not in params or (item.uuid in target.incoming_item_uuids) == params["ownership_filter"]) and ("location_uuid_filter" not in params or item.location_uuids[-1] == params["location_uuid_filter"]) and ("stage_filter" not in params or item.uuid in stages[params["stage_filter"]])) return rpc.make_success_resp([i.one_hot_jsonify() for i in items if p(i)], _id) except WrappedErrorResponse as e: file_logger.log_error({ "method": "get_user_items" + str(e.methods), "params": params, "error": str(e.exception) }) return e.response_obj except Exception as e: file_logger.log_error({ "method": "get_user_items", "params": params, "error": str(e), }) return rpc.make_error_resp(const.INTERNAL_ERROR_CODE, const.INTERNAL_ERROR, _id)
def redirect_transfer(params, _id, conn, logger, config, session): try: # NOTE: find user_id user_id = params.get("user_id", session.get("user_id", None)) # CHECK: was a user_id found? if user_id is None: return rpc.make_error_resp( 0, "PROBLEM: There was no `user_id` argument provided and no user_id could be " "located in the session.\n" "SUGGESTION: Either either try again with a `user_id` argument, call " "login() then try again, or use put_sess() to manually add your user_id to " "your session then try again.", _id) with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # NOTE: load item item = db.load_item(conn, params["item_uuid"], _id) # CHECK: is the item actually in transit? if item.status != lconst.TRANSIT: return rpc.make_error_resp( 0, "PROBLEM: The item designated by the `item_uuid` argument is not in transit," " and therefore, there is no transfer to redirect.\n" "SUGGESTION: Try calling this method with an `item_uuid` that corresponds to" " an item that is in transit.", _id) # NOTE: load the caller's user caller = db.load_user_w_user_id(conn, user_id, _id) # CHECK: is the caller the owner of this item? if item.uuid not in caller.outgoing_item_uuids | caller.item_uuids: return rpc.make_error_resp( 0, "PROBLEM: You are not the owner of this item, and therefore, don't have the " "ability to redirect this transfer\n" "SUGGESTION: You may be logged in to someone else's account without " "realizing it. Try calling login() then trying again.", _id) # NOTE: load the new receiver's user new_receiver = db.load_user_w_uuid( conn, params["new_destination_user_uuid"], _id) # NOTE: load the new destination location new_destination = db.load_location( conn, params["new_destination_location_uuid"], _id) # CHECK: is the destination attached to the receiver? if new_destination.uuid not in new_receiver.location_uuids: return rpc.make_error_resp( 0, "PROBLEM: The designated new receiver has no presence at the designated " "new destination which is a contradiction.\n" "SUGGESTION: Try again with either a different new receiver who has a " "presence at the destination, or a different new destination where the " "receiver has a presence.", _id) # NOTE: load the old_destination old_destination = db.load_location(conn, item.location_uuids[-1], _id) # NOTE: combine if necessary if new_destination.uuid == old_destination.uuid: new_destination = old_destination # NOTE: remove the item from the old_destination's incoming_item_uuids list old_destination.incoming_item_uuids.discard(item.uuid) # NOTE: add the item to the new_destination's incoming_item_uuids new_destination.incoming_item_uuids.add(item.uuid) # NOTE: load the sender's user sender = db.load_user_w_uuid(conn, item.user_uuids[-2], _id) # NOTE: load the old_receiver's user old_receiver = db.load_user_w_uuid(conn, item.user_uuids[-1], _id) # NOTE: combine if necessary if (sender.uuid == old_receiver.uuid and old_receiver.uuid == caller.uuid): old_receiver = sender caller = old_receiver elif old_receiver.uuid == caller.uuid: old_receiver = caller elif caller.uuid == sender.uuid: caller = sender elif sender.uuid == old_receiver.uuid: sender = old_receiver # NOTE: remove the item from the old receiver's incoming_item_uuids old_receiver.incoming_item_uuids.discard(item.uuid) # NOTE: is this an external redirect? if sender.uuid != new_receiver.uuid: # NOTE: add the item to the new receiver's incoming_item_uuids new_receiver.incoming_item_uuids.append(item.uuid) # NOTE: remove the item from the sender's item_uuids list sender.item_uuids.discard(item.uuid) # NOTE: add the item to the sender's outgoing_item_uuids list sender.outgoing_item_uuids.add(item.uuid) # NOTE: remove the old receiver from the item's user_uuids list item.user_uuids = item.user_uuids[:-1] # NOTE: remove the old destination from the item's location_uuids item.location_uuids = item.location_uuids[:-1] # NOTE: add the new receiver to the item's user_uuids list # OPT: combine with the above step item.user_uuids.append(new_receiver.uuid) # NOTE: add the new destination to the item's location_uuids list # OPT: combine with the above step item.location_uuids.append(new_destination.uuid) # NOTE: save everything db.save_existing_item(conn, item, _id) db.save_existing_user(conn, new_receiver, _id) db.save_existing_user(conn, old_receiver, _id) return rpc.make_success_resp(item.one_hot_jsonify(), _id) except WrappedErrorResponse as e: file_logger.log_error({ "method": "redirect_transfer" + str(e.methods), "params": params, "error": str(e.exception) }) return e.response_obj except Exception as e: file_logger.log_error({ "method": "redirect_transfer", "params": params, "error": str(e) }) return rpc.make_error_resp(const.INTERNAL_ERROR_CODE, const.INTERNAL_ERROR, _id)
def begin_transfer(params, _id, conn, logger, config, session): try: # NOTE: find user_id user_id = params.get("user_id", session.get("user_id", None)) # CHECK: was a user_id found? if user_id is None: return rpc.make_error_resp( 0, "PROBLEM: There was no `user_id` argument provided and no user_id could be " "located in the session.\n" "SUGGESTION: Either either try again with a `user_id` argument, call " "login() then try again, or use put_sess() to manually add your user_id to " "your session then try again.", _id) with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # NOTE: load item item = db.load_item(conn, params["item_uuid"], _id) # CHECK: is the item not stationary? if item.status != lconst.STATIONARY: return rpc.make_error_resp( 0, "PROBLEM: The item designated by the `item_uuid` argument is not in " "stationary, and therefore, it cannot be transferred.\n" "SUGGESTION: Try calling this method with an `item_uuid` that corresponds to" " an item that is stationary, call redirect_transfer() instead, or call " "rescind_transfer() on this item then try again.", _id) # NOTE: load the caller's user caller = db.load_user_w_user_id(conn, user_id, _id) # CHECK: does the caller own the item? if item.uuid not in caller.item_uuids: return rpc.make_error_resp( 0, "PROBLEM: The item begin transferred is not owned by this account.\n" "SUGGESTION: You may be logged in to someone else's account without " "realizing it. Try calling login() then trying again.", _id) # NOTE: load the receiver's user receiver = db.load_user_w_uuid(conn, params["destination_user_uuid"], _id) # NOTE: load the destination destination = db.load_location(conn, params["destination_location_uuid"], _id) # CHECK: is the destination attached to the receiver? if destination.uuid not in receiver.location_uuids: return rpc.make_error_resp( 0, "PROBLEM: The designated receiver has no presence at the designated " "destination which is a contradiction.\n" "SUGGESTION: Try again with either a different receiver who has a presence " "at the destination, or a different destination where the receiver has a " "presence.", _id) # NOTE: load the origin origin = db.load_location(conn, item.location_uuids[-1], _id) # NOTE: combine if necessary if origin.uuid == destination.uuid: destination = origin # NOTE: remove the item from the origin's item_uuids list origin.item_uuids.discard(item.uuid) # NOTE: add the item to the origin's outgoing_item_uuids list origin.outgoing_item_uuids.add(item.uuid) # NOTE: add the item to the destination's incoming_item_uuids list destination.incoming_item_uuids.add(item.uuid) # NOTE: conditionally move item around in the user if receiver.uuid != caller.uuid: # NOTE: remove the item from the caller's item_uuids list caller.item_uuids.discard(item.uuid) # NOTE: add the item to the caller's outgoing_item_uuids list caller.outgoing_item_uuids.add(item.uuid) # NOTE: add the item to the receiver's incoming_item_uuids list receiver.incoming_item_uuids.add(item.uuid) # NOTE: set the item to transit item.status = lconst.TRANSIT # NOTE: add the destination to the item's location_uuids list item.location_uuids.append(destination.uuid) # NOTE: add the receiver to the item's user_uuids list item.user_uuids.append(receiver.uuid) # NOTE: save everything db.save_existing_item(conn, item, _id) db.save_existing_user(conn, caller, _id) db.save_existing_user(conn, receiver, _id) db.save_existing_location(conn, origin, _id) db.save_existing_location(conn, destination, _id) return rpc.make_success_resp(item.one_hot_jsonify(), _id) except WrappedErrorResponse as e: file_logger.log_error({ "method": "begin_transfer" + str(e.methods), "params": params, "error": str(e.exception), }) return e.response_obj except Exception as e: file_logger.log_error({ "method": "begin_transfer", "params": params, "error": str(e), }) return rpc.make_error_resp(const.INTERNAL_ERROR_CODE, const.INTERNAL_ERROR, _id)
def rescind_transfer(params, _id, conn, logger, config, session): try: # NOTE: find user_id user_id = params.get("user_id", session.get("user_id", None)) # CHECK: was a user_id found? if user_id is None: return rpc.make_error_resp( 0, "PROBLEM: There was no `user_id` argument provided and no user_id could be " "located in the session.\n" "SUGGESTION: Either either try again with a `user_id` argument, call " "login() then try again, or use put_sess() to manually add your user_id to " "your session then try again.", _id) with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # NOTE: load item item = db.load_item(conn, params["item_uuid"], _id) # CHECK: is the item actually in transit? if item.status != lconst.TRANSIT: return rpc.make_error_resp( 0, "PROBLEM: The item designated by the `item_uuid` argument is not in transit," " and therefore, there is no transfer to rescind.\n" "SUGGESTION: Try calling this method with an `item_uuid` that corresponds to" " an item that is in transit.", _id) # NOTE: load the caller's user caller = db.load_user_w_user_id(conn, user_id, _id) # CHECK: does the caller own the item? if item.uuid not in caller.outgoing_item_uuids | caller.item_uuids: return rpc.make_error_resp( 0, "PROBLEM: You are not the owner of this item, and therefore, can't rescind " "it's transfer\n" "SUGGESTION: You may be logged in to someone else's account without " "realizing it. Try calling login() then trying again.", _id) # NOTE: load the origin origin = db.load_location(conn, item.location_uuids[-2], _id) # NOTE: load the destination destination = db.load_location(conn, item.location_uuids[-1], _id) # NOTE: combine if necessary if origin.uuid == destination.uuid: destination = origin # NOTE: load the receiver's user receiver = db.load_user_w_uuid(conn, item.user_uuids[-1], _id) # NOTE: combine if necessary if caller.uuid == receiver.uuid: receiver = caller # NOTE: remove the item from the destination's incoming_item_uuids destination.incoming_item_uuids.discard(item.uuid) # NOTE: remove the item from the origin's outgoing_item_uuids list origin.outgoing_item_uuids.discard(item.uuid) # NOTE: add the item to the origin's item_uuids list origin.item_uuids.add(item.uuid) # NOTE: set the item to stationary item.status = lconst.STATIONARY # NOTE: remove the item from the user's outgoing_item_uuids list caller.outgoing_item_uuids.discard(item.uuid) # NOTE: add the item to the user's item_uuids list caller.item_uuids.add(item.uuid) # NOTE: remove the item from the receiever's incoming_item_uuids receiver.incoming_item_uuids.discard(item.uuid) # NOTE: remove the receiver from the item's user_uuids list item.user_uuids = item.user_uuids[:-1] # NOTE: remove the destination from the item's location_uuids list item.location_uuids = item.location_uuids[:-1] # NOTE: save everything db.save_existing_item(conn, item, _id) db.save_existing_user(conn, caller, _id) db.save_existing_user(conn, receiver, _id) db.save_existing_location(conn, origin, _id) db.save_existing_location(conn, destination, _id) return rpc.make_success_resp(item.one_hot_jsonify(), _id) except WrappedErrorResponse as e: file_logger.log_error({ "method": "rescind_transfer" + str(e.methods), "params": params, "error": str(e.exception) }) return e.response_obj except Exception as e: file_logger.log_error({ "method": "rescind_transfer", "params": params, "error": str(e) }) return rpc.make_error_resp(const.INTERNAL_ERROR_CODE, const.INTERNAL_ERROR, _id)
def split_item(params, _id, conn, logger, config, session): try: # NOTE: find user_id user_id = params.get("user_id", session.get("user_id", None)) # CHECK: was a user_id found? if user_id is None: return rpc.make_error_resp( 0, "PROBLEM: There was no `user_id` argument provided and no " "user_id could be located in the session.\n" "SUGGESTION: Either either try again with a `user_id` " "argument, call login() then try again, or use put_sess() to " "manually add your user_id to your session then try again.", _id ) with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # NOTE: load the caller's user caller = db.load_user_w_user_id(conn, user_id, _id) # NOTE: load the item item = db.load_item(conn, params["item_uuid"], _id) # CHECK: does the caller own this item? if item.uuid not in caller.item_uuids | caller.outgoing_item_uuids: return rpc.make_error_resp( 0, "PROBLEM: You don't own the item you're trying to edit.\n" "SUGGESTION: Try editing an edit you own or ensure you're " "not logged in to someone else's account.", _id ) # CHECK: is the item stationary? if item.status != lconst.STATIONARY: return rpc.make_error_resp( 0, "PROBLEM: You can't edit a non-stationary item.\n" "SUGGESTION: Rescind the transfer then try again.", _id ) # NOTE: load the item's location location = db.load_location(conn, item.location_uuids[-1], _id) # NOTE: make n new items with the parent item's children = {Item( _type=item.type, location_uuids=item.location_uuids, user_uuids=item.user_uuids, status=item.status, details=item.details ) for _ in range(params.get("ways", 2))} # NOTE: generate the set of sister item uuids sister_uuids =\ {child.uuid for child in children} | set(item.sister_items) for child in children: # NOTE: add the sisters to the child child.sister_items = list(sister_uuids - {child.uuid}) # NOTE: save the new child db.save_new_item(conn, child, _id) # NOTE: add the child to the location's item_uuids list location.item_uuids.add(child.uuid) # NOTE: add the child to the caller's item_uuids list caller.item_uuids.add(child.uuid) # NOTE: update the item's status to `split` item.status = lconst.SPLIT # NOTE: remove the item from the location's item_uuids list location.item_uuids.discard(item.uuid) # NOTE: remove the item from the caller's item_uuids list caller.item_uuids.discard(item.uuid) # NOTE save everything db.save_existing_item(conn, item, _id) db.save_existing_location(conn, location, _id) db.save_existing_user(conn, caller, _id) children = [child.one_hot_jsonify() for child in children] return rpc.make_success_resp(list(children), _id) except WrappedErrorResponse as e: file_logger.log_error({ "method": "split_item" + str(e.methods), "params": params, "error": str(e.exception) }) return e.response_obj except Exception as e: file_logger.log_error({ "method": "split_item", "params": params, "error": str(e) }) return rpc.make_error_resp( const.INTERNAL_ERROR_CODE, const.INTERNAL_ERROR, _id )
def change_item_detail(params, _id, conn, logger, config, session): try: # NOTE: find user_id user_id = params.get("user_id", session.get("user_id", None)) # CHECK: was a user_id found? if user_id is None: return rpc.make_error_resp( 0, "PROBLEM: There was no `user_id` argument provided and no " "user_id could be located in the session.\n" "SUGGESTION: Either either try again with a `user_id` " "argument, call login() then try again, or use put_sess() to " "manually add your user_id to your session then try again.", _id ) with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # NOTE: load the caller's user caller = db.load_user_w_user_id(conn, user_id, _id) # NOTE: load the item item = db.load_item(conn, params["item_uuid"], _id) # CHECK: does the caller own this item? if item.uuid not in caller.item_uuids | caller.outgoing_item_uuids: return rpc.make_error_resp( 0, "PROBLEM: You don't own the item you're trying to edit.\n" "SUGGESTION: Try editing an edit you own or ensure you're " "not logged in to someone else's account.", _id ) # CHECK: is the item stationary? if item.status != lconst.STATIONARY: return rpc.make_error_resp( 0, "PROBLEM: You can't edit a non-stationary item.\n" "SUGGESTION: Rescind the transfer then try again.", _id ) # NOTE: update the item's details field item.details[params["key"]] = params["value"] # NOTE: save the item db.save_existing_item(conn, item, _id) return rpc.make_success_resp(item.one_hot_jsonify(), _id) except WrappedErrorResponse as e: file_logger.log_error({ "method": "change_item_detail" + str(e.methods), "params": params, "error": str(e.exception) }) return e.response_obj except Exception as e: file_logger.log_error({ "method": "change_item_detail", "params": params, "error": str(e) }) return rpc.make_error_resp( const.INTERNAL_ERROR_CODE, const.INTERNAL_ERROR, _id )
def get_user_item_uuids(params, _id, conn, logger, config, session): try: # NOTE: find user_id user_id = params.get("user_id", session.get("user_id", None)) # CHECK: was a user_id found? if user_id is None: return rpc.make_error_resp( 0, "PROBLEM: There was no `user_id` argument provided and no user_id could be " "located in the session.\n" "SUGGESTION: Either either try again with a `user_id` argument, call " "login() then try again, or use put_sess() to manually add your user_id to " "your session then try again.", _id) if "username" in params: with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # CHECK: is there a user associated with the given username? if not __is_username_taken(_id, conn, params["username"]): return rpc.make_error_resp( 0, "PROBLEM: No user in the system exists with the designated username.\n" "SUGGESTION: Try a different username.", _id) # NOTE: load the target user target = db.load_user_w_uuid( conn, conn.execute( """SELECT uuid FROM users WHERE username = ?""", (params["username"], )).fetchone()[0], _id) # NOTE: load the target's items items = [ db.load_item(conn, uuid, _id) for uuid in target.item_uuids | target.incoming_item_uuids | target.outgoing_item_uuids ] else: with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # NOTE: load the caller's user caller = db.load_user_w_user_id(conn, user_id, _id) # NOTE: load the caller's items items = [ db.load_item(conn, uuid, _id) for uuid in caller.item_uuids | caller.incoming_item_uuids | caller.outgoing_item_uuids ] return rpc.make_success_resp([i.uuid for i in items], _id) except WrappedErrorResponse as e: file_logger.log_error({ "method": "get_user_item_uuids" + str(e.methods), "params": params, "error": str(e.exception) }) return e.response_obj except Exception as e: file_logger.log_error({ "method": "get_user_item_uuids", "params": params, "error": str(e), }) return rpc.make_error_resp(const.INTERNAL_ERROR_CODE, const.INTERNAL_ERROR, _id)
def drop_item(params, _id, conn, logger, config, session): try: # NOTE: find user_id user_id = params.get("user_id", session.get("user_id", None)) # CHECK: was a user_id found? if user_id is None: return rpc.make_error_resp( 0, "PROBLEM: There was no `user_id` argument provided and no user_id could be " "located in the session.\n" "SUGGESTION: Either either try again with a `user_id` argument, call " "login() then try again, or use put_sess() to manually add your user_id to " "your session then try again.", _id) with conn: # NOTE: get a lock on the database conn.execute("BEGIN EXCLUSIVE") # NOTE: load the user user = db.load_user_w_user_id(conn, user_id, _id) # CHECK: is the item attached to the user? if params[ "item_uuid"] not in user.item_uuids | user.outgoing_item_uuids: return rpc.make_error_resp( 0, "PROBLEM: The designated item uuid doesn't correspond to an item " "owned by the user.\n" "SUGGESTIONS: Try again with a valid item uuid.", _id) # NOTE: load the item item = db.load_item(conn, params["item_uuid"], _id) # CHECK: is the item stationary? if item.status != lconst.STATIONARY: return rpc.make_error_resp( 0, "PROBLEM: The designated item uuid corresponds to a non-stationary item.\n" "SUGGESTION: Try calling rescind_transfer() then trying again.", _id) # NOTE: load the item's location location = db.load_location(conn, item.location_uuids[-1], _id) # NOTE: set status to retired item.status = lconst.RETIRED # NOTE: remove the item from the user's item_uuids list user.item_uuids.discard(item.uuid) # NOTE: remove the item from the location's item_uuids list location.item_uuids.discard(item.uuid) # NOTE: load the each sister diamond for sister_uuid in item.sister_items: # NOTE: load sister sister = db.load_item(conn, sister_uuid, _id) # NOTE: remove the item's uuid from the sister_items list sister.sister_items.remove(item.uuid) # NOTE: save the sister db.save_existing_item(conn, sister, _id) # NOTE: save everything db.save_existing_item(conn, item, _id) db.save_existing_user(conn, user, _id) db.save_existing_location(conn, location, _id) return rpc.make_success_resp(item.one_hot_jsonify(), _id) except WrappedErrorResponse as e: file_logger.log_error({ "method": "drop_item" + str(e.methods), "params": params, "error": str(e.exception) }) return e.response_obj except Exception as e: file_logger.log_error({ "method": "drop_item", "params": params, "error": str(e), }) return rpc.make_error_resp(const.INTERNAL_ERROR_CODE, const.INTERNAL_ERROR, _id)