Passed
Pull Request — master (#227)
by Piotr
01:06
created

BaseEmail.send()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
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.utils.encoding import force_bytes, force_text
0 ignored issues
show
introduced by
Unable to import 'django.utils.encoding'
Loading history...
6
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...
7
8
from templated_email import send_templated_mail
0 ignored issues
show
introduced by
Unable to import 'templated_email'
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
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...
37
    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...
38
        serializer = self.get_serializer(data=request.data)
39
        serializer.is_valid(raise_exception=True)
40
        return self._action(serializer)
41
42
43
class BaseEmail(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...
44
    template_name = None
45
46
    @classmethod
47
    def send(cls, request, from_email, **context):
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...
48
        context = cls.get_context(request, from_email, **context)
49
50
        user = context.get('user')
51
        recipients_list = cls._get_recipients_list([user])
52
        send_templated_mail(
53
            cls.template_name, from_email, recipients_list, context
54
        )
55
56
    @classmethod
57
    def get_context(cls, request, from_email, **context):
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...
58
        site = get_current_site(request)
59
        context.update({
60
            'domain': django_settings.DJOSER.get('DOMAIN') or site.domain,
61
            'from_email': from_email or getattr(
62
                settings, 'DEFAULT_FROM_EMAIL', ''),
63
            'protocol': context.get('protocol') or (
64
                'https' if request.is_secure() else 'http'),
65
            'site_name': django_settings.DJOSER.get('SITE_NAME') or site.name,
66
            'user': context.get('user') or request.user,
67
        })
68
        return context
69
70
    @classmethod
71
    def _get_recipients_list(cls, users):
72
        recipients_list = []
73
        for user in users:
74
            user_email = get_user_email(user)
75
            if user_email is None:
76
                raise ValueError(constants.USER_WITHOUT_EMAIL_FIELD_ERROR)
77
            recipients_list.append(user_email)
78
        return recipients_list
79
80
81 View Code Duplication
class UserActivationEmail(BaseEmail):
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...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
82
    template_name = 'activation'
83
84
    @classmethod
85
    def get_context(cls, request, from_email, **context):
86
        context = super(UserActivationEmail, cls).get_context(
87
            request, from_email, **context
88
        )
89
90
        user = context.get('user')
91
        context['uid'] = encode_uid(user.pk)
92
        context['token'] = default_token_generator.make_token(user),
93
        context['url'] = settings.ACTIVATION_URL.format(**context)
94
        return context
95
96
97 View Code Duplication
class UserPasswordResetEmail(BaseEmail):
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...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
98
    template_name = 'password_reset'
99
100
    @classmethod
101
    def get_context(cls, request, from_email, **context):
102
        context = super(UserPasswordResetEmail, cls).get_context(
103
            request, from_email, **context
104
        )
105
106
        user = context.get('user')
107
        context['uid'] = encode_uid(user.pk)
108
        context['token'] = default_token_generator.make_token(user),
109
        context['url'] = settings.PASSWORD_RESET_CONFIRM_URL.format(**context)
110
        return context
111
112
113
#  class UserConfirmationEmailFactory(UserEmailFactoryBase):
114
#     subject_template_name = 'confirmation_email_subject.txt'
115
#     plain_body_template_name = 'confirmation_email_body.txt'
116
#     if settings.USE_HTML_EMAIL_TEMPLATES:
117
#         html_body_template_name = 'confirmation_email_body.html'
118