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

ActivationEmail.get_context()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
1
from django.contrib.auth.tokens import default_token_generator
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.contrib.auth.tokens'
Loading history...
2
from django.contrib.sites.shortcuts import get_current_site
0 ignored issues
show
introduced by
Unable to import 'django.contrib.sites.shortcuts'
Loading history...
3
4
from mail_templated import send_mail
0 ignored issues
show
introduced by
Unable to import 'mail_templated'
Loading history...
5
6
from djoser import constants, utils
7
from djoser.compat import get_user_email
8
from djoser.conf import settings
9
10
11
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...
12
    template_name = None
13
14
    @classmethod
15
    def send(cls, request, context, from_email=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...
16
        context = cls.get_context(request, context)
17
18
        user = context.get('user')
19
        recipients_list = cls._get_recipients_list([user])
20
        send_mail(
21
            cls.template_name, context, from_email, recipients_list,
22
        )
23
24
    @classmethod
25
    def get_context(cls, request, 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...
26
        site = get_current_site(request)
27
        context.update({
28
            'domain': settings.DOMAIN or site.domain,
29
            'protocol': context.get('protocol') or (
30
                'https' if request.is_secure() else 'http'),
31
            'site_name': settings.SITE_NAME or site.name,
32
            'user': context.get('user') or request.user,
33
        })
34
        return context
35
36
    @classmethod
37
    def _get_recipients_list(cls, users):
38
        recipients_list = []
39
        for user in users:
40
            user_email = get_user_email(user)
41
            if user_email is None:
42
                raise ValueError(constants.USER_WITHOUT_EMAIL_FIELD_ERROR)
43
            recipients_list.append(user_email)
44
        return recipients_list
45
46
47
class ActivationEmail(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...
48
    template_name = 'email/activation.html'
49
50
    @classmethod
51
    def get_context(cls, request, context):
52
        context = super(ActivationEmail, cls).get_context(request, context)
53
54
        user = context.get('user')
55
        context['uid'] = utils.encode_uid(user.pk)
56
        context['token'] = default_token_generator.make_token(user)
57
        context['url'] = settings.ACTIVATION_URL.format(**context)
58
        return context
59
60
61
class ConfirmationEmail(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...
62
    template_name = 'email/confirmation.html'
63
64
65
class PasswordResetEmail(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...
66
    template_name = 'email/password_reset.html'
67
68
    @classmethod
69
    def get_context(cls, request, context):
70
        context = super(PasswordResetEmail, cls).get_context(
71
            request, context
72
        )
73
74
        user = context.get('user')
75
        context['uid'] = utils.encode_uid(user.pk)
76
        context['token'] = default_token_generator.make_token(user)
77
        context['url'] = settings.PASSWORD_RESET_CONFIRM_URL.format(**context)
78
        return context
79