Exemple #1
0
def send_login_email(accounting_data):
    from sesame.utils import get_query_string

    User = get_user_model()
    Organization = swapper.load_model('openwisp_users', 'Organization')
    username = accounting_data.get('username', None)
    org_uuid = accounting_data.get('organization')
    try:
        user = User.objects.get(username=username)
        organization = Organization.objects.select_related('radius_settings').get(
            id=org_uuid
        )
    except ObjectDoesNotExist:
        logger.warning(f'user with {username} does not exists')
        return
    if not organization.is_member(user):
        logger.warning(f'{username} is not the member of {organization.name}')
        return

    org_radius_settings = organization.radius_settings
    login_url = org_radius_settings.login_url
    if not login_url:
        logger.error(f'login_url is not defined for {organization.name} organization')
        return
    with translation.override(user.language):
        one_time_login_url = login_url + get_query_string(user)
        subject = _('New WiFi session started')
        context = {
            'user': user,
            'subject': subject,
            'call_to_action_url': one_time_login_url,
            'call_to_action_text': _('Manage Session'),
        }
        body_html = loader.render_to_string('radius_accounting_start.html', context)
        send_email(subject, body_html, body_html, [user.email], context)
Exemple #2
0
def send_email_notification(sender, instance, created, **kwargs):
    # Abort if a new notification is not created
    if not created:
        return
    # Get email preference of user for this type of notification.
    target_org = getattr(getattr(instance, 'target', None), 'organization_id',
                         None)
    if instance.type and target_org:
        try:
            notification_setting = instance.recipient.notificationsetting_set.get(
                organization=target_org, type=instance.type)
        except NotificationSetting.DoesNotExist:
            return
        email_preference = notification_setting.email_notification
    else:
        # We can not check email preference if notification type is absent,
        # or if target_org is not present
        # therefore send email anyway.
        email_preference = True

    if not (email_preference and instance.recipient.email):
        return

    try:
        subject = instance.email_subject
    except NotificationRenderException:
        # Do not send email if notification is malformed.
        return
    url = instance.data.get('url', '') if instance.data else None
    description = instance.message
    if url:
        target_url = url
    elif instance.target:
        target_url = instance.redirect_view_url
    else:
        target_url = None
    if target_url:
        description += _('\n\nFor more information see %(target_url)s.') % {
            'target_url': target_url
        }

    send_email(
        subject,
        description,
        instance.message,
        recipients=[instance.recipient.email],
        extra_context={
            'call_to_action_url': target_url,
            'call_to_action_text': _('Find out more'),
        },
    )

    # flag as emailed
    instance.emailed = True
    # bulk_update is used to prevent emitting post_save signal
    Notification.objects.bulk_update([instance], ['emailed'])
Exemple #3
0
 def test_blank_html_body(self):
     send_email(
         'Test mail',
         'This is a test email',
         '',
         ['*****@*****.**', '*****@*****.**'],
     )
     self.assertEqual(len(mail.outbox), 1)
     email = mail.outbox.pop()
     self.assertEqual(email.subject, 'Test mail')
     self.assertEqual(email.body, 'This is a test email')
     self.assertFalse(email.alternatives)
Exemple #4
0
 def send_mail(
     self,
     subject_template_name,
     email_template_name,
     context,
     from_email,
     to_email,
     html_email_template_name=None,
 ):
     subject = context.get('subject')
     body_html = loader.render_to_string(email_template_name, context)
     send_email(subject, body_html, body_html, [to_email], context)
Exemple #5
0
 def test_email_action_text_and_url(self):
     send_email(
         'Test mail',
         '',
         'This is a test email',
         ['*****@*****.**', '*****@*****.**'],
         extra_context={
             'call_to_action_text': 'Click me',
             'call_to_action_url': 'https://openwisp.io/docs/index.html',
         },
     )
     self.assertEqual(len(mail.outbox), 1)
     email = mail.outbox.pop()
     self.assertIn('Click me</a>\n', email.alternatives[0][0])
     self.assertIn(
         '<a href="https://openwisp.io/docs/index.html" class="btn">',
         email.alternatives[0][0],
     )
Exemple #6
0
 def test_email(self):
     send_email(
         'Test mail',
         '',
         'This is a test email',
         ['*****@*****.**'],
     )
     self.assertEqual(len(mail.outbox), 1)
     email = mail.outbox.pop()
     # test from email
     self.assertEqual(email.from_email, '*****@*****.**')
     # test image logo
     self.assertIn(
         'https://raw.githubusercontent.com/openwisp/openwisp-utils/master/'
         'openwisp_utils/static/openwisp-utils/images/openwisp-logo.png',
         email.alternatives[0][0],
     )
     # test email doesn't contain link
     self.assertNotIn('<a href', email.alternatives[0][0])
Exemple #7
0
 def send_mail(self, template_prefix, email, context):
     subject = render_to_string("{0}_subject.txt".format(template_prefix),
                                context)
     subject = " ".join(subject.splitlines()).strip()
     subject = self.format_email_subject(subject)
     content = {}
     errors = {}
     for ext in ['html', 'txt']:
         template_name = '{0}_message.{1}'.format(template_prefix, ext)
         if 'activate_url' in context:
             context['call_to_action_url'] = context['activate_url']
             context['call_to_action_text'] = _('Confirm')
         try:
             template_name = '{0}_message.{1}'.format(template_prefix, ext)
             content[ext] = render_to_string(template_name, context,
                                             self.request).strip()
         except TemplateDoesNotExist as e:
             errors[ext] = e
         text = content.get('txt', '')
         html = content.get('html', '')
         # both templates fail to load, raise the exception
         if len(errors.keys()) >= 2:
             raise errors['txt'] from errors['html']
     send_email(subject, text, html, [email], context)