GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

send_notification_email_reciver()   F
last analyzed

Complexity

Conditions 9

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
c 1
b 0
f 0
dl 0
loc 43
rs 3
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3
"""
4
Send notification emails to admins, managers or particular recipients 
5
when new user has registered in the site
6
7
admins or managers are determined from ``ADMINS`` and ``MANAGERS`` attribute of
8
``settings.py``
9
10
You can disable this notification feature by setting ``False`` to 
11
``REGISTRATION_NOTIFICATION``.
12
You can disable sending emails to admins by setting ``False`` to
13
``REGISTRATION_NOTIFICATION_ADMINS``.
14
You can disable sending emails to managers by settings ``False`` to
15
``REGISTRATION_NOTIFICATION_MANAGERS``.
16
17
If you need extra recipients for the notification email, set a list of email 
18
addresses or a function which return a list to 
19
``REGISTRATION_NOTIFICATION_RECIPIENTS``
20
21
The notification email use the following templates in default
22
23
``registration/notification_email.txt``
24
    Used for email body, the following context will be passed
25
    
26
    ``site``
27
        A instance of ``django.contrib.sites.models.Site`` or
28
        ``django.contrib.sites.models.RequestSite``
29
30
    ``user``
31
        A ``User`` instance who has just registered
32
33
    ``profile``
34
        A ``RegistrationProfile`` instance of the ``user``
35
36
``registration/notification_email_subject.txt``
37
    Used for email subject, the following context will be passed
38
    
39
    ``site``
40
        A instance of ``django.contrib.sites.models.Site`` or
41
        ``django.contrib.sites.models.RequestSite``
42
43
    ``user``
44
        A ``User`` instance who has just registered
45
46
    ``profile``
47
        A ``RegistrationProfile`` instance of the ``user``
48
49
    .. Note::
50
        Newlies of the template will be removed.
51
52
If you want to change the name of template, use following settings
53
54
-   ``REGISTRATION_NOTIFICATION_EMAIL_TEMPLATE_NAME``
55
-   ``REGISTRATION_NOTIFICATION_EMAIL_SUBJECT_TEMPLATE_NAME``
56
57
    
58
.. Note::
59
    This feature is not available in tests because default tests of 
60
    django-inspectional-registration are not assumed to test with contributes.
61
62
    If you do want this feature to be available in tests, set
63
    ``_REGISTRATION_NOTIFICATION_IN_TESTS`` to ``True`` in ``setUp()`` method
64
    of the test case class and delete the attribute in ``tearDown()`` method.
65
66
"""
67
__author__ = "Alisue <[email protected]>"
68
import sys
69
70
from django.core.exceptions import ImproperlyConfigured
71
from django.template.loader import render_to_string
72
73
from registration.utils import get_site
74
from registration.utils import send_mail
75
from registration.signals import user_registered
76
from registration.contrib.notification.conf import settings
77
78
79
def is_notification_enable():
80
    """get whether the registration notification is enable"""
81
    if not settings.REGISTRATION_NOTIFICATION:
82
        return False
83
    if 'test' in sys.argv and not getattr(settings,
84
                                          '_REGISTRATION_NOTIFICATION_IN_TESTS',
85
                                          False):
86
        # Registration Notification is not available in test to prevent the test
87
        # fails of ``registration.tests.*``.
88
        # For testing Registration Notification, you must set
89
        # ``_REGISTRATION_NOTIFICATION_IN_TESTS`` to ``True``
90
        return False
91
    admins = settings.REGISTRATION_NOTIFICATION_ADMINS
92
    managers = settings.REGISTRATION_NOTIFICATION_MANAGERS
93
    recipients = settings.REGISTRATION_NOTIFICATION_RECIPIENTS
94
    if not (admins or managers or recipients):
95
        # All REGISTRATION_NOTIFICATION_{ADMINS, MANAGERS, RECIPIENTS} = False
96
        # is same as REGISTRATION_NOTIFICATION = False but user should use
97
        # REGISTRATION_NOTIFICATION = False insted of setting False to all
98
        # settings of notification.
99
        import warnings
100
        warnings.warn(
101
            'To set ``registration.contrib.notification`` disable, '
102
            'set ``REGISTRATION_NOTIFICATION`` to ``False``')
103
        return False
104
    return True
105
106
107
def send_notification_email_reciver(sender, user, profile, request, **kwargs):
108
    """send a notification email to admins/managers"""
109
    if not is_notification_enable():
110
        return
111
112
    context = {
113
        'user': user,
114
        'profile': profile,
115
        'site': get_site(request),
116
    }
117
    subject = render_to_string(
118
        settings.REGISTRATION_NOTIFICATION_EMAIL_SUBJECT_TEMPLATE_NAME,
119
        context)
120
    subject = "".join(subject.splitlines())
121
    message = render_to_string(
122
        settings.REGISTRATION_NOTIFICATION_EMAIL_TEMPLATE_NAME,
123
        context)
124
125
    recipients = []
126
    if settings.REGISTRATION_NOTIFICATION_ADMINS:
127
        for userinfo in settings.ADMINS:
128
            recipients.append(userinfo[1])
129
    if settings.REGISTRATION_NOTIFICATION_MANAGERS:
130
        for userinfo in settings.MANAGERS:
131
            recipients.append(userinfo[1])
132
    if settings.REGISTRATION_NOTIFICATION_RECIPIENTS:
133
        method_or_iterable = settings.REGISTRATION_NOTIFICATION_RECIPIENTS
134
        if callable(method_or_iterable):
135
            recipients.extend(method_or_iterable())
136
        elif isinstance(method_or_iterable, (list, tuple)):
137
            recipients.extend(method_or_iterable)
138
        else:
139
            raise ImproperlyConfigured((
140
                '``REGISTRATION_NOTIFICATION_RECIPIENTS`` must '
141
                'be a list of recipients or function which return '
142
                'a list of recipients (Currently the value was "%s")'
143
            ) % method_or_iterable)
144
    # remove duplications
145
    recipients = frozenset(recipients)
146
147
    mail_from = getattr(settings, 'REGISTRATION_FROM_EMAIL', '') or \
148
                    settings.DEFAULT_FROM_EMAIL
149
    send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipients)
150
user_registered.connect(send_notification_email_reciver)
151