<commit_before>from functools import partial
from django.contrib import messages

from django.contrib.auth import logout
from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.tokens import default_token_generator
from django.contrib.auth.views import password_reset, password_reset_done
from django.http.response import HttpResponseRedirect, HttpResponse, Http404
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_protect
from django.views.generic import View, CreateView
from django.core.urlresolvers import reverse
from django.views.generic.base import TemplateView

from accounts.forms import (
    UserForm, InstructorForm, ChangePasswordForm,
    DeleteAccountForm, ChangeEmailForm,
    CreatePasswordForm)
from accounts.models import Instructor
from ctms.views import json_response
from mysite.mixins import LoginRequiredMixin, NotAnonymousRequiredMixin
from .forms import SocialForm
from psa.custom_backends import EmailAuth
from psa.custom_django_storage import CustomDjangoStorage, CustomCode
from psa.custom_django_strategy import CustomDjangoStrategy


class AccountSettingsView(NotAnonymousRequiredMixin, TemplateView):
    template_name = 'accounts/settings.html'

    def get_instructor(self):
        try:
            return self.request.user.instructor
        except Instructor.DoesNotExist:
            return

    def get_password_form_cls(self):
        return ChangePasswordForm if self.request.user.has_usable_password() else CreatePasswordForm

    def get(self, request):
        instructor = self.get_instructor()
        return self.render_to_response(
            dict(
                user_form=UserForm(instance=request.user),
                instructor_form=InstructorForm(instance=instructor),
                password_form=self.get_password_form_cls()(instance=request.user),
                delete_account_form=DeleteAccountForm(instance=request.user),
                email_form=ChangeEmailForm(initial={'email': request.user.email}),
                person=request.user
            )
        )

    def post(self, request):
        instructor = self.get_instructor()
        form_name = {
            'user_form': partial(UserForm, instance=request.user),
            'instructor_form': partial(InstructorForm, instance=instructor),
            'email_form': partial(ChangeEmailForm, initial={'email': request.user.email}),
            'password_form': partial(self.get_password_form_cls(), instance=request.user),
            'delete_account_form': partial(DeleteAccountForm, instance=request.user),
        }

        form_save_part = {
            'user_form': lambda form_obj: partial(form_obj.save),
            'instructor_form': lambda form_obj: partial(form_obj.save),
            'email_form': lambda form_obj: partial(form_obj.save, request, commit=False),
            'password_form': lambda form_obj: partial(form_obj.save),
            'delete_account_form': lambda form_obj: partial(form_obj.save),
        }
        kwargs = {}
        has_errors = False

        def get_form_changed_data(form):
            data = form.changed_data
            if 'form_id' in form.changed_data:
                data.pop(data.index('form_id'))
            return data

        import ipdb; ipdb.set_trace()
        for form_id, form_cls in form_name.items():
            if form_id in request.POST.getlist('form_id'):
                form = form_cls(data=request.POST)
                changed_data = get_form_changed_data(form)
                if form.is_valid() and changed_data:
                    save = form_save_part[form_id](form)
                    rsp = save()
                    if form_id == 'email_form':
                        return rsp
                elif changed_data:
                    has_errors = True
                kwargs[form_id] = form
            else:
                kwargs[form_id] = form_cls()

        kwargs['person'] = request.user
        if not has_errors:
            return HttpResponseRedirect(reverse('accounts:settings'))
        else:
            messages.add_message(request, messages.WARNING, "Please correct errors below")
        return self.render_to_response(<commit_msg>fix(pdb): Remove pdb breakpoint<commit_after>from functools import partial
from django.contrib import messages

from django.contrib.auth import logout
from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.tokens import default_token_generator
from django.contrib.auth.views import password_reset, password_reset_done
from django.http.response import HttpResponseRedirect, HttpResponse, Http404
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_protect
from django.views.generic import View, CreateView
from django.core.urlresolvers import reverse
from django.views.generic.base import TemplateView

from accounts.forms import (
    UserForm, InstructorForm, ChangePasswordForm,
    DeleteAccountForm, ChangeEmailForm,
    CreatePasswordForm)
from accounts.models import Instructor
from ctms.views import json_response
from mysite.mixins import LoginRequiredMixin, NotAnonymousRequiredMixin
from .forms import SocialForm
from psa.custom_backends import EmailAuth
from psa.custom_django_storage import CustomDjangoStorage, CustomCode
from psa.custom_django_strategy import CustomDjangoStrategy


class AccountSettingsView(NotAnonymousRequiredMixin, TemplateView):
    template_name = 'accounts/settings.html'

    def get_instructor(self):
        try:
            return self.request.user.instructor
        except Instructor.DoesNotExist:
            return

    def get_password_form_cls(self):
        return ChangePasswordForm if self.request.user.has_usable_password() else CreatePasswordForm

    def get(self, request):
        instructor = self.get_instructor()
        return self.render_to_response(
            dict(
                user_form=UserForm(instance=request.user),
                instructor_form=InstructorForm(instance=instructor),
                password_form=self.get_password_form_cls()(instance=request.user),
                delete_account_form=DeleteAccountForm(instance=request.user),
                email_form=ChangeEmailForm(initial={'email': request.user.email}),
                person=request.user
            )
        )

    def post(self, request):
        instructor = self.get_instructor()
        form_name = {
            'user_form': partial(UserForm, instance=request.user),
            'instructor_form': partial(InstructorForm, instance=instructor),
            'email_form': partial(ChangeEmailForm, initial={'email': request.user.email}),
            'password_form': partial(self.get_password_form_cls(), instance=request.user),
            'delete_account_form': partial(DeleteAccountForm, instance=request.user),
        }

        form_save_part = {
            'user_form': lambda form_obj: partial(form_obj.save),
            'instructor_form': lambda form_obj: partial(form_obj.save),
            'email_form': lambda form_obj: partial(form_obj.save, request, commit=False),
            'password_form': lambda form_obj: partial(form_obj.save),
            'delete_account_form': lambda form_obj: partial(form_obj.save),
        }
        kwargs = {}
        has_errors = False

        def get_form_changed_data(form):
            data = form.changed_data
            if 'form_id' in form.changed_data:
                data.pop(data.index('form_id'))
            return data

        for form_id, form_cls in form_name.items():
            if form_id in request.POST.getlist('form_id'):
                form = form_cls(data=request.POST)
                changed_data = get_form_changed_data(form)
                if form.is_valid() and changed_data:
                    save = form_save_part[form_id](form)
                    rsp = save()
                    if form_id == 'email_form':
                        return rsp
                elif changed_data:
                    has_errors = True
                kwargs[form_id] = form
            else:
                kwargs[form_id] = form_cls()

        kwargs['person'] = request.user
        if not has_errors:
            return HttpResponseRedirect(reverse('accounts:settings'))
        else:
            messages.add_message(request, messages.WARNING, "Please correct errors below")
        return self.render_to_response(
{'fraction_non_alphanumeric': 0.07979606355228835, 'fraction_numerical': 0.0007114062129475931, 'mean_word_length': 2.8132911392405062, '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': '4406333', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2195, 'n_words': 481}