Exemple #1
0
def email_users_view(request,
                     mailing_list=False,
                     template_name='studioadmin/email_users_form.html'):

    if mailing_list:
        subscribed, _ = Group.objects.get_or_create(name='subscribed')
        users_to_email = subscribed.user_set.all()
    else:
        users_to_email = User.objects.filter(
            id__in=request.session['users_to_email'])

    if request.method == 'POST':

        form = EmailUsersForm(request.POST)
        test_email = request.POST.get('send_test', False)

        if form.is_valid():
            subject = '{}{}'.format(form.cleaned_data['subject'],
                                    ' [TEST EMAIL]' if test_email else '')
            from_address = form.cleaned_data['from_address']
            message = form.cleaned_data['message']
            cc = form.cleaned_data['cc']

            # bcc recipients
            email_addresses = [user.email for user in users_to_email]
            email_count = len(email_addresses)
            number_of_emails = ceil(email_count / 99)

            if test_email:
                email_lists = [[from_address]]
            else:
                email_lists = [email_addresses]  # will be a list of lists
                # split into multiple emails of 99 bcc plus 1 cc
                if email_count > 99:
                    email_lists = [
                        email_addresses[i:i + 99]
                        for i in range(0, email_count, 99)
                    ]

            host = 'http://{}'.format(request.META.get('HTTP_HOST'))

            try:
                for i, email_list in enumerate(email_lists):
                    ctx = {
                        'subject': subject,
                        'message': message,
                        'number_of_emails': number_of_emails,
                        'email_count': email_count,
                        'is_test': test_email,
                        'mailing_list': mailing_list,
                        'host': host,
                    }
                    msg = EmailMultiAlternatives(
                        subject,
                        get_template(
                            'studioadmin/email/email_users.txt').render(ctx),
                        bcc=email_list,
                        cc=[from_address] if
                        (i == 0 and cc and not test_email) else [],
                        reply_to=[from_address])
                    msg.attach_alternative(
                        get_template(
                            'studioadmin/email/email_users.html').render(ctx),
                        "text/html")
                    msg.send(fail_silently=False)

                    if not test_email:
                        ActivityLog.objects.create(
                            log='{} email with subject "{}" sent to users {} by'
                            ' admin user {}'.format(
                                'Mailing list' if mailing_list else 'Bulk',
                                subject, ', '.join(
                                    email_list), request.user.username))
            except Exception as e:
                # send mail to tech support with Exception
                send_support_email(e, __name__, "Bulk Email to students")
                ActivityLog.objects.create(
                    log="Possible error with sending {} email; "
                    "notification sent to tech support".format(
                        'mailing list' if mailing_list else 'bulk'))

                if not test_email:
                    ActivityLog.objects.create(
                        log='{} email error '
                        '(email subject "{}"), sent by '
                        'by admin user {}'.format(
                            'Mailing list' if mailing_list else 'Bulk',
                            subject, request.user.username))

            if not test_email:
                messages.success(
                    request, '{} email with subject "{}" has been sent to '
                    'users'.format('Mailing list' if mailing_list else 'Bulk',
                                   subject))
                return HttpResponseRedirect(reverse('studioadmin:users'))
            else:
                messages.success(
                    request, 'Test email has been sent to {} only. Click '
                    '"Send Email" below to send this email to '
                    'users.'.format(from_address))

        # Do this if form not valid OR sending test email
        event_ids = request.session.get('events', [])
        lesson_ids = request.session.get('lessons', [])
        events = Event.objects.filter(id__in=event_ids)
        lessons = Event.objects.filter(id__in=lesson_ids)

        if form.errors:
            totaleventids = event_ids + lesson_ids
            totalevents = Event.objects.filter(id__in=totaleventids)
            messages.error(
                request,
                mark_safe("Please correct errors in form: {}".format(
                    form.errors)))
            form = EmailUsersForm(
                initial={
                    'subject': "; ".join((str(event) for event in totalevents))
                })

        if test_email:
            form = EmailUsersForm(request.POST)

    else:
        event_ids = ast.literal_eval(request.GET.get('events', '[]'))
        events = Event.objects.filter(id__in=event_ids)
        lesson_ids = ast.literal_eval(request.GET.get('lessons', '[]'))
        lessons = Event.objects.filter(id__in=lesson_ids)
        totaleventids = event_ids + lesson_ids
        totalevents = Event.objects.filter(id__in=totaleventids)
        form = EmailUsersForm(
            initial={
                'subject': "; ".join((str(event) for event in totalevents))
            })

    return TemplateResponse(
        request, template_name, {
            'form': form,
            'users_to_email': users_to_email,
            'sidenav_selection':
            'mailing_list' if mailing_list else 'email_users',
            'events': events,
            'lessons': lessons,
            'mailing_list': mailing_list
        })
Exemple #2
0
 def test_missing_message(self):
     form = EmailUsersForm(data=self.form_data({'message': ''}))
     self.assertFalse(form.is_valid())
     self.assertEquals(form.errors['message'], ['This field is required.'])
Exemple #3
0
 def test_form_valid(self):
     form = EmailUsersForm(data=self.form_data())
     self.assertTrue(form.is_valid())
Exemple #4
0
 def test_missing_from_address(self):
     form = EmailUsersForm(data=self.form_data({'from_address': ''}))
     self.assertFalse(form.is_valid())
     self.assertEquals(form.errors['from_address'],
                       ['This field is required.'])
Exemple #5
0
def email_users_view(request,
                     template_name='studioadmin/email_users_form.html'):

    users_to_email = User.objects.filter(
        id__in=request.session['users_to_email'])

    if request.method == 'POST':

        form = EmailUsersForm(request.POST)

        if form.is_valid():
            subject = '{} {}'.format(settings.ACCOUNT_EMAIL_SUBJECT_PREFIX,
                                     form.cleaned_data['subject'])
            from_address = form.cleaned_data['from_address']
            message = form.cleaned_data['message']
            cc = form.cleaned_data['cc']

            # do this per email address so recipients are not visible to
            # each
            email_addresses = [user.email for user in users_to_email]
            for email_address in email_addresses:
                try:
                    msg = EmailMultiAlternatives(
                        subject,
                        message,
                        from_address, [email_address],
                        cc=[from_address] if cc else [],
                        reply_to=[from_address])
                    msg.attach_alternative(
                        get_template(
                            'studioadmin/email/email_users.html').render({
                                'subject':
                                subject,
                                'message':
                                message
                            }), "text/html")
                    msg.send(fail_silently=False)
                except Exception as e:
                    # send mail to tech support with Exception
                    send_support_email(e, __name__, "Bulk Email to students")
                    ActivityLog.objects.create(
                        log="Possible error with "
                        "sending bulk email; notification sent to tech support"
                    )

            ActivityLog.objects.create(
                log='Bulk email with subject "{}" sent to users {} by '
                'admin user {}'.format(subject, email_addresses,
                                       request.user.username))

            return render(request, 'studioadmin/email_users_confirmation.html')

        else:
            messages.error(
                request,
                mark_safe("Please correct errors in form: {}".format(
                    form.errors)))

    else:

        form = EmailUsersForm()

    return TemplateResponse(
        request, template_name, {
            'form': form,
            'users_to_email': users_to_email,
            'sidenav_selection': 'email_users',
        })