<commit_before>from django.views.generic import CreateView, UpdateView, FormView, ListView
from django.core.urlresolvers import reverse_lazy, reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login as auth_login, update_session_auth_hash
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.contrib import messages

from braces.views import LoginRequiredMixin

from .forms import RegisterForm, UpdateUserForm, CrispyPasswordForm, \
    CrispyAuthForm
from .models import ForumUser
from forum.models import ThreadCession


class Register(CreateView):
    form_class = RegisterForm
    template_name = 'registration/register.html'
    success_url = reverse_lazy('user:login')


class Login(FormView):
    form_class = CrispyAuthForm
    template_name = 'registration/login.html'
    success_url = reverse_lazy('forum:top')

    def dispatch(self, request, *args, **kwargs):
        request.session.set_test_cookie()
        return super().dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        auth_login(self.request, form.get_user())
        if self.request.session.test_cookie_worked():
            self.request.session.delete_test_cookie()
        return super().form_valid(form)

    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data(form=form))


class UpdateUser(LoginRequiredMixin, UpdateView):
    form_class = UpdateUserForm
    template_name = 'user/profile.html'

    def get_object(self):
        return ForumUser.objects.get(username=self.request.user)

    def get_form_kwargs(self):
        """Pass user to form."""
        kwargs = super().get_form_kwargs()
        kwargs.update({'user': self.request.user})
        return kwargs

    def form_valid(self, form):
        print(form.cleaned_data.get('logo'))
        token = form.cleaned_data.get('token')
        if token:
            obj = ThreadCession.objects.get(token=token)
            t, p = obj.thread, obj.thread.posts.first()
            t.author, p.author = self.request.user, self.request.user
            t.save()
            p.save()
            obj.delete()
        return super().form_valid(form)


    def get_success_url(self):
        messages.success(self.request, "Paramètres sauvegardés.")
        return reverse('user:profile', kwargs=self.kwargs)


@login_required
def UpdatePassword(request):
    form = CrispyPasswordForm(user=request.user)
<commit_msg>Removing/changing its user logo now deletes the previous logo (if any) from the server.<commit_after>from django.views.generic import CreateView, UpdateView, FormView, ListView
from django.core.urlresolvers import reverse_lazy, reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login as auth_login, update_session_auth_hash
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.contrib import messages

from braces.views import LoginRequiredMixin

from .forms import RegisterForm, UpdateUserForm, CrispyPasswordForm, \
    CrispyAuthForm
from .models import ForumUser
from forum.models import ThreadCession


class Register(CreateView):
    form_class = RegisterForm
    template_name = 'registration/register.html'
    success_url = reverse_lazy('user:login')


class Login(FormView):
    form_class = CrispyAuthForm
    template_name = 'registration/login.html'
    success_url = reverse_lazy('forum:top')

    def dispatch(self, request, *args, **kwargs):
        request.session.set_test_cookie()
        return super().dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        auth_login(self.request, form.get_user())
        if self.request.session.test_cookie_worked():
            self.request.session.delete_test_cookie()
        return super().form_valid(form)

    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data(form=form))


class UpdateUser(LoginRequiredMixin, UpdateView):
    form_class = UpdateUserForm
    template_name = 'user/profile.html'

    def get_object(self):
        return ForumUser.objects.get(username=self.request.user)

    def get_form_kwargs(self):
        """Pass user to form."""
        kwargs = super().get_form_kwargs()
        kwargs.update({'user': self.request.user})
        return kwargs

    def form_valid(self, form):
        # Take care of deleting logo on the server if modified
        if form.cleaned_data.get('logo') != self.request.user.logo:
            self.request.user.logo.delete()
        # Change thread owner if a token has been entered
        token = form.cleaned_data.get('token')
        if token:
            obj = ThreadCession.objects.get(token=token)
            t, p = obj.thread, obj.thread.posts.first()
            t.author, p.author = self.request.user, self.request.user
            t.save()
            p.save()
            obj.delete()
        return super().form_valid(form)


    def get_success_url(self):
        messages.success(self.request, "Paramètres sauvegardés.")
        return reverse('user:profile', kwargs=self.kwargs)


@login_required
def UpdatePassword(request):
    form = CrispyPasswordForm(user=request.user)

{'fraction_non_alphanumeric': 0.09862909367859862, 'fraction_numerical': 0.0, 'mean_word_length': 3.497431506849315, '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': '1734486', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1482, 'n_words': 343}