<commit_before>from openbudget.settings import base as settings


def get_language_key(host, domain, user):
    """Invoked in middleware to customize request.LANGUAGE_CODE

    A language key is returned according to the following logic:

    * If user is authenticated, set language to the user's language preference, no matter what the domain.
    * If user is anonymous, get the language key from the domain (we are using django-subdomains for language
        specific domains)
    * If the domain doesn't have an explicit language key we return the default language.
    """
    # Get lang from authenticated user
    if not user.is_anonymous():
        value = user.language

    # Get lang based on request host and global language settings
    else:
        current_subdomain = host[:-len(domain) - 1]
        default_language = settings.LANGUAGE_CODE
        valid_languages = [l[0] for l in settings.LANGUAGES]
        valid_subdomains = list(settings.SUBDOMAIN_URLCONFS)
        default_language_domains = []

        for d in valid_subdomains:
            if (d is default_language) or (d not in valid_languages):
                default_language_domains.append(d)

        if current_subdomain in default_language_domains:
            value = default_language
        else:
            value = current_subdomain

    return value


def translated_fields(*args, **kwargs):
    """Given field names, returns a list of related translated field names.

    TODO: use the translation registry files to *ensure* that the passed field
     is actually registered for translation, using model=None kwarg.
    """

    target_codes = [lang[0] for lang in settings.LANGUAGES]
    target_codes.remove(settings.MODELTRANSLATION_DEFAULT_LANGUAGE)

    value = []
    for field in args:
        value.extend([field + '_' + code for code in target_codes])

    return value
<commit_msg>Refactored translated_fields to take a model, rather than field names as arguments.<commit_after>from modeltranslation.translator import translator
from openbudget.settings import base as settings


def get_language_key(host, domain, user):
    """Invoked in middleware to customize request.LANGUAGE_CODE

    A language key is returned according to the following logic:

    * If user is authenticated, set language to the user's language preference, no matter what the domain.
    * If user is anonymous, get the language key from the domain (we are using django-subdomains for language
        specific domains)
    * If the domain doesn't have an explicit language key we return the default language.
    """
    # Get lang from authenticated user
    if not user.is_anonymous():
        value = user.language

    # Get lang based on request host and global language settings
    else:
        current_subdomain = host[:-len(domain) - 1]
        default_language = settings.LANGUAGE_CODE
        valid_languages = [l[0] for l in settings.LANGUAGES]
        valid_subdomains = list(settings.SUBDOMAIN_URLCONFS)
        default_language_domains = []

        for d in valid_subdomains:
            if (d is default_language) or (d not in valid_languages):
                default_language_domains.append(d)

        if current_subdomain in default_language_domains:
            value = default_language
        else:
            value = current_subdomain

    return value


def translated_fields(model):
    """Given a model, returns a list of translated field names for it.

    The returned list excludes the extra field created for the default language.

    """

    options = translator.get_options_for_model(model)
    fields = [f.name for l in options.fields.values() for f in l]

    for i, f in enumerate(fields):
        if f.endswith(settings.MODELTRANSLATION_DEFAULT_LANGUAGE):
            del fields[i]

    return fields

{'fraction_non_alphanumeric': 0.06068532566047607, 'fraction_numerical': 0.0013078733978550876, 'mean_word_length': 3.0380147835269273, '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': '4728270', 'n_tokens_mistral': 1037, 'n_tokens_neox': 978, 'n_words': 388}