<commit_before>from django import forms
from django.forms.models import BaseInlineFormSet
from models import Language

class BaseTranslationFormSet(BaseInlineFormSet):
    """
    This implements the default formset that should be used with translatable objects.
    """
    def __init__(self, *args, **kwargs):
        #calculate a queryset that retrieves all non-translated languages
        #for this instance 
        if 'instance' in kwargs and kwargs['instance']:
            queryset = Language.objects.exclude(
                name__in=kwargs['instance'].translations.values_list(
                    'language_id', flat=True)).values_list(
                        'name', flat=True)
        else:
            queryset = Language.objects.values_list('name', flat=True)

        #provide initial data based on untranslated languages
        kwargs['initial'] = [ {'language' : x} for x in queryset]
        #should not allow more inlines than that of the number of languages
        self.max_num = Language.objects.count()

        super(BaseTranslationFormSet, self).__init__(*args, **kwargs)
        
class PoFileForm(forms.Form):
    po_content = forms.CharField(widget=forms.Textarea(attrs={'class' : 'textarea-full'}))
<commit_msg>avoid extra query when counting available languages<commit_after>from django import forms
from django.forms.models import BaseInlineFormSet
from models import Language
from utils import get_supported_languages

class BaseTranslationFormSet(BaseInlineFormSet):
    """
    This implements the default formset that should be used with translatable objects.
    """
    def __init__(self, *args, **kwargs):
        #calculate a queryset that retrieves all non-translated languages
        #for this instance 
        if 'instance' in kwargs and kwargs['instance']:
            queryset = Language.objects.exclude(
                name__in=kwargs['instance'].translations.values_list(
                    'language_id', flat=True)).values_list(
                        'name', flat=True)
        else:
            queryset = Language.objects.values_list('name', flat=True)

        #provide initial data based on untranslated languages
        kwargs['initial'] = [ {'language' : x} for x in queryset]
        #should not allow more inlines than that of the number of languages
        self.max_num = len(get_supported_languages())

        super(BaseTranslationFormSet, self).__init__(*args, **kwargs)
        
class PoFileForm(forms.Form):
    po_content = forms.CharField(widget=forms.Textarea(attrs={'class' : 'textarea-full'}))

{'fraction_non_alphanumeric': 0.09253499222395023, 'fraction_numerical': 0.0, 'mean_word_length': 3.0647709320695102, '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': '474987', 'n_tokens_mistral': 669, 'n_tokens_neox': 656, 'n_words': 198}