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.
Completed
Push — master ( 0c3d5f...5b7906 )
by Lambda
6s
created

src.registration.contrib.notification.is_notification_enable()   C

Complexity

Conditions 7

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 26
rs 5.5
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
from django.core.exceptions import ImproperlyConfigured
70
from django.template.loader import render_to_string
71
72
from registration.utils import get_site
73
from registration.utils import send_mail
74
from registration.signals import user_registered
75
from registration.contrib.notification.conf import settings
76
77
78
def is_notification_enable():
79
    """get whether the registration notification is enable"""
80
    if not settings.REGISTRATION_NOTIFICATION:
81
        return False
82
    if 'test' in sys.argv and not getattr(settings,
83
                                          '_REGISTRATION_NOTIFICATION_IN_TESTS',
84
                                          False):
85
        # Registration Notification is not available in test to prevent the test
86
        # fails of ``registration.tests.*``.
87
        # For testing Registration Notification, you must set
88
        # ``_REGISTRATION_NOTIFICATION_IN_TESTS`` to ``True``
89
        return False
90
    admins = settings.REGISTRATION_NOTIFICATION_ADMINS
91
    managers = settings.REGISTRATION_NOTIFICATION_MANAGERS
92
    recipients = settings.REGISTRATION_NOTIFICATION_RECIPIENTS
93
    if not (admins or managers or recipients):
94
        # All REGISTRATION_NOTIFICATION_{ADMINS, MANAGERS, RECIPIENTS} = False
95
        # is same as REGISTRATION_NOTIFICATION = False but user should use
96
        # REGISTRATION_NOTIFICATION = False insted of setting False to all
97
        # settings of notification.
98
        import warnings
99
        warnings.warn(
100
                'To set ``registration.contrib.notification`` disable, '
101
                'set ``REGISTRATION_NOTIFICATION`` to ``False``')
102
        return False
103
    return True
104
105
106
def send_notification_email_reciver(sender, user, profile, request, **kwargs):
107
    """send a notification email to admins/managers"""
108
    if not is_notification_enable():
109
        return
110
111
    context = {
112
            'user': user,
113
            'profile': profile,
114
            'site': get_site(request),
115
        }
116
    subject = render_to_string(
117
            settings.REGISTRATION_NOTIFICATION_EMAIL_SUBJECT_TEMPLATE_NAME,
118
            context)
119
    subject = "".join(subject.splitlines())
120
    message = render_to_string(
121
            settings.REGISTRATION_NOTIFICATION_EMAIL_TEMPLATE_NAME,
122
            context)
123
124
    recipients = []
125
    if settings.REGISTRATION_NOTIFICATION_ADMINS:
126
        for userinfo in settings.ADMINS:
127
            recipients.append(userinfo[1])
128
    if settings.REGISTRATION_NOTIFICATION_MANAGERS:
129
        for userinfo in settings.MANAGERS:
130
            recipients.append(userinfo[1])
131
    if settings.REGISTRATION_NOTIFICATION_RECIPIENTS:
132
        method_or_iterable = settings.REGISTRATION_NOTIFICATION_RECIPIENTS
133
        if callable(method_or_iterable):
134
            recipients.extend(method_or_iterable())
135
        elif isinstance(method_or_iterable, (list, tuple)):
136
            recipients.extend(method_or_iterable)
137
        else:
138
            raise ImproperlyConfigured((
139
                    '``REGISTRATION_NOTIFICATION_RECIPIENTS`` must '
140
                    'be a list of recipients or function which return '
141
                    'a list of recipients (Currently the value was "%s")'
142
                    ) % method_or_iterable)
143
    # remove duplications
144
    recipients = frozenset(recipients)
145
146
    send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipients)
147
user_registered.connect(send_notification_email_reciver)
148