<commit_before># -*- coding: utf-8 -*-
# This file is part of Shoop.
#
# Copyright (c) 2012-2015, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.


def cache_translations(objects, languages=None):
    """
    Cache translation objects in given languages to the objects in one fell swoop.
    This will iterate a queryset, if one is passed!
    :param objects: List or queryset of Translatable models
    :param languages: Iterable of languages to fetch. In addition, all "_current_language"s will be fetched
    :return: objects
    """
    if not objects:
        return objects
    languages = set(languages or ())
    xlate_model = objects[0]._parler_meta.root_model
    object_map = dict((object.pk, object) for object in objects)
    languages.update(set(object._current_language for object in objects))
    for translation in xlate_model.objects.filter(master_id__in=object_map.keys(), language_code__in=languages):
        master = object_map[translation.master_id]
        master._translations_cache[translation.language_code] = translation
        setattr(translation, translation.__class__.master.cache_name, master)
    return objects<commit_msg>Fix `cache_translations` to work with new Parler<commit_after># -*- coding: utf-8 -*-
# This file is part of Shoop.
#
# Copyright (c) 2012-2015, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.


def cache_translations(objects, languages=None, meta=None):
    """
    Cache translation objects in given languages to the objects in one fell swoop.
    This will iterate a queryset, if one is passed!

    :param objects: List or queryset of Translatable models
    :param languages: Iterable of languages to fetch. In addition, all "_current_language"s will be fetched
    :return: objects
    """
    if not objects:
        return objects
    languages = set(languages or ())
    if meta is None:
        meta = objects[0]._parler_meta.root  # work on base model by default
    xlate_model = meta.model

    object_map = dict((object.pk, object) for object in objects)
    languages.update(set(object._current_language for object in objects))
    master_ids = object_map.keys()
    for translation in xlate_model.objects.filter(master_id__in=master_ids, language_code__in=languages):
        master = object_map[translation.master_id]
        master._translations_cache[xlate_model][translation.language_code] = translation
        setattr(translation, translation.__class__.master.cache_name, master)
    return objects
{'fraction_non_alphanumeric': 0.07880133185349611, 'fraction_numerical': 0.008139104698483167, 'mean_word_length': 3.872072072072072, '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': '2730449', 'n_tokens_mistral': 760, 'n_tokens_neox': 743, 'n_words': 268}