<commit_before># flake8: noqa

from django.core.exceptions import ImproperlyConfigured
from django.db import transaction
from django.forms import models as model_forms
from django.forms.formsets import all_valid
from django.http import HttpResponseRedirect
from django.utils.encoding import force_text
from django.views import generic


class MultiFormMixin(generic.base.ContextMixin):
    forms = {}
    success_url = None
    prefix = None

    def forms_invalid(self, forms):
        """
        If any form is invalid, re-render the context data with the
        data-filled forms and errors.
        """
        return self.render_to_response(self.get_context_data(forms=forms))

    def forms_valid(self, forms):
        """
        If all forms are valid, redirect to the supplied URL.
        """
        return HttpResponseRedirect(self.get_success_url())

    def get_context_data(self, **kwargs):
        """
        Insert the forms into the context dict.
        """
        if 'forms' not in kwargs:
            kwargs['forms'] = self.get_forms()
        return super().get_context_data(**kwargs)

    def get_forms(self):
        """
        Returns instances of the forms to be used in this view.
        """
        forms = {}
        for name in self.forms.keys():
            form_class = self.get_form_class(name)
            if form_class:
                forms[name] = form_class(**self.get_form_kwargs(name))
        return forms

    def _get_from_name(self, name, key, default=None):
        form = self.forms.get(name)
        if form:
            return form.get(key, default)

    def get_form_class(self, name):
        """
        Returns the form class to be used with the named form.
        """
        return self._get_from_name(name, 'form_class')

    def get_initial(self, name):
        """
        Returns the initial data to use for the named form.
        """
        initial = self._get_from_name(name, 'initial', {})
        return initial.copy()

    def get_prefix(self, name):
        """
        Returns the prefix to use for the named form.
        """
        if self.prefix:
            return '{}_{}'.format(self.prefix, name)
        return name

    def get_form_kwargs(self, name):
        """
        Returns the keyword arguments for instantiating the named form.
        """
        kwargs = {
            'initial': self.get_initial(name),
            'prefix': self.get_prefix(name),
        }
        if self.request.method in ('POST', 'PUT'):
            kwargs.update({
                'data': self.request.POST,
                'files': self.request.FILES,
            })
        return kwargs

    def get_success_url(self):
        """
        Returns the supplied success URL.
        """
        if self.success_url:
            # Forcing possible reverse_lazy evaluation
            url = force_text(self.success_url)
        else:
            raise ImproperlyConfigured(
                "No URL to redirect to. Provide a success_url.")
        return url


class MultiModelFormMixin(MultiFormMixin):
    objects = {}

    def forms_valid(self, forms):
        """
        If all forms are valid, save the associated models.
        """
        self.objects = self.forms_save(forms)
        return super().forms_valid(forms)

    def forms_save(self, forms, commit=True):
        """
        Save all the forms in one transaction.
        """
        objects = {}
        with transaction.atomic():
            for name in self.forms.keys():
                if hasattr(forms[name], 'save'):
                    objects[name] = forms[name].save(commit)
        return objects

    def get_form_class(self, name):
        """
        Returns the form class to be used with the named form.
        """
        fields = self._get_from_name(name, 'fields')
        form_class = self._get_from_name(name, 'form_class')
        model = self._get_from_name(name, 'model')
<commit_msg>Automatically call save_m2m on forms if available<commit_after># flake8: noqa

from django.core.exceptions import ImproperlyConfigured
from django.db import transaction
from django.forms import models as model_forms
from django.forms.formsets import all_valid
from django.http import HttpResponseRedirect
from django.utils.encoding import force_text
from django.views import generic


class MultiFormMixin(generic.base.ContextMixin):
    forms = {}
    success_url = None
    prefix = None

    def forms_invalid(self, forms):
        """
        If any form is invalid, re-render the context data with the
        data-filled forms and errors.
        """
        return self.render_to_response(self.get_context_data(forms=forms))

    def forms_valid(self, forms):
        """
        If all forms are valid, redirect to the supplied URL.
        """
        return HttpResponseRedirect(self.get_success_url())

    def get_context_data(self, **kwargs):
        """
        Insert the forms into the context dict.
        """
        if 'forms' not in kwargs:
            kwargs['forms'] = self.get_forms()
        return super().get_context_data(**kwargs)

    def get_forms(self):
        """
        Returns instances of the forms to be used in this view.
        """
        forms = {}
        for name in self.forms.keys():
            form_class = self.get_form_class(name)
            if form_class:
                forms[name] = form_class(**self.get_form_kwargs(name))
        return forms

    def _get_from_name(self, name, key, default=None):
        form = self.forms.get(name)
        if form:
            return form.get(key, default)

    def get_form_class(self, name):
        """
        Returns the form class to be used with the named form.
        """
        return self._get_from_name(name, 'form_class')

    def get_initial(self, name):
        """
        Returns the initial data to use for the named form.
        """
        initial = self._get_from_name(name, 'initial', {})
        return initial.copy()

    def get_prefix(self, name):
        """
        Returns the prefix to use for the named form.
        """
        if self.prefix:
            return '{}_{}'.format(self.prefix, name)
        return name

    def get_form_kwargs(self, name):
        """
        Returns the keyword arguments for instantiating the named form.
        """
        kwargs = {
            'initial': self.get_initial(name),
            'prefix': self.get_prefix(name),
        }
        if self.request.method in ('POST', 'PUT'):
            kwargs.update({
                'data': self.request.POST,
                'files': self.request.FILES,
            })
        return kwargs

    def get_success_url(self):
        """
        Returns the supplied success URL.
        """
        if self.success_url:
            # Forcing possible reverse_lazy evaluation
            url = force_text(self.success_url)
        else:
            raise ImproperlyConfigured(
                "No URL to redirect to. Provide a success_url.")
        return url


class MultiModelFormMixin(MultiFormMixin):
    objects = {}

    def forms_valid(self, forms):
        """
        If all forms are valid, save the associated models.
        """
        self.objects = self.forms_save(forms)
        self.forms_save_m2m(forms)
        return super().forms_valid(forms)

    def forms_save(self, forms, commit=True):
        """
        Save all the forms in one transaction.
        """
        objects = {}
        with transaction.atomic():
            for name in self.forms.keys():
                if hasattr(forms[name], 'save'):
                    objects[name] = forms[name].save(commit)
        return objects

    def forms_save_m2m(self, forms):
        """
        Calls save_m2m on every form where it is available.
        Has to be called after the forms have been saved.
        """
        for form in forms.values():
            if hasattr(form, 'save_m2m'):
                form.save_m2m()

    def get_form_class(self, name):
        """
        Returns the form class to be used with the named form.
        """
        fields = self._get_from_name(name, 'fields')
        form_class = self._get_from_name(name, 'form_class')
        model = self._get_from_name(name, 'model')

{'fraction_non_alphanumeric': 0.09770881318947751, 'fraction_numerical': 0.0009698145229724816, 'mean_word_length': 2.0308596620132255, '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': '6063935', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2228, 'n_words': 657}