Completed
Push — master ( 6187bd...94e226 )
by Piotr
06:28
created

BaseEmailMessage.send()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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