<commit_before>from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations


def pull_remote_group(apps, schema_editor):
    SecurityGroupRuleResource = apps.get_model('openstack', 'SecurityGroupRule')
    SecurityGroupProperty = apps.get_model('openstack_tenant', 'SecurityGroup')
    SecurityGroupRuleProperty = apps.get_model('openstack_tenant', 'SecurityGroupRule')
    ServiceSettings = apps.get_model('structure', 'ServiceSettings')

    for rule_resource in SecurityGroupRuleResource.objects.exclude(remote_group=None):
        try:
            service_settings = ServiceSettings.objects.get(
                scope=rule_resource.security_group.tenant, type='OpenStackTenant'
            )
            security_group = SecurityGroupProperty.objects.get(
                settings=service_settings,
                backend_id=rule_resource.security_group.backend_id,
            )
            remote_group = SecurityGroupProperty.objects.get(
                settings=service_settings,
                backend_id=rule_resource.remote_group.backend_id,
            )
            rule_property = SecurityGroupRuleProperty.objects.get(
                security_group=security_group, backend_id=rule_resource.backend_id
            )
        except ObjectDoesNotExist:
            continue
        else:
            if rule_property.remote_group != remote_group:
                rule_property.remote_group = remote_group
                rule_property.save(update_fields=['remote_group'])
<commit_msg>Fix data migration to pull remote group for security group rule.<commit_after>from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations


def pull_remote_group(apps, schema_editor):
    SecurityGroupRuleResource = apps.get_model('openstack', 'SecurityGroupRule')
    SecurityGroupProperty = apps.get_model('openstack_tenant', 'SecurityGroup')
    SecurityGroupRuleProperty = apps.get_model('openstack_tenant', 'SecurityGroupRule')
    ServiceSettings = apps.get_model('structure', 'ServiceSettings')
    Tenant = apps.get_model('openstack', 'Tenant')
    ContentType = apps.get_model('contenttypes', 'ContentType')
    content_type = ContentType.objects.get_for_model(Tenant)

    for rule_resource in SecurityGroupRuleResource.objects.exclude(remote_group=None):
        try:
            service_settings = ServiceSettings.objects.get(
                type='OpenStackTenant',
                content_type=content_type,
                object_id=rule_resource.security_group.tenant_id,
            )
            security_group = SecurityGroupProperty.objects.get(
                settings=service_settings,
                backend_id=rule_resource.security_group.backend_id,
            )
            remote_group = SecurityGroupProperty.objects.get(
                settings=service_settings,
                backend_id=rule_resource.remote_group.backend_id,
            )
            rule_property = SecurityGroupRuleProperty.objects.get(
                security_group=security_group, backend_id=rule_resource.backend_id
            )
        except ObjectDoesNotExist:
            continue
        else:
            if rule_property.remote_group != remote_group:
                rule_property.remote_group = remote_group
                rule_property.save(update_fields=['remote_group'])

{'fraction_non_alphanumeric': 0.07497013142174433, 'fraction_numerical': 0.0, 'mean_word_length': 2.93075117370892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5833888', 'n_tokens_mistral': 857, 'n_tokens_neox': 840, 'n_words': 127}