Пример #1
0
def get_authenticated_user(request):
    """Return the user from the request"""
    if not request:
        return err_resp('request is None')

    if not request.user.is_authenticated:
        return err_resp('user is not authenticated')

    return ok_resp(request.user)
Пример #2
0
def json_loads(json_str):
    """wrapper for json.loads with OrderedDict"""
    try:
        json_dict = json.loads(json_str,
                               object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = 'Failed to convert string to JSON: %s' % (err_obj)
        return err_resp(err_msg)
    except TypeError as err_obj:
        err_msg = 'Failed to convert string to JSON: %s' % (err_obj)
        return err_resp(err_msg)

    return ok_resp(json_dict)
Пример #3
0
def get_request_body(request):
    """Retrieve the request body
    Returns either:
        (True, content text)
        (Fales, error message)
    """
    if not request:
        return err_resp('request is None')

    if not request.body:
        return err_resp('request.body not found')

    return ok_resp(request.body.decode('utf-8'))
Пример #4
0
def load_file_contents(fpath):
    """Given a file path, open the file and return the contents"""
    if not isfile(fpath):
        user_msg = 'File not found: %s' % fpath
        return err_resp(user_msg)

    fcontents = None
    try:
        with open(fpath, 'rb') as fh:
            fcontents = fh.read()
    except IOError as ex_obj:
        user_msg = 'Could not read file: %s\nError: %s' % (fpath, ex_obj)
        return err_resp(user_msg)

    return ok_resp(fcontents)
Пример #5
0
def load_file_as_json(fpath):
    """Given a file path, open the file and convert it to an OrderedDict"""
    if not isfile(fpath):
        user_msg = 'File not found: %s' % fpath
        return err_resp(user_msg)

    fcontents = None
    try:
        with open(fpath, 'r') as fh:
            fcontents = fh.read()
    except IOError as ex_obj:
        user_msg = 'Could not read file: %s\nError: %s' % (fpath, ex_obj)
        return err_resp(user_msg)

    json_info = json_loads(fcontents)
    if not json_info.success:
        return err_resp(json_info.err_msg)

    return ok_resp(json_info.result_obj)
Пример #6
0
def json_dumps(data_dict, indent=None):
    """Dump JSON to a string w/o indents"""
    if indent is not None and \
        not isinstance(indent, int):
        # quick sanity check
        return err_resp('indent must be None or an integer')

    try:
        # dump it to a string
        jstring = json.dumps(data_dict,
                             indent=indent,
                             cls=PSIJSONEncoder)
        return ok_resp(jstring)

    except TypeError as err_obj:
        # uh oh
        user_msg = ('Failed to convert to JSON: %s'
                    ' (json_util)\n\n%s') % \
                    (err_obj, str(data_dict)[:200])
        return err_resp(user_msg)
Пример #7
0
def get_request_body_as_json(request):
    """Retrieve the request body converted to JSON (python OrderedDict)
    Returns either:
        (True, content text)
        (Fales, error message)
    """
    if not request:
        return err_resp('request is None')

    resp_info = get_request_body(request)
    if not resp_info.success:
        return resp_info

    try:
        json_data = json.loads(resp_info.result_obj,
                               object_pairs_hook=OrderedDict)
    except json.decoder.JSONDecodeError as err_obj:
        err_msg = ('Failed to convert request body to JSON: %s') % err_obj
        return err_resp(err_msg)
    except TypeError as err_obj:
        err_msg = ('Failed to convert request body to JSON: %s') % err_obj
        return err_resp(err_msg)

    return ok_resp(json_data)