<commit_before>from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator


def merge_dicts(x, y):
    """
    Given two dicts, merge them into a new dict as a shallow copy.
    """
    z = x.copy()
    z.update(y)
    return z


class LoginMixin(object):
    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(LoginMixin, self).dispatch(request, *args, **kwargs)


class MultipleFormView(TemplateView):
    """
    View mixin that handles multiple forms / formsets.
    After the successful data is inserted ``self.forms_valid`` is called.
    """
    form_classes = {}

    def initialize_forms(self, data=None):
        return {name: form(prefix=name, data=data)
                for name, form in self.form_classes.items()}

    def get_context_data(self, **kwargs):
        context = super(MultipleFormView, self).get_context_data(**kwargs)
        return merge_dicts(context, self.initialize_forms())

    def post(self, request):
        forms_initialized = self.initialize_forms(data=request.POST)
        valid = all([form_class.is_valid()
                     for name, form_class in forms_initialized.items()])
        if valid:
            return self.forms_valid(forms_initialized)
        else:
            context = merge_dicts(self.get_context_data(), forms_initialized)
            return self.render_to_response(context)

    def forms_valid(self, form_instances):
        raise NotImplementedError('Implement this in your view!')<commit_msg>code smell: Unused variable 'name'<commit_after>from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator


def merge_dicts(x, y):
    """
    Given two dicts, merge them into a new dict as a shallow copy.
    """
    z = x.copy()
    z.update(y)
    return z


class LoginMixin(object):
    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(LoginMixin, self).dispatch(request, *args, **kwargs)


class MultipleFormView(TemplateView):
    """
    View mixin that handles multiple forms / formsets.
    After the successful data is inserted ``self.forms_valid`` is called.
    """
    form_classes = {}

    def initialize_forms(self, data=None):
        return {name: form(prefix=name, data=data)
                for name, form in self.form_classes.items()}

    def get_context_data(self, **kwargs):
        context = super(MultipleFormView, self).get_context_data(**kwargs)
        return merge_dicts(context, self.initialize_forms())

    def post(self, request):
        forms_initialized = self.initialize_forms(data=request.POST)
        valid = all([form.is_valid() for form in forms_initialized.values()])
        
        if valid:
            return self.forms_valid(forms_initialized)
        else:
            context = merge_dicts(self.get_context_data(), forms_initialized)
            return self.render_to_response(context)

    def forms_valid(self, form_instances):
        raise NotImplementedError('Implement this in your view!')
{'fraction_non_alphanumeric': 0.10049627791563276, 'fraction_numerical': 0.0, 'mean_word_length': 3.3114973262032086, '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': '1263128', 'n_tokens_mistral': 922, 'n_tokens_neox': 912, 'n_words': 236}