<commit_before>from __future__ import absolute_import, unicode_literals


# Helper functions for migrating the Rendition.filter foreign key to the filter_spec field,
# and the corresponding reverse migration

def get_fill_filter_spec_migrations(app_name, rendition_model_name):

    def fill_filter_spec_forward(apps, schema_editor):
        # Populate Rendition.filter_spec with the spec string of the corresponding Filter object
        Rendition = apps.get_model(app_name, rendition_model_name)
        db_alias = schema_editor.connection.alias
        for rendition in Rendition.objects.using(db_alias).select_related('filter'):
            rendition.filter_spec = rendition.filter.spec
            rendition.save()

    def fill_filter_spec_reverse(apps, schema_editor):
        # Populate the Rendition.filter
        Rendition = apps.get_model(app_name, rendition_model_name)
        Filter = apps.get_model('wagtailimages', 'Filter')
        db_alias = schema_editor.connection.alias

        filters_by_spec = {}
        for rendition in Rendition.objects.using(db_alias):
            try:
                filter = filters_by_spec[rendition.filter_spec]
            except KeyError:
                filter, _ = Filter.objects.get_or_create(spec=rendition.filter_spec)
                filters_by_spec[rendition.filter_spec] = filter

            rendition.filter = filter
            rendition.save()

    return (fill_filter_spec_forward, fill_filter_spec_reverse)<commit_msg>Use a more efficient method to populate Rendition.filter_spec<commit_after>from __future__ import absolute_import, unicode_literals


# Helper functions for migrating the Rendition.filter foreign key to the filter_spec field,
# and the corresponding reverse migration

def get_fill_filter_spec_migrations(app_name, rendition_model_name):

    def fill_filter_spec_forward(apps, schema_editor):
        # Populate Rendition.filter_spec with the spec string of the corresponding Filter object
        Rendition = apps.get_model(app_name, rendition_model_name)
        Filter = apps.get_model('wagtailimages', 'Filter')

        db_alias = schema_editor.connection.alias
        for flt in Filter.objects.using(db_alias):
            renditions = Rendition.objects.using(db_alias).filter(filter=flt, filter_spec__isnull=True)
            renditions.update(filter_spec=flt.spec)

    def fill_filter_spec_reverse(apps, schema_editor):
        # Populate the Rendition.filter
        Rendition = apps.get_model(app_name, rendition_model_name)
        Filter = apps.get_model('wagtailimages', 'Filter')
        db_alias = schema_editor.connection.alias

        filters_by_spec = {}
        for rendition in Rendition.objects.using(db_alias):
            try:
                filter = filters_by_spec[rendition.filter_spec]
            except KeyError:
                filter, _ = Filter.objects.get_or_create(spec=rendition.filter_spec)
                filters_by_spec[rendition.filter_spec] = filter

            rendition.filter = filter
            rendition.save()

    return (fill_filter_spec_forward, fill_filter_spec_reverse)
{'fraction_non_alphanumeric': 0.06874397687118536, 'fraction_numerical': 0.0, 'mean_word_length': 3.3370473537604455, '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': '338805', 'n_tokens_mistral': 893, 'n_tokens_neox': 861, 'n_words': 180}