def populate_rating_scale(xml_file):
    """Scan original model and return change reason as well as change type."""

    create_record = False
    esma_record = None

    create_or_update_id = []

    reporting_reason_dict = {}
    reporting_type_dict = {}
    change_reason_dict = {}
    """Rating scale."""
    for rating_scale in RatingScaleOrig.objects.order_by('id').all():
        """Do a field by field comparison"""
        # Unique identifier for this model
        unique_identifier_rating_scale = format_reference_number(
            object_type='rating_scale', number=rating_scale.id)

        try:
            esma_record = RatingScaleESMA.objects.filter(
                rating_scale_code=unique_identifier_rating_scale,
                xml_file__status_code=1).order_by('-id').all()[0]

        except (IndexError, RatingScaleESMA.DoesNotExist):
            """There is no record in the ESMA model, create it."""

            create_record = True

            # Create a list of IDs that should be inserted
            create_or_update_id.append(rating_scale.id)

            reporting_type_dict[rating_scale.id] = 1  # 1=NEW

            # must be list
            reporting_reason_dict[rating_scale.id] = [STRING_NEW_ITEM]

        if not create_record:
            """There is an existing parent, check for differences and if
            differences, generate change reason and change."""

            reporting_reason_dict[rating_scale.id] = []

            for compare_obj in RSCALE_COMPARE_FIELDS:
                """Compare value of field in base model with ESMA record."""
                if (getattr(rating_scale, compare_obj['field_orig']) !=
                        getattr(esma_record, compare_obj['field_esma'])):

                    create_or_update_id.append(rating_scale.id)

                    reporting_type_dict[
                        rating_scale.id] = compare_obj['reporting_type']
                    change_reason_dict[
                        rating_scale.id] = compare_obj['change_reason']
                    reporting_reason_dict[rating_scale.id].append(
                        compare_obj['reporting_reason'])
            """Scope."""
            for orig_scope_record in RatingScopeOrig.objects.filter(
                    rating_scale=rating_scale).order_by('-id').all():

                unique_identifier_rating_scope = format_reference_number(
                    object_type='rating_scope', number=orig_scope_record.id)

                esma_record_scope = RatingScopeESMA.objects.filter(
                    rating_scope_code=unique_identifier_rating_scope).order_by(
                        '-id')[0]

                for compare_obj in RSCOPE_COMPARE_FIELDS:
                    """Compare value of field in base model with
                    ESMA record."""
                    if (getattr(orig_scope_record, compare_obj['field_orig'])
                            != getattr(esma_record_scope,
                                       compare_obj['field_esma'])):

                        # Should be top level id
                        create_or_update_id.append(rating_scale.id)

                        reporting_type_dict[
                            rating_scale.id] = compare_obj['reporting_type']
                        change_reason_dict[
                            rating_scale.id] = compare_obj['change_reason']
                        reporting_reason_dict[rating_scale.id].append(
                            compare_obj['reporting_reason'])
            """Category."""
            for orig_category_record in RatingCategoryOrig.objects.filter(
                    rating_scale=rating_scale).order_by('-id').all():

                unique_identifier_rating_category = format_reference_number(
                    object_type='rating_category',
                    number=orig_category_record.id)

                esma_record_category = RatingCategoryESMA.objects.filter(
                    rating_category_code=unique_identifier_rating_category
                ).order_by('-id')[0]

                for compare_obj in RCATEGORY_COMPARE_FIELDS:
                    """Compare value of field in base model with ESMA
                    record."""
                    if (getattr(orig_category_record,
                                compare_obj['field_orig']) != getattr(
                                    esma_record_category,
                                    compare_obj['field_esma'])):

                        # Should be top level id
                        create_or_update_id.append(rating_scale.id)

                        reporting_type_dict[
                            rating_scale.id] = compare_obj['reporting_type']
                        change_reason_dict[
                            rating_scale.id] = compare_obj['change_reason']
                        reporting_reason_dict[rating_scale.id].append(
                            compare_obj['reporting_reason'])
                    """Notch."""
                    for orig_notch_record in RatingNotchOrig.objects.filter(
                            rating_category=orig_category_record).order_by(
                                '-id').all():

                        unique_identifier_rating_notch = \
                            format_reference_number(
                                object_type='rating_notch',
                                number=orig_notch_record.id)

                        esma_record_notch = RatingNotchESMA.objects.filter(
                            rating_notch_code=unique_identifier_rating_notch
                        ).order_by('-id')[0]

                        for compare_obj in RNOTCH_COMPARE_FIELDS:
                            """Compare value of field in base model with
                            ESMA record."""
                            if (getattr(orig_notch_record,
                                        compare_obj['field_orig']) != getattr(
                                            esma_record_notch,
                                            compare_obj['field_esma'])):

                                # Should be top level id
                                create_or_update_id.append(rating_scale.id)

                                reporting_type_dict[
                                    rating_scale.
                                    id] = compare_obj['reporting_type']
                                change_reason_dict[
                                    rating_scale.
                                    id] = compare_obj['change_reason']
                                reporting_reason_dict[rating_scale.id].append(
                                    compare_obj['reporting_reason'])

            # Looks like nothing happened with this record, delete it
            if len(reporting_reason_dict[rating_scale.id]) == 0:
                del reporting_reason_dict[rating_scale.id]

    for rating_scale_orig in RatingScaleOrig.objects.filter(
            pk__in=create_or_update_id).all():

        reporting_type = reporting_type_dict[rating_scale_orig.id]
        reporting_reason = reporting_reason_dict[rating_scale_orig.id]

        try:
            change_reason = change_reason_dict[rating_scale_orig.id]
        except KeyError:
            change_reason = None

        unique_identifier_rating_scale = format_reference_number(
            object_type='rating_scale', number=rating_scale_orig.id)

        # Create a new ReportingTypeInfo record
        reporting_type_info, _ = get_or_create_reporting_type_info(
            reporting_type=reporting_type,
            change_reason=change_reason,
            reporting_reason_text=create_reporting_reason_string(
                reporting_reason),
            hash_string=create_hash_string(unique_identifier_rating_scale,
                                           reporting_type))

        # Create a new record
        rating_scale_esma = RatingScaleESMA.objects.create(
            reporting_type_info=reporting_type_info,
            xml_file=xml_file,

            # Insert all model fields below here
            rating_scale_code=unique_identifier_rating_scale,
            start_date=rating_scale_orig.start_date,
            end_date=rating_scale_orig.end_date,
            description=rating_scale_orig.description,
        )

        for rating_scope_orig in RatingScopeOrig.objects.filter(
                rating_scale=rating_scale_orig).order_by('id').all():

            unique_identifier_rating_scope = format_reference_number(
                object_type='rating_scope', number=rating_scope_orig.id)

            # Create a new record
            RatingScopeESMA.objects.create(
                rating_scale=rating_scale_esma,

                # Insert all model fields below here
                rating_scope_code=unique_identifier_rating_scope,
                time_horizon=rating_scope_orig.time_horizon,
                rating_type=rating_scope_orig.rating_type,
                rating_scale_scope=rating_scope_orig.rating_scale_scope,
                relevant_for_cerep_flag=rating_scope_orig.
                relevant_for_cerep_flag,
                start_date=rating_scope_orig.start_date,
                end_date=rating_scope_orig.end_date,
            )

        for rating_category_orig in RatingCategoryOrig.objects.filter(
                rating_scale=rating_scale_orig).order_by('id').all():

            unique_identifier_rating_category = format_reference_number(
                object_type='rating_category', number=rating_category_orig.id)

            # Create a new record
            rating_category_esma = RatingCategoryESMA.objects.create(
                rating_scale=rating_scale_esma,

                # Insert all model fields below here
                rating_category_code=unique_identifier_rating_category,
                value=rating_category_orig.value,
                label=rating_category_orig.label,
                description=rating_category_orig.description,
            )

            for rating_notch_orig in RatingNotchOrig.objects.filter(
                    rating_category=rating_category_orig).order_by('id').all():

                unique_identifier_rating_notch = format_reference_number(
                    object_type='rating_notch', number=rating_notch_orig.id)

                # Create a new record
                RatingNotchESMA.objects.create(
                    rating_category=rating_category_esma,

                    # Insert all model fields below here
                    rating_notch_code=unique_identifier_rating_notch,
                    value=rating_notch_orig.value,
                    label=rating_notch_orig.label,
                    description=rating_notch_orig.description,
                )
Example #2
0
def populate_cra_info(xml_file):
    """Scan original model and insert into ESMA model if required.
    This function does a field-by-field comparison against"""

    for orig_record in OrigModel.objects.all():

        # As a default, don't do anything
        change_reason = None
        reporting_type = None
        reporting_reason = []
        create_record = False

        # Unique identifier for this model
        unique_identifier = format_reference_number(
            object_type='cra_info',
            number=orig_record.id)

        try:
            esma_record = ESMAModel.objects.last_valid_record()[0]

            for compare_obj in COMPARE_FIELDS:

                """Compare value of field in base model with ESMA record."""
                if (getattr(orig_record, compare_obj['field_orig']) !=
                        getattr(esma_record, compare_obj['field_esma'])):

                    # Something has changed, create a new entry in ESMA-table.
                    create_record = True

                    reporting_type = compare_obj['reporting_type']
                    change_reason = compare_obj['change_reason']
                    reporting_reason.append(
                        compare_obj['reporting_reason'])

        except (IndexError, ESMAModel.DoesNotExist):
            """The object is not existing in the ESMA table:
            create it."""

            create_record = True
            reporting_type = 1  # 1=NEW
            reporting_reason.append(STRING_NEW_ITEM)

        if create_record:
            # Create a new ReportingTypeInfo record
            reporting_type_info, _ = get_or_create_reporting_type_info(
                reporting_type=reporting_type,
                change_reason=change_reason,
                reporting_reason_text=create_reporting_reason_string(
                    reporting_reason),
                hash_string=create_hash_string(
                    unique_identifier,
                    reporting_type)
            )

            # Because of the long variable name
            a = orig_record.\
                solicited_unsolicited_rating_policy_description

            # Create a new record
            ESMAModel.objects.create(
                xml_file=xml_file,
                reporting_type_info=reporting_type_info,

                # Insert all model fields below here
                cra_info_code=unique_identifier,
                cra_name=orig_record.cra_name,
                cra_description=orig_record.cra_description,
                cra_methodology=orig_record.cra_methodology,
                cra_methodology_webpage_link=orig_record.
                cra_methodology_webpage_link,
                solicited_unsolicited_rating_policy_description=a,
                subsidiary_rating_policy=orig_record.
                subsidiary_rating_policy,
                global_reporting_scope_flag=orig_record.
                global_reporting_scope_flag,
                definition_default=orig_record.definition_default,
                cra_website_link=orig_record.cra_website_link,
            )
Example #3
0
def populate_lead_analyst(xml_file):
    """Scan original model and insert into ESMA model if required.
    This function does a field-by-field comparison against"""

    for orig_record in OrigModel.objects.all():

        # As a default, don't do anything
        change_reason = None
        reporting_type = None
        reporting_reason = []
        create_record = False

        # Unique identifier for this model
        unique_identifier = orig_record.profile.user.username

        try:
            esma_record = ESMAModel.objects.last_valid_record().filter(
                lead_analyst_code=unique_identifier)[0]

            for compare_obj in COMPARE_FIELDS:
                """Compare value of field in base model with ESMA record."""
                if (getattr(orig_record, compare_obj['field_orig']) != getattr(
                        esma_record, compare_obj['field_esma'])):

                    # Something has changed, create a new entry in ESMA-table.
                    create_record = True

                    reporting_type = compare_obj['reporting_type']
                    change_reason = compare_obj['change_reason']
                    reporting_reason.append(compare_obj['reporting_reason'])

            # Custom comparison of full name due to the OrigModel
            # being a bit different
            if orig_record.profile.full_name != esma_record.lead_analyst_name:
                create_record = True

                reporting_type = 2  # 2=CHG
                change_reason = 2  # 2=U: Consider this to be an an update
                reporting_reason.append('change of full name')

        except (IndexError, ESMAModel.DoesNotExist):
            """The object is not existing in the ESMA table:
            create it."""

            create_record = True
            reporting_type = 1  # 1=NEW
            reporting_reason.append(STRING_NEW_ITEM)

        if create_record:
            # Create a new ReportingTypeInfo record
            reporting_type_info, _ = get_or_create_reporting_type_info(
                reporting_type=reporting_type,
                change_reason=change_reason,
                reporting_reason_text=create_reporting_reason_string(
                    reporting_reason),
                hash_string=create_hash_string(unique_identifier,
                                               reporting_type))

            # Create a new record
            ESMAModel.objects.create(
                xml_file=xml_file,
                reporting_type_info=reporting_type_info,

                # Insert all model fields below here
                lead_analyst_code=unique_identifier,
                lead_analyst_name=orig_record.profile.full_name,
                lead_analyst_start_date=orig_record.start_date,
                lead_analyst_end_date=orig_record.end_date,
            )
Example #4
0
def populate_issue_program(xml_file):
    """Scan original model and insert into ESMA model if required."""

    for orig_record in OrigModel.objects.all():

        # As a default, don't do anything
        change_reason = None
        reporting_type = None
        reporting_reason = []
        create_record = False

        # Unique identifier for this model
        unique_identifier = format_reference_number(
            object_type='issue_program', number=orig_record.id)

        try:
            esma_record = ESMAModel.objects.last_valid_record().filter(
                program_code=unique_identifier)[0]

            for compare_obj in COMPARE_FIELDS:
                """Compare value of field in base model with ESMA record."""
                if (getattr(orig_record, compare_obj['field_orig']) != getattr(
                        esma_record, compare_obj['field_esma'])):

                    create_record = True

                    reporting_type = compare_obj['reporting_type']
                    change_reason = compare_obj['change_reason']
                    reporting_reason.append(compare_obj['reporting_reason'])

        except (IndexError, ESMAModel.DoesNotExist):
            """The rating scale is not existing in the ESMA table,
            create it."""

            create_record = True

            reporting_type = 1  # 1=NEW
            reporting_reason.append(STRING_NEW_ITEM)

        if create_record:

            # Create a new ReportingTypeInfo record
            reporting_type_info, _ = get_or_create_reporting_type_info(
                reporting_type=reporting_type,
                change_reason=change_reason,
                reporting_reason_text=create_reporting_reason_string(
                    reporting_reason),
                #  Change the unique identifiere here
                hash_string=create_hash_string(unique_identifier,
                                               reporting_type))

            # Create a new record
            ESMAModel.objects.create(
                xml_file=xml_file,
                reporting_type_info=reporting_type_info,

                # Insert all model fields below here
                program_code=unique_identifier,
                program_name=orig_record.name,
                program_description=orig_record.description,
                program_start_date=orig_record.start_date,
                program_end_date=orig_record.end_date,
            )
def populate_debt_classification(xml_file):
    """Scan original model and insert into ESMA model if required."""

    for orig_record in OrigModel.objects.all():

        # As a default, don't do anything
        change_reason = None
        reporting_type = None
        reporting_reason = []
        create_record = False

        # Unique identifier for this model
        unique_identifier = format_reference_number(object_type='debt_class',
                                                    number=orig_record.id)

        try:
            esma_record = ESMAModel.objects.last_valid_record().filter(
                debt_classification_code=unique_identifier)[0]

            for compare_obj in COMPARE_FIELDS:
                """Compare value of field in base model with ESMA record."""
                if (getattr(orig_record, compare_obj['field_orig']) != getattr(
                        esma_record, compare_obj['field_esma'])):

                    create_record = True

                    reporting_type = compare_obj['reporting_type']
                    change_reason = compare_obj['change_reason']
                    reporting_reason.append(compare_obj['reporting_reason'])

            # Custom comparison of seniority due to the OrigModel
            # being a bit different
            if orig_record.seniority_level.id != esma_record.seniority:
                create_record = True

                reporting_type = 2  # 2=CHG
                change_reason = 1  # 2=U: Consider this to be an an update
                reporting_reason.append('seniority level')

        except (IndexError, ESMAModel.DoesNotExist):
            """The rating scale is not existing in the ESMA table,
            create it."""

            create_record = True

            reporting_type = 1  # 1=NEW
            reporting_reason.append(STRING_NEW_ITEM)

        if create_record:

            # Create a new ReportingTypeInfo record
            reporting_type_info, _ = get_or_create_reporting_type_info(
                reporting_type=reporting_type,
                change_reason=change_reason,
                reporting_reason_text=create_reporting_reason_string(
                    reporting_reason),
                hash_string=create_hash_string(unique_identifier,
                                               reporting_type))

            # Create a new record
            ESMAModel.objects.create(
                xml_file=xml_file,
                reporting_type_info=reporting_type_info,

                # Insert all model fields below here
                debt_classification_code=unique_identifier,
                debt_classification_name=orig_record.name,
                debt_classification_description=orig_record.description,
                seniority=orig_record.seniority_level.id,
                debt_classification_start_date=orig_record.start_date,
                debt_classification_end_date=orig_record.end_date,
            )