Passed
Push — master ( 0b279a...518a13 )
by Alexander
02:26
created

tcms.kiwi_auth.forms.PasswordResetForm.save()   A

Complexity

Conditions 1

Size

Total Lines 18
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 18
rs 9.55
c 0
b 0
f 0
cc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
# -*- coding: utf-8 -*-
2
from django import forms
3
from django.urls import reverse
4
from django.conf import settings
5
from django.contrib.sites.models import Site
6
from django.contrib.auth import get_user_model
7
from django.contrib.auth.forms import UserCreationForm
8
from django.contrib.auth.tokens import default_token_generator
9
from django.contrib.auth.forms import PasswordResetForm as DjangoPasswordResetForm
10
from django.utils.translation import ugettext_lazy as _
11
12
from tcms.core.utils import request_host_link
13
from tcms.core.utils.mailto import mailto
14
from tcms.kiwi_auth.models import UserActivationKey
15
from tcms.utils.permissions import initiate_user_with_default_setups
16
17
18
User = get_user_model()  # pylint: disable=invalid-name
19
20
21
class RegistrationForm(UserCreationForm):
22
    email = forms.EmailField()
23
24
    class Meta:
25
        model = User
26
        fields = ("username",)
27
28
    def clean_email(self):
29
        email = self.cleaned_data['email']
30
        try:
31
            User.objects.get(email=email)
32
        except User.DoesNotExist:
33
            return email
34
        raise forms.ValidationError(
35
            _("A user with that email already exists."))
36
37
    def save(self, commit=True):
38
        user = super(RegistrationForm, self).save(commit=False)
39
        user.email = self.cleaned_data['email']
40
        user.is_active = False
41
        user.set_password(self.cleaned_data["password1"])
42
43
        if User.objects.filter(is_superuser=True).count() == 0:
44
            user.is_superuser = True
45
46
        if commit:
47
            user.save()
48
            initiate_user_with_default_setups(user)
49
        return user
50
51
    def set_activation_key(self):
52
        return UserActivationKey.set_random_key_for_user(user=self.instance)
53
54
    def send_confirm_mail(self, request, activation_key):
55
        current_site = Site.objects.get(pk=settings.SITE_ID)
56
        confirm_url = '%s%s' % (
57
            request_host_link(request, current_site.domain),
58
            reverse('tcms-confirm',
59
                    args=[activation_key.activation_key, ])
60
        )
61
        mailto(
62
            template_name='email/confirm_registration.txt', recipients=self.cleaned_data['email'],
63
            subject=_('Your new %s account confirmation') % current_site.domain,
64
            context={
65
                'user': self.instance,
66
                'site_domain': current_site.domain,
67
                'confirm_url': confirm_url,
68
            }
69
        )
70
71
72
class PasswordResetForm(DjangoPasswordResetForm):
73
    """
74
        Overrides the default form b/c it uses Site.objects.get_current()
75
        which uses an internal cache and produces wrong results when
76
        kiwitcms-tenants is installed.
77
    """
78
    def save(self, domain_override=None,  # pylint: disable=too-many-arguments
79
             subject_template_name='registration/password_reset_subject.txt',
80
             email_template_name='registration/password_reset_email.html',
81
             use_https=False, token_generator=default_token_generator,
82
             from_email=None, request=None, html_email_template_name=None,
83
             extra_email_context=None):
84
        current_site = Site.objects.get(pk=settings.SITE_ID)
85
        # call the stock method and just overrides the domain
86
        super().save(
87
            current_site.domain,
88
            subject_template_name,
89
            email_template_name,
90
            use_https,
91
            token_generator,
92
            from_email,
93
            request,
94
            html_email_template_name,
95
            extra_email_context,
96
        )
97