Exemple #1
0
def get_csr(cr_id=None, csr_id=None, cursor=None):
    """
    Get one csr entry from database by Account ID and ID
    :param slr_id:
    :param cr_id:
    :param csr_id:
    :return: dict
    """
    if cr_id is None:
        raise AttributeError("Provide cr_id as parameter")
    if csr_id is None:
        raise AttributeError("Provide csr_id as parameter")
    if cursor is None:
        # Get DB cursor
        try:
            cursor = get_db_cursor()
        except Exception as exp:
            logger.error('Could not get database cursor: ' + repr(exp))
            raise

    try:
        db_entry_object = ConsentStatusRecord(consent_record_id=cr_id, consent_status_record_id=csr_id)
    except Exception as exp:
        error_title = "Failed to create csr object"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("csr object created: " + db_entry_object.log_entry)

    # Get csr from DB
    try:
        cursor = db_entry_object.from_db(cursor=cursor)
    except Exception as exp:
        error_title = "Failed to fetch csr from DB"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.info("csr fetched")
        logger.info("csr fetched from db: " + db_entry_object.log_entry)

    return db_entry_object.to_record_dict
Exemple #2
0
def get_last_cr_status(consent_id=None,
                       account_id="",
                       endpoint="get_last_cr_status()"):
    if consent_id is None:
        raise AttributeError("Provide consent_id as parameter")
    if account_id is None:
        raise AttributeError("Provide account_id as parameter")

    # Get DB cursor
    try:
        logger.info("Getting database cursor")
        cursor = get_db_cursor()
    except Exception as exp:
        logger.error('Could not get database cursor: ' + repr(exp))
        raise

    # Init Consent Record Object
    try:
        logger.info("Create ConsentRecord object")
        cr_entry = ConsentRecord(consent_id=consent_id)
        logger.info(cr_entry.log_entry)
    except Exception as exp:
        error_title = "Failed to create Consent Record object"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("ConsentRecord object: " + cr_entry.log_entry)

    # Get Consent Record from DB
    try:
        logger.info("Getting Consent Record from DB")
        cursor = cr_entry.from_db(cursor=cursor)
    except IndexError as exp:
        error_title = "Consent Record not found from DB with given ID"
        logger.error(error_title + ": " + repr(exp))
        raise
    except Exception as exp:
        error_title = "Failed to fetch Consent Record from DB"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("cr_entry: " + cr_entry.log_entry)

    # Create Consent Status Record object
    try:
        logger.info("Creating Consent Status Record object")
        csr_entry = ConsentStatusRecord()
    except Exception as exp:
        error_title = "Failed to create Consent Status Record object"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("Consent Status Record object: " + csr_entry.log_entry)

    # Get Consent Status Record ID
    try:
        logger.info("Getting ID of last Consent Status Record")
        cursor, csr_id = get_last_csr_id(cursor=cursor,
                                         consent_id=consent_id,
                                         account_id=account_id,
                                         table_name=csr_entry.table_name)
    except IndexError as exp:
        error_title = "Consent Status Record not found from DB with given Consent Record ID"
        logger.error(error_title + ": " + repr(exp))
        raise
    except Exception as exp:
        error_title = "Failed to get last Consent Status Record ID from database"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("Consent Status Record ID: " + str(csr_id))

    # Append IDs to Consent Status Record Object
    try:
        logger.info("Appending IDs to Consent Status Record object")
        csr_entry.consent_status_record_id = csr_id
        csr_entry.accounts_id = account_id
    except Exception as exp:
        error_title = "Failed to append IDs to Consent Status Record object"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.info("Appended IDs to Consent Status Record object: " +
                    csr_entry.log_entry)

    # Get Consent Status Record from DB
    try:
        logger.info("Getting Consent Status Record from DB")
        cursor = csr_entry.from_db(cursor=cursor)
    except IndexError as exp:
        error_title = "Consent Status Record not found from DB with given ID"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=404,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    except Exception as exp:
        error_title = "Failed to fetch Consent Status Record from DB"
        logger.error(error_title + ": " + repr(exp))
        raise ApiError(code=500,
                       title=error_title,
                       detail=repr(exp),
                       source=endpoint)
    else:
        logger.debug("Consent Status Record object: " + csr_entry.log_entry)

    return csr_entry.to_api_dict
Exemple #3
0
def get_csr(csr_id="",
            cr_id="",
            prev_record_id="",
            account_id="",
            cursor=None):
    """
    Get Consent Record entry
    
    :param csr_id: 
    :param cr_id: 
    :param prev_record_id: 
    :param account_id: 
    :param cursor: 
    :return: dict
    """
    try:
        cr_id = str(cr_id)
    except Exception:
        raise TypeError("cr_id MUST be str, not " + str(type(cr_id)))
    try:
        csr_id = str(csr_id)
    except Exception:
        raise TypeError("csr_id MUST be str, not " + str(type(csr_id)))
    try:
        prev_record_id = str(prev_record_id)
    except Exception:
        raise TypeError("prev_record_id MUST be str, not " +
                        str(type(prev_record_id)))
    try:
        account_id = str(account_id)
    except Exception:
        raise TypeError("account_id MUST be str, not " + str(type(account_id)))

    if cursor is None:
        # Get DB cursor
        try:
            cursor = get_db_cursor()
        except Exception as exp:
            logger.error('Could not get database cursor: ' + repr(exp))
            raise

    try:
        db_entry_object = ConsentStatusRecord(consent_record_id=cr_id,
                                              consent_status_record_id=csr_id,
                                              prev_record_id=prev_record_id,
                                              accounts_id=account_id)
    except Exception as exp:
        error_title = "Failed to create ConsentRecord object"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.debug("ConsentRecord object created: " +
                     db_entry_object.log_entry)

    # Get slr from DB
    try:
        cursor = db_entry_object.from_db(cursor=cursor)
    except Exception as exp:
        error_title = "Failed to fetch ConsentRecord from DB"
        logger.error(error_title + ": " + repr(exp))
        raise
    else:
        logger.info("ConsentRecord fetched")
        logger.debug("ConsentRecord fetched from db: " +
                     db_entry_object.log_entry)

    return db_entry_object.to_api_dict, str(db_entry_object.accounts_id)
Exemple #4
0
def get_csrs(cr_id=None, last_csr_id=None):
    """
    Get all csr -entries related to service link record
    :param cr_id:
    :return: List of dicts
    """
    if cr_id is None:
        raise AttributeError("Provide cr_id as parameter")

    # Get DB cursor
    try:
        cursor = get_db_cursor()
    except Exception as exp:
        logger.error('Could not get database cursor: ' + repr(exp))
        raise

    # Get CSR limit if necessary
    if last_csr_id is None:
        logger.info("No limiting CSR ID provided")
        csr_primary_key = None
    else:
        csr_limit_id = last_csr_id
        logger.info("csr_limit_id: " + str(csr_limit_id))

        # Get primary key of limiting CSR
        try:
            logger.info("Create CSR object")
            csr_entry = ConsentStatusRecord(consent_record_id=cr_id, consent_status_record_id=last_csr_id)
        except Exception as exp:
            error_title = "Failed to create csr object"
            logger.error(error_title + ": " + repr(exp))
            raise
        else:
            logger.debug("csr object created: " + csr_entry.log_entry)

        # Get csr from DB
        try:
            cursor = csr_entry.from_db(cursor=cursor)
        except Exception as exp:
            error_title = "Failed to fetch csr from DB"
            logger.error(error_title + ": " + repr(exp))
            raise
        else:
            logger.info("csr fetched")
            logger.info("csr fetched from db: " + csr_entry.log_entry)

        # Get primary key of Consent Record database entry
        try:
            logger.info("Get primary key of Consent Record database entry")
            csr_primary_key = csr_entry.id
        except Exception as exp:
            error_title = "Failed to get primary key of Consent Record database entry"
            logger.error(error_title + ": " + repr(exp))
            raise

        logger.debug("csr_primary_key: " + str(csr_primary_key))

    # Get primary keys for csrs
    try:
        # Get table name
        logger.info("Create csr")
        db_entry_object = ConsentStatusRecord()
        logger.info(db_entry_object.log_entry)
        logger.info("Get table name")
        table_name = db_entry_object.table_name
        logger.info("Got table name: " + str(table_name))

        cursor, id_list = get_csr_ids(cursor=cursor, cr_id=cr_id, csr_primary_key=csr_primary_key, table_name=table_name)
    except Exception as exp:
        logger.error('Could not get primary key list: ' + repr(exp))
        raise

    # Get csrs from database
    logger.info("Get csrs from database")
    db_entry_list = []
    for id in id_list:
        # TODO: try-except needed?
        logger.info("Getting csr with cr_id: " + str(cr_id) + " csr_id: " + str(id))
        db_entry_dict = get_csr(cr_id=cr_id, csr_id=id)
        db_entry_list.append(db_entry_dict)
        logger.info("csr object added to list: " + json.dumps(db_entry_dict))

    return db_entry_list