<commit_before>from django.http import Http404
from django.shortcuts import render, redirect
from django.views.generic.edit import CreateView, UpdateView, FormView
from django.views.generic import DetailView, View
from django.contrib import messages
from django.shortcuts import get_object_or_404
from .models import User
from tag.models import Tag
from base.utils import send_template_mail
from django.contrib.auth import views as auth_views
import uuid
from django.contrib.auth import login

from .models import UserActivation, UserPasswordResetting
from .form import UserReviewListForm
from django.urls import reverse

from django.utils import translation
from django.conf import settings


class UserCreateView(CreateView):
    model = User
    fields = ['email', 'password', 'username']
    template_name = 'user/register.html'

    def form_valid(self, form):
        user = form.save(commit=False)
        user.set_password(form.cleaned_data['password'])
        user.is_active = False
        user.save()

        activation_key = self.create_activation_key()
        activation = UserActivation(user=user, key=activation_key)
        activation.save()

        base_url = "/".join(self.request.build_absolute_uri().split("/")[:3])

        activation_url = "{0}/user/activation/{1}".format(base_url,
                                                          activation_key)

        send_template_mail(
            "email/activation.txt",
            {"activation_url": activation_url},
            "Sovol Info <<EMAIL>>",
            [user.email],
        )

        info_msg = "記入したメールアドレス%(email)sに確認メールを送信しました。" % {
            'email': user.email,
        }
        messages.info(self.request, info_msg)
        return redirect("top")

    def create_activation_key(self):
        key = uuid.uuid4().hex
        return key

    def get_form(self):
        from django import forms
        form = super().get_form()
        form.fields['password'].widget = forms.PasswordInput()
        form.fields['username'].maxlength = 15
        form.fields['username'].label = "ニックネーム（15文字以内)"
        return form


class UserActivationView(View):
    def get(self, request, *args, **kwargs):
        activation = get_object_or_404(UserActivation, key=kwargs['key'])
        user = activation.user
        user.is_active = True
        user.save()
        login(request, user, "django.contrib.auth.backends.ModelBackend")
        messages.info(request, "本登録が完了しました。")
        return redirect("top")


class RequestPasswordReset(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'user/request_password_reset.html')

    def post(self, request, *args, **kwargs):
        email = request.POST.get('email')
        user = User.objects.filter(email=email)
        if user.exists():
            user = user.first()
            reset_key = self.create_reset_key()

            if hasattr(user, "userpasswordresetting"):
                user.userpasswordresetting.key = reset_key
                user.userpasswordresetting.save()
            else:
                UserPasswordResetting(user=user, key=reset_key).save()

            base_url = "/".join(self.request.build_absolute_uri().split("/")[:3])
            reset_url = "{0}/user/reset_password/{1}".format(base_url, reset_key)

            send_template_mail(
                "email/reset_password.txt",
                {"reset_url": reset_url},
                "Sovol Info<<EMAIL>>",
                [user.email]
            )

        messages.info(request, "リクエストを受け付けました。メールアドレスが登録されている場合、アドレスにパスワード再設定のリンクが送信されます。")
        return redirect("top")

    def create_reset_key(self):
        key = uuid.uuid4().hex
        return key


class ResetPassword(View):
    def get(self, request, *args, **kwargs):<commit_msg>[COSME] Add 'absolute_uri' intermediate variable<commit_after>from django.http import Http404
from django.shortcuts import render, redirect
from django.views.generic.edit import CreateView, UpdateView, FormView
from django.views.generic import DetailView, View
from django.contrib import messages
from django.shortcuts import get_object_or_404
from .models import User
from tag.models import Tag
from base.utils import send_template_mail
from django.contrib.auth import views as auth_views
import uuid
from django.contrib.auth import login

from .models import UserActivation, UserPasswordResetting
from .form import UserReviewListForm
from django.urls import reverse

from django.utils import translation
from django.conf import settings


class UserCreateView(CreateView):
    model = User
    fields = ['email', 'password', 'username']
    template_name = 'user/register.html'

    def form_valid(self, form):
        user = form.save(commit=False)
        user.set_password(form.cleaned_data['password'])
        user.is_active = False
        user.save()

        activation_key = self.create_activation_key()
        activation = UserActivation(user=user, key=activation_key)
        activation.save()

        base_url = "/".join(self.request.build_absolute_uri().split("/")[:3])

        activation_url = "{0}/user/activation/{1}".format(base_url,
                                                          activation_key)

        send_template_mail(
            "email/activation.txt",
            {"activation_url": activation_url},
            "Sovol Info <<EMAIL>>",
            [user.email],
        )

        info_msg = "記入したメールアドレス%(email)sに確認メールを送信しました。" % {
            'email': user.email,
        }
        messages.info(self.request, info_msg)
        return redirect("top")

    def create_activation_key(self):
        key = uuid.uuid4().hex
        return key

    def get_form(self):
        from django import forms
        form = super().get_form()
        form.fields['password'].widget = forms.PasswordInput()
        form.fields['username'].maxlength = 15
        form.fields['username'].label = "ニックネーム（15文字以内)"
        return form


class UserActivationView(View):
    def get(self, request, *args, **kwargs):
        activation = get_object_or_404(UserActivation, key=kwargs['key'])
        user = activation.user
        user.is_active = True
        user.save()
        login(request, user, "django.contrib.auth.backends.ModelBackend")
        messages.info(request, "本登録が完了しました。")
        return redirect("top")


class RequestPasswordReset(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'user/request_password_reset.html')

    def post(self, request, *args, **kwargs):
        email = request.POST.get('email')
        user = User.objects.filter(email=email)
        if user.exists():
            user = user.first()
            reset_key = self.create_reset_key()

            if hasattr(user, "userpasswordresetting"):
                user.userpasswordresetting.key = reset_key
                user.userpasswordresetting.save()
            else:
                UserPasswordResetting(user=user, key=reset_key).save()

            # XXX: What does 3 mean?
            # XXX: os.path?
            absolute_uri = self.request.build_absolute_uri()
            base_url = "/".join(absolute_uri.split("/")[:3])

            reset_url = "{0}/user/reset_password/{1}".format(base_url, reset_key)

            send_template_mail(
                "email/reset_password.txt",
                {"reset_url": reset_url},
                "Sovol Info<<EMAIL>>",
                [user.email]
            )

        messages.info(request, "リクエストを受け付けました。メールアドレスが登録されている場合、アドレスにパスワード再設定のリンクが送信されます。")
        return redirect("top")

    def create_reset_key(self):
        key = uuid.uuid4().hex
        return key


class ResetPassword(View):
    def get(self, request, *args, **kwargs):
{'fraction_non_alphanumeric': 0.11124016002064782, 'fraction_numerical': 0.00554910311007872, 'mean_word_length': 2.602975360297536, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1827059', 'n_tokens_mistral': 2340, 'n_tokens_neox': 2250, 'n_words': 464}