Пример #1
0
    def post(self, request, *args, **kwargs):
        config = SiteConfiguration.get_solo()
        email_to_verify = value_without_invalid_marker(request.user.email)
        url, token = create_unique_url({
            'action': 'email_update',
            'v': True,
            'pk': request.user.pk,
            'email': email_to_verify,
        })
        context = {
            'site_name': config.site_name,
            'ENV': settings.ENVIRONMENT,
            'subject_prefix': settings.EMAIL_SUBJECT_PREFIX_FULL,
            'url': url,
            'url_first': url[:url.rindex('/') + 1],
            'url_second': token,
            'user': request.user,
        }
        email_template_subject = get_template(
            'email/system-email_verify_subject.txt')
        email_template_text = get_template('email/system-email_verify.txt')
        email_template_html = get_template('email/system-email_verify.html')
        send_mail(
            ''.join(email_template_subject.render(
                context).splitlines()),  # no newlines allowed in subject.
            email_template_text.render(context),
            settings.DEFAULT_FROM_EMAIL,
            recipient_list=[email_to_verify],
            html_message=email_template_html.render(context),
            fail_silently=False)

        if request.is_ajax():
            return JsonResponse({'success': 'verification-requested'})
        else:
            return TemplateResponse(request, self.template_name)
Пример #2
0
    def post(self, request, *args, **kwargs):
        config = SiteConfiguration.get_solo()
        email_to_verify = value_without_invalid_marker(request.user.email)
        url = create_unique_url({
            'action': 'email_update',
            'v': True,
            'pk': request.user.pk,
            'email': email_to_verify,
        })
        context = {
            'site_name': config.site_name,
            'url': url,
            'user': request.user,
        }
        subject = _("[Pasporta Servo] Is this your email address?")
        email_template_text = get_template('email/system-email_verify.txt')
        email_template_html = get_template('email/system-email_verify.html')
        send_mail(
            subject,
            email_template_text.render(context),
            settings.DEFAULT_FROM_EMAIL,
            recipient_list=[email_to_verify],
            html_message=email_template_html.render(context),
            fail_silently=False)

        if request.is_ajax():
            return JsonResponse({'success': 'verification-requested'})
        else:
            return TemplateResponse(request, self.template_name)
Пример #3
0
 def test_value_without_invalid_marker(self):
     test_data = (
         ("*****@*****.**", "*****@*****.**"),
         (f"user_{settings.INVALID_PREFIX}@mail.com", f"user_{settings.INVALID_PREFIX}@mail.com"),
         (f"[email protected]_{settings.INVALID_PREFIX}", f"[email protected]_{settings.INVALID_PREFIX}"),
         ("{0}{0}user".format(settings.INVALID_PREFIX), f"{settings.INVALID_PREFIX}user"),
         ("{0}user{0}".format(settings.INVALID_PREFIX), f"user{settings.INVALID_PREFIX}"),
         (f"{settings.INVALID_PREFIX}user@not--mail", "user@not--mail"),
     )
     self.assertNotEqual(settings.INVALID_PREFIX, "")
     for original_value, expected_value in test_data:
         with self.subTest(value=original_value):
             self.assertEqual(value_without_invalid_marker(original_value), expected_value)
Пример #4
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     # Stores the value before the change.
     self.previous_email = value_without_invalid_marker(self.instance.email)
Пример #5
0
 def remove_invalid_prefix(user):
     user.email = value_without_invalid_marker(user.email)
     return user
Пример #6
0
    def form_valid(self, form):
        body = form.cleaned_data['body']
        md_body = commonmark(body)
        subject = form.cleaned_data['subject']
        preheader = form.cleaned_data['preheader']
        heading = form.cleaned_data['heading']
        category = form.cleaned_data['categories']
        default_from = settings.DEFAULT_FROM_EMAIL
        template = get_template('email/mass_email.html')

        opening = datetime(2014, 11, 24)
        profiles = []

        if category in ("test", "just_user"):
            # only active profiles, linked to existing user accounts
            profiles = Profile.objects.filter(user__isnull=False)
            # exclude completely those who have at least one active available place
            profiles = profiles.exclude(owned_places__in=Place.objects.filter(
                available=True))
            # remove profiles with places available in the past, that is deleted
            profiles = profiles.filter(
                Q(owned_places__available=False)
                | Q(owned_places__isnull=True))
            # finally remove duplicates
            profiles = profiles.distinct()
        elif category == "old_system":
            # those who logged in before the opening date; essentially, never used the new system
            profiles = Profile.objects.filter(
                user__last_login__lte=opening).distinct()
        else:
            # those who logged in after the opening date
            profiles = Profile.objects.filter(user__last_login__gt=opening)
            # filter by active places according to 'in-book?' selection
            if category == "in_book":
                profiles = profiles.filter(owned_places__in_book=True)
            elif category == "not_in_book":
                profiles = profiles.filter(owned_places__in_book=False,
                                           owned_places__available=True)
            # finally remove duplicates
            profiles = profiles.distinct()

        if category == 'test':
            test_email = form.cleaned_data['test_email']
            context = {
                'preheader': mark_safe(preheader.format(nomo=test_email)),
                'heading': heading,
                'body': mark_safe(md_body.format(nomo=test_email)),
            }
            messages = [(
                subject,
                body.format(nomo=test_email),
                template.render(context),
                default_from,
                [test_email],
            )]

        else:
            name_placeholder = _("user")
            messages = [(
                subject,
                body.format(nomo=profile.name or name_placeholder),
                template.render({
                    'preheader':
                    mark_safe(
                        preheader.format(
                            nomo=escape(profile.name or name_placeholder))),
                    'heading':
                    heading,
                    'body':
                    mark_safe(
                        md_body.format(
                            nomo=escape(profile.name or name_placeholder))),
                }),
                default_from,
                [value_without_invalid_marker(profile.user.email)],
            ) for profile in profiles] if profiles else []

        self.nb_sent = send_mass_html_mail(messages)
        return super().form_valid(form)