Пример #1
0
def process_complex_get(req_dict):
    mime_type = "application/json"
    # Parse out params into single dict-GET data not in body
    param_dict = {}
    try:
        param_dict = req_dict['body']
    except KeyError:
        pass  # no params in the body
    param_dict.update(req_dict['params'])
    format = param_dict['format']

    # Set language if one pull from req_dict since language is from a header,
    # not an arg
    language = None
    if 'headers' in req_dict and ('format' in param_dict
                                  and param_dict['format'] == "canonical"):
        if 'language' in req_dict['headers']:
            language = req_dict['headers']['language']
        else:
            language = settings.LANGUAGE_CODE

    # If auth is in req dict, add it to param dict
    if 'auth' in req_dict:
        param_dict['auth'] = req_dict['auth']

    # Get limit if one
    limit = None
    if 'params' in req_dict and 'limit' in req_dict['params']:
        limit = int(req_dict['params']['limit'])
    elif 'body' in req_dict and 'limit' in req_dict['body']:
        limit = int(req_dict['body']['limit'])

    # See if attachments should be included
    try:
        attachments = req_dict['params']['attachments']
    except Exception:
        attachments = False

    # Create returned stmt list from the req dict
    stmt_result = complex_get(param_dict, limit, language, format, attachments)

    # Get the length of the response - make sure in string format to count
    # every character
    if isinstance(stmt_result, dict):
        content_length = len(json.dumps(stmt_result))
    else:
        content_length = len(stmt_result)

    # If attachments=True in req_dict then include the attachment payload and
    # return different mime type
    if attachments:
        stmt_result, mime_type, content_length = build_response(stmt_result)
        resp = HttpResponse(stmt_result, content_type=mime_type, status=200)
    # Else attachments are false for the complex get so just dump the
    # stmt_result
    else:
        if isinstance(stmt_result, dict):
            stmt_result = json.dumps(stmt_result)
        resp = HttpResponse(stmt_result, content_type=mime_type, status=200)
    return resp, content_length
Пример #2
0
def statements_get(req_dict):
    log_dict = req_dict['initial_user_action']    
    log_info_processing(log_dict, 'GET', __name__)
    
    if 'statements_mine_only' in req_dict:
        mine_only = True
    else:
        mine_only = False

    stmt_result = {}
    # If statementId is in req_dict then it is a single get
    if 'statementId' in req_dict:
        statementId = req_dict['statementId']
        # Try to retrieve stmt, if DNE then return empty else return stmt info                
        try:
            st = models.statement.objects.get(statement_id=statementId)
        except models.statement.DoesNotExist:
            err_msg = 'There is no statement associated with the id: %s' % statementId
            log_exception(log_dict, err_msg, statements_get.__name__)
            update_parent_log_status(log_dict, 404)
            raise exceptions.IDNotFoundError(err_msg)
        
        # check if stmt authority is in oauth group
        if mine_only and not (st.authority.id == req_dict['auth'].id):
            raise exceptions.Forbidden("Incorrect permissions to view statements that do not have auth %s" % str(req_dict['auth']))

        stmt_result = st.object_return()
    else:
        stmt_list = retrieve_statement.complex_get(req_dict)
        stmt_result = retrieve_statement.build_statement_result(req_dict, stmt_list)
    
    update_parent_log_status(log_dict, 200)

    return HttpResponse(stream_response_generator(stmt_result), mimetype="application/json", status=200)
Пример #3
0
def process_complex_get(req_dict):
    mime_type = "application/json"
    # Parse out params into single dict-GET data not in body
    param_dict = {}
    try:
        param_dict = req_dict['body']
        if not isinstance(param_dict, dict):
            param_dict = convert_to_dict(param_dict)
    except KeyError:
        pass # no params in the body    
    param_dict.update(req_dict['params'])
    format = param_dict['format']
    
    # Set language if one pull from req_dict since language is from a header, not an arg 
    language = None
    if 'headers' in req_dict and ('format' in param_dict and param_dict['format'] == "canonical"):
        if 'language' in req_dict['headers']:
            language = req_dict['headers']['language']
        else:
            language = settings.LANGUAGE_CODE

    # If auth is in req dict, add it to param dict
    if 'auth' in req_dict:
        param_dict['auth'] = req_dict['auth']

    # Get limit if one
    limit = None
    if 'params' in req_dict and 'limit' in req_dict['params']:
        limit = int(req_dict['params']['limit'])
    elif 'body' in req_dict and 'limit' in req_dict['body']:
        limit = int(req_dict['body']['limit'])

    # See if attachments should be included
    try:
        attachments = req_dict['params']['attachments']
    except Exception:
        attachments = False

    # Create returned stmt list from the req dict
    stmt_result = complex_get(param_dict, limit, language, format, attachments)
    
    if format == 'exact':
        content_length = len(stmt_result)    
    else:
        content_length = len(json.dumps(stmt_result))

    # If attachments=True in req_dict then include the attachment payload and return different mime type
    if attachments:
        stmt_result, mime_type, content_length = build_response(stmt_result)
        resp = HttpResponse(stmt_result, content_type=mime_type, status=200)
    # Else attachments are false for the complex get so just dump the stmt_result
    else:
        if format == 'exact':
            result = stmt_result
        else:
            result = json.dumps(stmt_result)
        content_length = len(result)
        resp = HttpResponse(result, content_type=mime_type, status=200)    
    return resp, content_length
Пример #4
0
def statements_get(req_dict):
    log_dict = req_dict['initial_user_action']    
    log_info_processing(log_dict, 'GET', __name__)

    stmt_result = {}
    # If statementId is in req_dict then it is a single get
    if 'statementId' in req_dict:
        statementId = req_dict['statementId']
        # Try to retrieve stmt, if DNE then return empty else return stmt info                
        try:
            st = models.statement.objects.get(statement_id=statementId)
        except models.statement.DoesNotExist:
            err_msg = 'There is no statement associated with the id: %s' % statementId
            log_exception(log_dict, err_msg, statements_get.__name__)
            update_parent_log_status(log_dict, 404)
            raise exceptions.IDNotFoundError(err_msg)
        stmt_result = st.object_return()
    else:
        stmt_list = retrieve_statement.complex_get(req_dict)
        stmt_result = retrieve_statement.build_statement_result(req_dict.copy(), stmt_list)
    
    update_parent_log_status(log_dict, 200)
    return HttpResponse(stream_response_generator(stmt_result), mimetype="application/json", status=200)
Пример #5
0
    # Get limit if one
    limit = None
    if 'params' in req_dict and 'limit' in req_dict['params']:
        limit = int(req_dict['params']['limit'])
    elif 'body' in req_dict and 'limit' in req_dict['body']:
        limit = int(req_dict['body']['limit'])

    # See if attachments should be included
    try:
        attachments = req_dict['params']['attachments']
    except Exception, e:
        attachments = False

    # Create returned stmt list from the req dict
    stmt_result = retrieve_statement.complex_get(param_dict, limit, language, format, attachments)
    
    if format == 'exact':
        content_length = len(stmt_result)    
    else:
        content_length = len(json.dumps(stmt_result))

    # If attachments=True in req_dict then include the attachment payload and return different mime type
    if attachments:
        stmt_result, mime_type, content_length = build_response(stmt_result, content_length)
        resp = HttpResponse(stmt_result, mimetype=mime_type, status=200)
    # Else attachments are false for the complex get so just dump the stmt_result
    else:
        if format == 'exact':
            result = stmt_result
        else:
Пример #6
0
def statements_get(req_dict):
    auth = req_dict.get('auth', None)
    mine_only = auth and 'statements_mine_only' in auth

    stmt_result = {}
    mime_type = "application/json"
    # If statementId is in req_dict then it is a single get
    if 'params' in req_dict and ('statementId' in req_dict['params']
                                 or 'voidedStatementId' in req_dict['params']):
        if 'statementId' in req_dict['params']:
            statementId = req_dict['params']['statementId']
            voided = False
        else:
            statementId = req_dict['params']['voidedStatementId']
            voided = True

        # Try to retrieve stmt, if DNE then return empty else return stmt info
        try:
            st = models.Statement.objects.get(statement_id=statementId)
        except models.Statement.DoesNotExist:
            err_msg = 'There is no statement associated with the id: %s' % statementId
            raise exceptions.IDNotFoundError(err_msg)

        if mine_only and st.authority.id != req_dict['auth']['id'].id:
            err_msg = "Incorrect permissions to view statements that do not have auth %s" % str(
                req_dict['auth']['id'])
            raise exceptions.Forbidden(err_msg)

        if st.voided != voided:
            if st.voided:
                err_msg = 'The requested statement (%s) is voided. Use the "voidedStatementId" parameter to retrieve your statement.' % statementId
            else:
                err_msg = 'The requested statement (%s) is not voided. Use the "statementId" parameter to retrieve your statement.' % statementId
            raise exceptions.IDNotFoundError(err_msg)

        # Once validated, return the object, dump to json, and set content length
        stmt_result = json.dumps(st.object_return())
        resp = HttpResponse(stmt_result, mimetype=mime_type, status=200)
        content_length = len(stmt_result)
    # Complex GET
    else:
        # Parse out params into single dict-GET data not in body
        param_dict = {}
        try:
            param_dict = req_dict['body']
            if not isinstance(param_dict, dict):
                param_dict = convert_to_dict(param_dict)
        except KeyError:
            pass  # no params in the body
        param_dict.update(req_dict['params'])
        format = param_dict['format']

        # Set language if one pull from req_dict since language is from a header, not an arg
        language = None
        if 'headers' in req_dict and ('format' in param_dict
                                      and param_dict['format'] == "canonical"):
            if 'language' in req_dict['headers']:
                language = req_dict['headers']['language']
            else:
                language = settings.LANGUAGE_CODE

        # If auth is in req dict, add it to param dict
        if 'auth' in req_dict:
            param_dict['auth'] = req_dict['auth']

        # Create returned stmt list from the req dict
        stmt_list = retrieve_statement.complex_get(param_dict)
        # Build json result({statements:...,more:...}) and set content length
        limit = None
        if 'params' in req_dict and 'limit' in req_dict['params']:
            limit = int(req_dict['params']['limit'])
        elif 'body' in req_dict and 'limit' in req_dict['body']:
            limit = int(req_dict['body']['limit'])

        attachments = req_dict['params']['attachments']

        stmt_result = retrieve_statement.build_statement_result(
            language, format, limit, stmt_list, attachments)
        content_length = len(json.dumps(stmt_result))

        # If attachments=True in req_dict then include the attachment payload and return different mime type
        if 'params' in req_dict and ('attachments' in req_dict['params']
                                     and req_dict['params']['attachments']):
            stmt_result, mime_type, content_length = build_response(
                stmt_result, content_length)
            resp = HttpResponse(stmt_result, mimetype=mime_type, status=200)
        # Else attachments are false for the complex get so just dump the stmt_result
        else:
            resp = HttpResponse(json.dumps(stmt_result),
                                mimetype=mime_type,
                                status=200)

    # Set consistent through and content length headers for all responses
    try:
        resp['X-Experience-API-Consistent-Through'] = str(
            models.Statement.objects.latest('stored').stored)
    except:
        resp['X-Experience-API-Consistent-Through'] = str(datetime.now())

    resp['Content-Length'] = str(content_length)
    return resp
Пример #7
0
def process_complex_get(req_dict):
    mime_type = "application/json"
    # Parse out params into single dict-GET data not in body
    param_dict = {}
    try:
        param_dict = req_dict['body']
    except KeyError:
        pass  # no params in the body
    param_dict.update(req_dict['params'])
    format = param_dict['format']

    # Set language if one pull from req_dict since language is from a header,
    # not an arg
    language = None
    if 'headers' in req_dict and ('format' in param_dict and param_dict['format'] == "canonical"):
        if 'language' in req_dict['headers']:
            lang_list = req_dict['headers']['language'].split(',')
            if len(lang_list) > 1:
                for idx, lang in enumerate(lang_list):
                    parts = lang.split(';')
                    if len(parts) == 1:
                        if parts[0] == '*':
                            lang_list[idx] = "anylanguage;q=0.1"
                        else:
                            lang_list[idx] = parts[0] + ";q=1.0"          
                lang_list = sorted(lang_list, key=lambda x : float(x[-3:]), reverse=True)
                language = [x[:x.index(';')].strip() for x in lang_list]
            else:
                if lang_list[0] == '*':
                    language = ["anylanguage"]
                else:
                    language = [req_dict['headers']['language']]
        else:
            language = [settings.LANGUAGE_CODE]

    # If auth is in req dict, add it to param dict
    if 'auth' in req_dict:
        param_dict['auth'] = req_dict['auth']

    # Get limit if one
    limit = None
    if 'params' in req_dict and 'limit' in req_dict['params']:
        limit = int(req_dict['params']['limit'])
    elif 'body' in req_dict and 'limit' in req_dict['body']:
        limit = int(req_dict['body']['limit'])

    # See if attachments should be included
    try:
        attachments = req_dict['params']['attachments']
    except Exception:
        attachments = False

    # Create returned stmt list from the req dict
    stmt_result = complex_get(param_dict, limit, language, format, attachments)

    # Get the length of the response - make sure in string format to count
    # every character
    if isinstance(stmt_result, dict):
        content_length = len(json.dumps(stmt_result))
    else:
        content_length = len(stmt_result)

    # If attachments=True in req_dict then include the attachment payload and
    # return different mime type
    if attachments:
        stmt_result, mime_type, content_length = build_response(stmt_result)
        resp = HttpResponse(stmt_result, content_type=mime_type, status=200)
    # Else attachments are false for the complex get so just dump the
    # stmt_result
    else:
        if isinstance(stmt_result, dict):
            stmt_result = json.dumps(stmt_result)
        resp = HttpResponse(stmt_result, content_type=mime_type, status=200)
    return resp, content_length
Пример #8
0
def statements_get(req_dict):
    auth = req_dict.get('auth', None)
    mine_only = auth and 'statements_mine_only' in auth

    stmt_result = {}
    mime_type = "application/json"
    # If statementId is in req_dict then it is a single get
    if 'params' in req_dict and ('statementId' in req_dict['params'] or 'voidedStatementId' in req_dict['params']):
        if 'statementId' in req_dict['params']:
            statementId = req_dict['params']['statementId']
            voided = False
        else:
            statementId = req_dict['params']['voidedStatementId']
            voided = True

        # Try to retrieve stmt, if DNE then return empty else return stmt info                
        try:
            st = models.Statement.objects.get(statement_id=statementId)
        except models.Statement.DoesNotExist:
            err_msg = 'There is no statement associated with the id: %s' % statementId
            raise exceptions.IDNotFoundError(err_msg)

        if mine_only and st.authority.id != req_dict['auth']['id'].id:
            err_msg = "Incorrect permissions to view statements that do not have auth %s" % str(req_dict['auth']['id'])
            raise exceptions.Forbidden(err_msg)
        
        if st.voided != voided:
            if st.voided:
                err_msg = 'The requested statement (%s) is voided. Use the "voidedStatementId" parameter to retrieve your statement.' % statementId
            else:
                err_msg = 'The requested statement (%s) is not voided. Use the "statementId" parameter to retrieve your statement.' % statementId
            raise exceptions.IDNotFoundError(err_msg)
        
        # Once validated, return the object, dump to json, and set content length
        stmt_result = json.dumps(st.object_return())
        resp = HttpResponse(stmt_result, mimetype=mime_type, status=200)
        content_length = len(json.dumps(stmt_result))
    # Complex GET
    else:
        # Create returned stmt list from the req dict
        stmt_list = retrieve_statement.complex_get(req_dict)
        # Build json result({statements:...,more:...}) and set content length
        limit = None
        if 'params' in req_dict and 'limit' in req_dict['params']:
            limit = int(req_dict['params']['limit'])
        elif 'body' in req_dict and 'limit' in req_dict['body']:
            limit = int(req_dict['body']['limit'])
        
        stmt_result = retrieve_statement.build_statement_result(limit, stmt_list, req_dict['params']['attachments'])
        content_length = len(json.dumps(stmt_result))

        # If attachments=True in req_dict then include the attachment payload and return different mime type
        if 'params' in req_dict and ('attachments' in req_dict['params'] and req_dict['params']['attachments']):
            stmt_result, mime_type, content_length = build_response(stmt_result, content_length)
            resp = HttpResponse(stmt_result, mimetype=mime_type, status=200)
        # Else attachments are false for the complex get so just dump the stmt_result
        else:
            resp = HttpResponse(json.dumps(stmt_result), mimetype=mime_type, status=200)
    
    # Set consistent through and content length headers for all responses
    try:
        resp['X-Experience-API-Consistent-Through'] = str(models.Statement.objects.latest('stored').stored)
    except:
        resp['X-Experience-API-Consistent-Through'] = str(datetime.now())
    
    resp['Content-Length'] = str(content_length)
    
    return resp
Пример #9
0
def statements_get(req_dict):
    auth = req_dict.get('auth', None)
    mine_only = auth and 'statements_mine_only' in auth

    stmt_result = {}
    mime_type = "application/json"
    # If statementId is in req_dict then it is a single get
    if 'params' in req_dict and ('statementId' in req_dict['params'] or 'voidedStatementId' in req_dict['params']):
        if 'statementId' in req_dict['params']:
            statementId = req_dict['params']['statementId']
            voided = False
        else:
            statementId = req_dict['params']['voidedStatementId']
            voided = True

        # Try to retrieve stmt, if DNE then return empty else return stmt info                
        try:
            st = models.Statement.objects.get(statement_id=statementId)
        except models.Statement.DoesNotExist:
            err_msg = 'There is no statement associated with the id: %s' % statementId
            raise exceptions.IDNotFoundError(err_msg)

        if mine_only and st.authority.id != req_dict['auth']['id'].id:
            err_msg = "Incorrect permissions to view statements that do not have auth %s" % str(req_dict['auth']['id'])
            raise exceptions.Forbidden(err_msg)
        
        if st.voided != voided:
            if st.voided:
                err_msg = 'The requested statement (%s) is voided. Use the "voidedStatementId" parameter to retrieve your statement.' % statementId
            else:
                err_msg = 'The requested statement (%s) is not voided. Use the "statementId" parameter to retrieve your statement.' % statementId
            raise exceptions.IDNotFoundError(err_msg)
        
        # Once validated, return the object, will already be json since format will be exact
        stmt_result = st.object_return()
        
        resp = HttpResponse(stmt_result, mimetype=mime_type, status=200)
        content_length = len(stmt_result)
    # Complex GET
    else:
        # Parse out params into single dict-GET data not in body
        param_dict = {}
        try:
            param_dict = req_dict['body']
            if not isinstance(param_dict, dict):
                param_dict = convert_to_dict(param_dict)
        except KeyError:
            pass # no params in the body    
        param_dict.update(req_dict['params'])
        format = param_dict['format']
        
        # Set language if one pull from req_dict since language is from a header, not an arg 
        language = None
        if 'headers' in req_dict and ('format' in param_dict and param_dict['format'] == "canonical"):
            if 'language' in req_dict['headers']:
                language = req_dict['headers']['language']
            else:
                language = settings.LANGUAGE_CODE

        # If auth is in req dict, add it to param dict
        if 'auth' in req_dict:
            param_dict['auth'] = req_dict['auth']

        # Get limit if one
        limit = None
        if 'params' in req_dict and 'limit' in req_dict['params']:
            limit = int(req_dict['params']['limit'])
        elif 'body' in req_dict and 'limit' in req_dict['body']:
            limit = int(req_dict['body']['limit'])

        # See if attachments should be included
        try:
            attachments = req_dict['params']['attachments']
        except Exception, e:
            attachments = False

        # Create returned stmt list from the req dict
        stmt_result = retrieve_statement.complex_get(param_dict, limit, language, format, attachments)
        
        if format == 'exact':
            content_length = len(stmt_result)    
        else:
            content_length = len(json.dumps(stmt_result))

        # If attachments=True in req_dict then include the attachment payload and return different mime type
        if attachments:
            stmt_result, mime_type, content_length = build_response(stmt_result, content_length)
            resp = HttpResponse(stmt_result, mimetype=mime_type, status=200)
        # Else attachments are false for the complex get so just dump the stmt_result
        else:
            if format == 'exact':
                result = stmt_result
            else:
                result = json.dumps(stmt_result)
            content_length = len(result)
            resp = HttpResponse(result, mimetype=mime_type, status=200)
Пример #10
0
    # Get limit if one
    limit = None
    if 'params' in req_dict and 'limit' in req_dict['params']:
        limit = int(req_dict['params']['limit'])
    elif 'body' in req_dict and 'limit' in req_dict['body']:
        limit = int(req_dict['body']['limit'])

    # See if attachments should be included
    try:
        attachments = req_dict['params']['attachments']
    except Exception, e:
        attachments = False

    # Create returned stmt list from the req dict
    stmt_result = retrieve_statement.complex_get(param_dict, limit, language,
                                                 format, attachments)

    if format == 'exact':
        content_length = len(stmt_result)
    else:
        content_length = len(json.dumps(stmt_result))

    # If attachments=True in req_dict then include the attachment payload and return different mime type
    if attachments:
        stmt_result, mime_type, content_length = build_response(
            stmt_result, content_length)
        resp = HttpResponse(stmt_result, mimetype=mime_type, status=200)
    # Else attachments are false for the complex get so just dump the stmt_result
    else:
        if format == 'exact':
            result = stmt_result