示例#1
0
def add_location(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)
        # CHECK: is the location type valid?
        if params["type"] not in lconst.LOCATION_TYPES:
            return rpc.make_error_resp(
                0, "PROBLEM: The provided location type is not valid.\n"
                "SUGGESTION: Try again with a valid location type.", _id)
        # CHECK: is the representative's title valid?
        if params["representative"]["title"] not in lconst.TITLES:
            return rpc.make_error_resp(
                0, "PROBLEM: The provided representative title is not valid.\n"
                "SUGGESTION: Try again with a valid representative title.",
                _id)
        location = Location(_type=params["type"],
                            name=params["name"],
                            address=params["address"],
                            details=params["details"],
                            latitude=params["latitude"],
                            longitude=params["longitude"],
                            representative=params["representative"])
        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)
            # NOTE: add the location to the user's location_uuids list
            user.location_uuids.add(location.uuid)
            # NOTE: add the user to the location's user_uuids list
            location.user_uuids.add(user.uuid)
            # NOTE: save everything
            db.save_existing_user(conn, user, _id)
            db.save_new_location(conn, location, _id)
        return rpc.make_success_resp(location.one_hot_jsonify(), _id)
    except WrappedErrorResponse as e:
        file_logger.log_error({
            "method": "add_location" + str(e.methods),
            "params": params,
            "error": str(e.exception)
        })
        return e.response_obj
    except Exception as e:
        file_logger.log_error({
            "method": "add_location",
            "params": params,
            "error": str(e),
        })
        return rpc.make_error_resp(const.INTERNAL_ERROR_CODE,
                                   const.INTERNAL_ERROR, _id)
示例#2
0
def drop_location(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 location attached to the user?
            if params["location_uuid"] not in user.location_uuids:
                return rpc.make_error_resp(
                    0,
                    "PROBLEM: The designated location uuid doesn't correspond to a location "
                    "owned by the user.\n"
                    "SUGGESTIONS: Try again with a valid location uuid.", _id)
            # NOTE: load the location
            location = db.load_location(conn, params["location_uuid"], _id)
            # CHECK: is the location empty of items?
            if len(location.item_uuids | location.incoming_item_uuids
                   | location.outgoing_item_uuids) != 0:
                return rpc.make_error_resp(
                    0,
                    "PROBLEM: The designated location has incoming, outgoing, or inventory  items.\n"
                    "SUGGESTION: Try transferring all items then trying again.",
                    _id)
            # NOTE: remove the location from the user's location_uuids list
            user.location_uuids.discard(location.uuid)
            # NOTE: save everything
            db.save_existing_user(conn, user, _id)
        return rpc.make_success_resp(location.one_hot_jsonify(), _id)
    except WrappedErrorResponse as e:
        file_logger.log_error({
            "method": "drop_location" + str(e.methods),
            "params": params,
            "error": str(e.exception)
        })
        return e.response_obj
    except Exception as e:
        file_logger.log_error({
            "method": "drop_location",
            "params": params,
            "error": str(e),
        })
        return rpc.make_error_resp(const.INTERNAL_ERROR_CODE,
                                   const.INTERNAL_ERROR, _id)
示例#3
0
def change_username(params, _id, conn, logger, config, session):
	try:
		# CHECK: is the username valid?
		length = len(params["new_username"])
		if length < lconst.MIN_USERNAME_LEN or length > lconst.MAX_USERNAME_LEN:
			return rpc.make_error_resp(const.INVALID_USER_USERNAME_CODE,
									   const.INVALID_USER_USERNAME, _id)
		# 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")
			# CHECK: is the username available?
			if len(conn.execute("""SELECT * FROM users WHERE username = ?""",
								(params["new_username"],)).fetchall()) != 0:
				return rpc.make_error_resp(0,
										   "PROBLEM: The requested username is taken.\n"
										   "SUGGESTION: Try again with a different username.",
										   _id)
			# NOTE: load the caller's user
			caller = db.load_user_w_user_id(conn, user_id, _id)
			# NOTE: update the caller's username
			caller.username = params["new_username"]
			# NOTE: recalculate the caller's user_id
			caller.recalculate_user_id()
			# NOTE: save the caller
			db.save_existing_user(conn, caller, _id)
			# NOTE: update the user_id in the session
			session["user_id"] = caller.user_id
		return rpc.make_success_resp(caller.one_hot_jsonify(), _id)
	except WrappedErrorResponse as e:
		file_logger.log_error({
			"method": "change_username" + str(e.methods),
			"params": params,
			"error": str(e.exception)
		})
		return e.response_obj
	except Exception as e:
		file_logger.log_error({
			"method": "change_username",
			"params": params,
			"error": str(e)
		})
		return rpc.make_error_resp(const.INTERNAL_ERROR_CODE,
								   const.INTERNAL_ERROR, _id)
示例#4
0
def change_avatar(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: update the caller's username
			caller.avatar = params["new_avatar"]
			# NOTE: save the caller
			db.save_existing_user(conn, caller, _id)
		return rpc.make_success_resp(caller.one_hot_jsonify(), _id)
	except WrappedErrorResponse as e:
		file_logger.log_error({
			"method": "change_avatar" + str(e.methods),
			"params": params,
			"error": str(e.exception)
		})
		return e.response_obj
	except Exception as e:
		file_logger.log_error({
			"method": "change_avatar",
			"params": params,
			"error": str(e)
		})
		return rpc.make_error_resp(const.INTERNAL_ERROR_CODE,
								   const.INTERNAL_ERROR, _id)
示例#5
0
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)
示例#6
0
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)
示例#7
0
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)
示例#8
0
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
		)
示例#9
0
def add_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)
        # NOTE: load the type
        _type = params.get("type", lconst.DIAMOND)
        # CHECK: is the type valid?
        if _type not in lconst.ITEM_TYPES:
            return rpc.make_error_resp(
                0, "PROBLEM: The designated type was not valid.\n"
                "SUGGESTION: Try again with a valid type.", _id)
        # NOTE: make the new item
        item = Item(_type=_type, location_uuids=[], user_uuids=[])
        # NOTE: make the item stationary
        item.status = lconst.STATIONARY
        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 location attached to the user?
            if params["location_uuid"] not in user.location_uuids:
                return rpc.make_error_resp(
                    0,
                    "PROBLEM: The designated location uuid doesn't correspond to a location "
                    "owned by the user.\n"
                    "SUGGESTIONS: Try again with a valid location uuid.", _id)
            # NOTE: load the location
            location = db.load_location(conn, params["location_uuid"], _id)
            # NOTE: add the location to the item's location_uuids list
            item.location_uuids.append(location.uuid)
            # NOTE: add the user to the item's user_uuids list
            item.user_uuids.append(user.uuid)
            # NOTE: add the item to the user's item_uuids list
            user.item_uuids.add(item.uuid)
            # NOTE: add the item to the location's item_uuids list
            location.item_uuids.add(item.uuid)
            # NOTE: save everything
            db.save_new_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": "add_item" + str(e.methods),
            "params": params,
            "error": str(e.exception)
        })
        return e.response_obj
    except Exception as e:
        file_logger.log_error({
            "method": "add_item",
            "params": params,
            "error": str(e),
        })
        return rpc.make_error_resp(const.INTERNAL_ERROR_CODE,
                                   const.INTERNAL_ERROR, _id)
示例#10
0
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)