Passed
Push — master ( 95313c...be9045 )
by Piotr
57s
created

BaseEmailMessage.get_context_data()   B

Complexity

Conditions 3

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
dl 0
loc 29
rs 8.8571
1
from copy import deepcopy
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...
2
3
from django.contrib.sites.shortcuts import get_current_site
0 ignored issues
show
introduced by
Unable to import 'django.contrib.sites.shortcuts'
Loading history...
4
from django.core import mail
0 ignored issues
show
introduced by
Unable to import 'django.core'
Loading history...
5
from django.template.context import make_context
0 ignored issues
show
introduced by
Unable to import 'django.template.context'
Loading history...
6
from django.template.loader import get_template
0 ignored issues
show
introduced by
Unable to import 'django.template.loader'
Loading history...
7
8
from django.conf import settings
0 ignored issues
show
introduced by
Unable to import 'django.conf'
Loading history...
9
10
11
class BaseEmailMessage(mail.EmailMultiAlternatives):
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...
best-practice introduced by
Too many instance attributes (9/7)
Loading history...
12
    _node_map = {
13
        'subject': 'subject',
14
        'text_body': 'body',
15
        'html_body': 'html',
16
    }
17
    template_name = None
18
19
    def __init__(self, request=None, context=None, template_name=None,
20
                 *args, **kwargs):
21
        super(BaseEmailMessage, self).__init__(*args, **kwargs)
22
23
        self.request = request
24
        self.context = {} if context is None else context
25
        self.html = None
26
27
        if template_name is not None:
28
            self.template_name = template_name
29
30
    def get_context_data(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...
31
        context = deepcopy(self.context)
32
        if self.request:
33
            site = get_current_site(self.request)
34
            domain = context.get('domain') or (
35
                getattr(settings, 'DOMAIN', '') or site.domain
36
            )
37
            protocol = context.get('protocol') or (
38
                'https' if self.request.is_secure() else 'http'
39
            )
40
            site_name = context.get('site_name') or (
41
                getattr(settings, 'SITE_NAME', '') or site.name
42
            )
43
            user = context.get('user') or self.request.user
44
        else:
45
            domain = context.get('domain') or getattr(settings, 'DOMAIN', '')
46
            protocol = context.get('protocol') or 'http'
47
            site_name = context.get('site_name') or getattr(
48
                settings, 'SITE_NAME', ''
49
            )
50
            user = context.get('user')
51
52
        context.update({
53
            'domain': domain,
54
            'protocol': protocol,
55
            'site_name': site_name,
56
            'user': user
57
        })
58
        return context
59
60
    def render(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...
61
        context = make_context(self.get_context_data(), request=self.request)
62
        template = get_template(self.template_name)
63
        with context.bind_template(template.template):
64
            for node in template.template.nodelist:
65
                self._process_node(node, context)
66
        self._attach_body()
67
68
    def send(self, to, cc=None, bcc=None, *args, **kwargs):
0 ignored issues
show
Coding Style Naming introduced by
The name to 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 Naming introduced by
The name cc 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 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...
69
        self.render()
70
        self.to = to
0 ignored issues
show
Coding Style Naming introduced by
The name to does not conform to the attribute 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
The attribute to was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
71
        if cc:
72
            self.cc = cc
0 ignored issues
show
Coding Style Naming introduced by
The name cc does not conform to the attribute 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
The attribute cc was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
73
        if bcc:
74
            self.bcc = bcc
0 ignored issues
show
Coding Style introduced by
The attribute bcc was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
75
        super(BaseEmailMessage, self).send(*args, **kwargs)
76
77
    def _process_node(self, node, context):
78
        attr = self._node_map.get(getattr(node, 'name', ''))
79
        if attr is not None:
80
            setattr(self, attr, node.render(context).strip())
81
82
    def _attach_body(self):
83
        if self.body and self.html:
0 ignored issues
show
Bug introduced by
The member body does not seem to be defined here; its definition is on line 86.
Loading history...
84
            self.attach_alternative(self.html, 'text/html')
85
        elif self.html:
86
            self.body = self.html
0 ignored issues
show
Coding Style introduced by
The attribute body was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
87
            self.content_subtype = 'html'
0 ignored issues
show
Coding Style introduced by
The attribute content_subtype was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
88