<commit_before>from datetime import datetime
from copy import copy

from markdown2 import markdown

from django.views import generic
from django.http import HttpResponseRedirect, JsonResponse, Http404
from django.template.response import TemplateResponse
from django.views.decorators.vary import vary_on_headers
from django.conf import settings
from django.contrib import messages
from django.contrib.flatpages.models import FlatPage
from django.contrib.auth import get_user_model, authenticate, login
from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.core.mail import send_mail
from django.template.loader import get_template
from django.template import Context
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _

from blog.models import Post
from hosting.models import Place, Profile
from .auth import AuthMixin, ADMIN, SUPERVISOR, OWNER
from hosting.mixins import ProfileIsUserMixin, ProfileModifyMixin
from .mixins import LoginRequiredMixin, UserModifyMixin
from .forms import (
    UsernameUpdateForm, EmailUpdateForm, EmailStaffUpdateForm,
    MassMailForm, UserRegistrationForm
)
from hosting.utils import value_without_invalid_marker, format_lazy
from links.utils import create_unique_url
from .models import SiteConfiguration
from .utils import send_mass_html_mail

User = get_user_model()


class HomeView(generic.TemplateView):
    template_name = 'core/home.html'

    def news(self):
        return Post.objects.published(3).defer('content', 'body')

    def right_block(self):
        return FlatPage.objects.filter(url='/home-right-block/').values('content').first()

home = HomeView.as_view()


class RegisterView(generic.CreateView):
    model = User
    template_name = 'registration/register.html'
    form_class = UserRegistrationForm
    success_url = reverse_lazy('profile_create')

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            # Only anonymous (non-authenticated) users should access the registration page.
            return HttpResponseRedirect(self.get_authenticated_redirect_url())
        return super().dispatch(request, *args, **kwargs)

    def get_authenticated_redirect_url(self):
        if self.request.GET.get(settings.REDIRECT_FIELD_NAME):
            return self.request.GET[settings.REDIRECT_FIELD_NAME]
        try:
            # When user is already authenticated, redirect to profile edit page.
            return self.request.user.profile.get_edit_url()
        except Profile.DoesNotExist:
            # If profile does not exist yet, redirect to profile creation page.
            return self.success_url

    def form_valid(self, form):
        self.object = form.save()
        # Keeping this on ice; it interferes with the inline login, probably by wiping the session vars.
        result = super().form_valid(form)
        # Log in user.
        user = authenticate(
            username=form.cleaned_data['username'],
            password=form.cleaned_data['<PASSWORD>'])
        login(self.request, user)
        messages.success(self.request, _("You are logged in."))
        return result

register = RegisterView.as_view()


class UsernameChangeView(LoginRequiredMixin, UserModifyMixin, generic.UpdateView):
    model = User
    template_name = 'core/username_change_form.html'
    form_class = UsernameUpdateForm

    def get_object(self, queryset=None):
        self.original_username = self.request.user.username
        return self.request.user

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Avoid replacement of displayed username via template context when provided value is invalid.
        context['user'].username = self.original_username
        return context

username_change = UsernameChangeView.as_view()


class EmailUpdateView(AuthMixin, UserModifyMixin, generic.UpdateView):
    model = User
    template_name = 'core/system-email_form.html'
    form_class = EmailUpdateForm
    exact_role = OWNER

    def dispatch(self, request, *args, **kwargs):
        if not hasattr(self, 'user'):
            self.user = self.request.user
            self.kwargs[self.pk_url_kwarg] = self.user.id
        return super().dispatch(request, *args, **kwargs)

    def get_object(self, queryset=None):
        super().get_object(queryset)
        return self.user

    def get_owner(self, object):
        return self

    def form_valid(self, form):
        response = super().form_valid(form)
        if form.previous_email != form.instance.email:
            messages.warning(self.request, extra_tags='eminent',
                             message=_("A confirmation email has been sent. "
                                       "Please check your mailbox to complete the process."))
        return response

email_update = EmailUpdateView.as_view()

<commit_msg>Fixed EmailUpdateView<commit_after>from datetime import datetime
from copy import copy

from markdown2 import markdown

from django.views import generic
from django.http import HttpResponseRedirect, JsonResponse, Http404
from django.template.response import TemplateResponse
from django.views.decorators.vary import vary_on_headers
from django.conf import settings
from django.contrib import messages
from django.contrib.flatpages.models import FlatPage
from django.contrib.auth import get_user_model, authenticate, login
from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.core.mail import send_mail
from django.template.loader import get_template
from django.template import Context
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _

from blog.models import Post
from hosting.models import Place, Profile
from .auth import AuthMixin, ADMIN, SUPERVISOR, OWNER
from hosting.mixins import ProfileIsUserMixin, ProfileModifyMixin
from .mixins import LoginRequiredMixin, UserModifyMixin
from .forms import (
    UsernameUpdateForm, EmailUpdateForm, EmailStaffUpdateForm,
    MassMailForm, UserRegistrationForm
)
from hosting.utils import value_without_invalid_marker, format_lazy
from links.utils import create_unique_url
from .models import SiteConfiguration
from .utils import send_mass_html_mail

User = get_user_model()


class HomeView(generic.TemplateView):
    template_name = 'core/home.html'

    def news(self):
        return Post.objects.published(3).defer('content', 'body')

    def right_block(self):
        return FlatPage.objects.filter(url='/home-right-block/').values('content').first()

home = HomeView.as_view()


class RegisterView(generic.CreateView):
    model = User
    template_name = 'registration/register.html'
    form_class = UserRegistrationForm
    success_url = reverse_lazy('profile_create')

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            # Only anonymous (non-authenticated) users should access the registration page.
            return HttpResponseRedirect(self.get_authenticated_redirect_url())
        return super().dispatch(request, *args, **kwargs)

    def get_authenticated_redirect_url(self):
        if self.request.GET.get(settings.REDIRECT_FIELD_NAME):
            return self.request.GET[settings.REDIRECT_FIELD_NAME]
        try:
            # When user is already authenticated, redirect to profile edit page.
            return self.request.user.profile.get_edit_url()
        except Profile.DoesNotExist:
            # If profile does not exist yet, redirect to profile creation page.
            return self.success_url

    def form_valid(self, form):
        self.object = form.save()
        # Keeping this on ice; it interferes with the inline login, probably by wiping the session vars.
        result = super().form_valid(form)
        # Log in user.
        user = authenticate(
            username=form.cleaned_data['username'],
            password=form.cleaned_data['<PASSWORD>'])
        login(self.request, user)
        messages.success(self.request, _("You are logged in."))
        return result

register = RegisterView.as_view()


class UsernameChangeView(LoginRequiredMixin, UserModifyMixin, generic.UpdateView):
    model = User
    template_name = 'core/username_change_form.html'
    form_class = UsernameUpdateForm

    def get_object(self, queryset=None):
        self.original_username = self.request.user.username
        return self.request.user

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Avoid replacement of displayed username via template context when provided value is invalid.
        context['user'].username = self.original_username
        return context

username_change = UsernameChangeView.as_view()


class EmailUpdateView(AuthMixin, UserModifyMixin, generic.UpdateView):
    model = User
    template_name = 'core/system-email_form.html'
    form_class = EmailUpdateForm
    exact_role = OWNER

    def dispatch(self, request, *args, **kwargs):
        if not hasattr(self, 'user'):
            self.user = self.request.user
            self.kwargs[self.pk_url_kwarg] = self.user.id
        return super().dispatch(request, *args, **kwargs)

    def get_object(self, queryset=None):
        super().get_object(queryset)
        return self.user

    def get_owner(self, object):
        return self.user.profile

    def form_valid(self, form):
        response = super().form_valid(form)
        if form.previous_email != form.instance.email:
            messages.warning(self.request, extra_tags='eminent',
                             message=_("A confirmation email has been sent. "
                                       "Please check your mailbox to complete the process."))
        return response

email_update = EmailUpdateView.as_view()


{'fraction_non_alphanumeric': 0.08054486230381996, 'fraction_numerical': 0.0015793110255650973, 'mean_word_length': 3.547576301615799, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6665933', 'n_tokens_mistral': 2778, 'n_tokens_neox': 2698, 'n_words': 747}