Exemplo n.º 1
0
def get_user_asset(collection=None, asset_id=None):
    """ Tries to return an initialized asset from one of our collections.
    Returns a (bad) HTTP response if it cannot. """

    R = badResponse()
    try:
        if collection == "settlement":
            return settlements.Settlement(_id=asset_id)
        elif collection == "survivor":
            return survivors.Survivor(_id=asset_id)
        elif collection == "user":
            return users.User(_id=asset_id)
        else:
            return R

    except Exception as e:
        return R.send_bad_response(e)
Exemplo n.º 2
0
def new_user_asset(asset_type=None):
    """ Hands off a new asset creation request and returns the result of the
    request. Like all brokerage methods, this method always returns an HTTP
    response.

    The idea is to call this in request-processing workflow when you know that
    you've got an asset creation request.

    This brokerage method expects a few things:

        1.) you've added a logger to the request
        2.) you've also added a models.users.User object to the request
        3.) you're passing in JSON params that can be processed when
            fulfilling your request

    If you are NOT doing all of that, do NOT pass off a request to this method,
    unless you want to throw a 500 back at the user.

    """

    #    logger.debug("%s is requesting a new '%s' asset!" % (request.User, asset_type))

    if asset_type == "settlement":
        S = settlements.Settlement()
        return S.serialize()
    elif asset_type == "survivor":
        S = survivors.Survivor()
        return S.serialize()
    elif asset_type == "survivors":
        output = survivors.add_many_survivors(dict(request.get_json()))
        return Response(response=json.dumps(output, default=json_util.default),
                        status=200,
                        mimetype="application/json")
    else:
        return Response(
            response=
            "Creating '%s' user assets via request_broker() is not supported!"
            % asset_type,
            status=422,
            mimetype="text/plain",
        )

    return utils.http_400
Exemplo n.º 3
0
    def pretty_survivor(self, survivor):
        """ Clean a survivor up and make it 'shippable' as part of the world
        JSON. This initializes the survivor and will normalize it. """

        # init
        S = survivors_models.Survivor(_id=survivor["_id"],
                                      normalize_on_init=False)
        survivor["epithets"] = S.get_epithets("pretty")
        survivor["age"] = utils.get_time_elapsed_since(survivor["created_on"],
                                                       "age")

        # redact/remove
        survivor["attribute_detail"] = 'REDACTED'

        # add settlement info
        settlement = utils.mdb.settlements.find_one(
            {"_id": survivor["settlement"]})
        survivor["settlement_name"] = settlement["name"]

        return survivor
Exemplo n.º 4
0
#!/usr/bin/python2.7

import unit_test

generic_logger = unit_test.set_env()

from models import survivors

S = survivors.Survivor(_id="581cb2d24af5ca76ebb4ee6f")
#S.logger.debug("here")