Passed
Push — master ( cb96a6...03dbbf )
by Piotr
01:10
created

send_email()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
1
from django.conf import settings as django_settings
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Unable to import 'django.conf'
Loading history...
2
from django.contrib.auth import user_logged_in, user_logged_out
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth'
Loading history...
3
from django.contrib.auth.tokens import default_token_generator
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth.tokens'
Loading history...
4
from django.contrib.sites.shortcuts import get_current_site
0 ignored issues
show
introduced by
Unable to import 'django.contrib.sites.shortcuts'
Loading history...
5
from django.core.mail import EmailMultiAlternatives, EmailMessage
0 ignored issues
show
introduced by
Unable to import 'django.core.mail'
Loading history...
6
from django.template import loader
0 ignored issues
show
introduced by
Unable to import 'django.template'
Loading history...
7
from django.utils.encoding import force_bytes, force_text
0 ignored issues
show
introduced by
Unable to import 'django.utils.encoding'
Loading history...
8
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
0 ignored issues
show
introduced by
Unable to import 'django.utils.http'
Loading history...
9
10
from djoser import constants
11
from djoser.compat import get_user_email
12
from djoser.conf import settings
13
14
15
def encode_uid(pk):
0 ignored issues
show
Coding Style Naming introduced by
The name pk does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
16
    return urlsafe_base64_encode(force_bytes(pk)).decode()
17
18
19
def decode_uid(pk):
0 ignored issues
show
Coding Style Naming introduced by
The name pk does not conform to the argument naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
20
    return force_text(urlsafe_base64_decode(pk))
21
22
23
def login_user(request, user):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
24
    token, _ = settings.TOKEN_MODEL.objects.get_or_create(user=user)
25
    user_logged_in.send(sender=user.__class__, request=request, user=user)
26
    return token
27
28
29
def logout_user(request):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
30
    settings.TOKEN_MODEL.objects.filter(user=request.user).delete()
31
    user_logged_out.send(
32
        sender=request.user.__class__, request=request, user=request.user
33
    )
34
35
36
def send_email(request, factory, user):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
37
    email_factory = factory.from_request(request, user)
38
    email = email_factory.create()
39
    email.send()
40
41
42
class ActionViewMixin(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
43
    def post(self, request):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
44
        serializer = self.get_serializer(data=request.data)
45
        serializer.is_valid(raise_exception=True)
46
        return self._action(serializer)
47
48
49
class UserEmailFactoryBase(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
50
    token_generator = default_token_generator
51
    subject_template_name = None
52
    plain_body_template_name = None
53
    html_body_template_name = None
54
55
    def __init__(self, from_email, user, protocol, domain, site_name,
0 ignored issues
show
best-practice introduced by
Too many arguments (6/5)
Loading history...
56
                 **context_data):
57
        self.from_email = from_email
58
        self.user = user
59
        self.domain = domain
60
        self.site_name = site_name
61
        self.protocol = protocol
62
        self.context_data = context_data
63
64
    @classmethod
65
    def from_request(cls, request, user=None, from_email=None, protocol=None,
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
66
                     **context_data):
67
        site = get_current_site(request)
68
        from_email = from_email or getattr(
69
            django_settings, 'DEFAULT_FROM_EMAIL', ''
70
        )
71
72
        return cls(
73
            from_email=from_email,
74
            user=user or request.user,
75
            domain=django_settings.DJOSER.get('DOMAIN') or site.domain,
76
            site_name=django_settings.DJOSER.get('SITE_NAME') or site.name,
77
            protocol=protocol or ('https' if request.is_secure() else 'http'),
78
            **context_data
79
        )
80
81
    def create(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
82
        assert self.plain_body_template_name or self.html_body_template_name
83
        context = self.get_context()
84
        subject = loader.render_to_string(self.subject_template_name, context)
85
        subject = ''.join(subject.splitlines())
86
87
        user_email = get_user_email(self.user)
88
        if user_email is None:
89
            raise ValueError(constants.USER_WITHOUT_EMAIL_FIELD_ERROR)
90
91
        if self.plain_body_template_name:
92
            plain_body = loader.render_to_string(
93
                self.plain_body_template_name, context
94
            )
95
            email_message = EmailMultiAlternatives(
96
                subject, plain_body, self.from_email, [user_email]
97
            )
98
            if self.html_body_template_name:
99
                html_body = loader.render_to_string(
100
                    self.html_body_template_name, context
101
                )
102
                email_message.attach_alternative(html_body, 'text/html')
103
        else:
104
            html_body = loader.render_to_string(
105
                self.html_body_template_name, context
106
            )
107
            email_message = EmailMessage(
108
                subject, html_body, self.from_email, [user_email]
109
            )
110
            email_message.content_subtype = 'html'
111
        return email_message
112
113
    def get_context(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
114
        context = {
115
            'user': self.user,
116
            'domain': self.domain,
117
            'site_name': self.site_name,
118
            'uid': encode_uid(self.user.pk),
119
            'token': self.token_generator.make_token(self.user),
120
            'protocol': self.protocol,
121
        }
122
        context.update(self.context_data)
123
        return context
124
125
126
class UserActivationEmailFactory(UserEmailFactoryBase):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
127
    subject_template_name = 'activation_email_subject.txt'
128
    plain_body_template_name = 'activation_email_body.txt'
129
    if settings.USE_HTML_EMAIL_TEMPLATES:
130
        html_body_template_name = 'activation_email_body.html'
131
132
    def get_context(self):
133
        context = super(UserActivationEmailFactory, self).get_context()
134
        context['url'] = settings.ACTIVATION_URL.format(**context)
135
        return context
136
137
138
class UserPasswordResetEmailFactory(UserEmailFactoryBase):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
139
    subject_template_name = 'password_reset_email_subject.txt'
140
    plain_body_template_name = 'password_reset_email_body.txt'
141
    if settings.USE_HTML_EMAIL_TEMPLATES:
142
        html_body_template_name = 'password_reset_email_body.html'
143
144
    def get_context(self):
145
        context = super(UserPasswordResetEmailFactory, self).get_context()
146
        context['url'] = settings.PASSWORD_RESET_CONFIRM_URL.format(
147
            **context
148
        )
149
        return context
150
151
152
class UserConfirmationEmailFactory(UserEmailFactoryBase):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
153
    subject_template_name = 'confirmation_email_subject.txt'
154
    plain_body_template_name = 'confirmation_email_body.txt'
155
    if settings.USE_HTML_EMAIL_TEMPLATES:
156
        html_body_template_name = 'confirmation_email_body.html'
157