Ejemplo n.º 1
0
    def send_email(self, email):
        User = get_user_model()
        protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
        current_site = get_current_site(
            self.request)  # now gets the site from the url domain
        emailfrom = get_current_email_from(
            self.request)  # gets 'username' from smtp project settings
        connection = get_current_smtp_connection(
            self.request)  # gets the connection object from project settings

        email_qs = EmailAddress.objects.filter(email__iexact=email)
        for user in User.objects.filter(pk__in=email_qs.values("user")):
            uid = int_to_base36(user.id)
            token = self.make_token(user)
            password_reset_url = "{0}://{1}{2}".format(
                protocol, current_site.domain,
                reverse("account_password_reset_token",
                        kwargs=dict(uidb36=uid, token=token)))
            ctx = {
                "user": user,
                "current_site": current_site,
                "password_reset_url": password_reset_url,
                "email_from": emailfrom,
                "connection": connection
            }
            hookset.send_password_reset_email([user.email], ctx)
Ejemplo n.º 2
0
    def test_get_current_site(self):
        
        request = HttpRequest()
        request.META = {
            "SERVER_NAME": u"example2.com",
            "SERVER_PORT": "80",
        }
        site = get_current_site(request)
        self.assertTrue(isinstance(site, Site))
        self.assertEqual(site.name, u"example2.com")

        site.delete()
        
        self.assertRaises(Http404, get_current_site, request, raise404=True)

        site = get_current_site(request)
        self.assertEqual(site.id, settings.SITE_ID)
        self.assertEqual(site.name, u"example.com")
Ejemplo n.º 3
0
 def send_password_email(self, user):
     protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
     current_site = get_current_site(self.request)
     ctx = {
         "user": user,
         "protocol": protocol,
         "current_site": current_site,
     }
     hookset.send_password_change_email([user.email], ctx)
Ejemplo n.º 4
0
 def send_email(self, user):
     protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
     current_site = get_current_site(self.request)
     ctx = {
         "user": user,
         "protocol": protocol,
         "current_site": current_site,
     }
     subject = render_to_string("account/email/password_change_subject.txt", ctx)
     subject = "".join(subject.splitlines())
     message = render_to_string("account/email/password_change.txt", ctx)
     #send_mail(subject, message, current_site.email, [user.email])
     send_mail(subject, message, "%s <%s>" %(current_site.school.title, current_site.school.email), [user.email])
Ejemplo n.º 5
0
 def form_valid(self, form):
     self.created_user = self.create_user(form, commit=False)
     if settings.ACCOUNT_EMAIL_CONFIRMATION_REQUIRED:
         self.created_user.is_active = False
     # prevent User post_save signal from creating an Account instance
     # we want to handle that ourself.
     self.created_user._disable_account_creation = True
     self.created_user.save()
     self.create_account(form)
     email_kwargs = {
         "primary": True,
         "verified": False,
         "confirm": settings.ACCOUNT_EMAIL_CONFIRMATION_EMAIL,
         "site": get_current_site(self.request),
     }
     if self.signup_code:
         self.signup_code.use(self.created_user)
         if self.signup_code.email and self.created_user.email == self.signup_code.email:
             email_kwargs["verified"] = True
     email_address = EmailAddress.objects.add_email(self.created_user,
                                                    self.created_user.email,
                                                    **email_kwargs)
     self.after_signup(form)
     if settings.ACCOUNT_EMAIL_CONFIRMATION_EMAIL and not email_kwargs[
             "verified"]:
         email_address.send_confirmation(**email_kwargs)
     if settings.ACCOUNT_EMAIL_CONFIRMATION_REQUIRED and not email_kwargs[
             "verified"]:
         response_kwargs = {
             "request": self.request,
             "template": self.template_name_email_confirmation_sent,
             "context": {
                 "email": self.created_user.email,
                 "success_url": self.get_success_url(),
             }
         }
         return self.response_class(**response_kwargs)
     else:
         show_message = [
             settings.ACCOUNT_EMAIL_CONFIRMATION_EMAIL,
             self.messages.get("email_confirmation_sent"),
             not email_kwargs["verified"]
         ]
         if all(show_message):
             messages.add_message(
                 self.request,
                 self.messages["email_confirmation_sent"]["level"],
                 self.messages["email_confirmation_sent"]["text"] %
                 {"email": form.cleaned_data["email"]})
         self.login_user()
     return redirect(self.get_success_url())
Ejemplo n.º 6
0
 def update_email(self, form, confirm=None):
     user = self.request.user
     if confirm is None:
         confirm = settings.ACCOUNT_EMAIL_CONFIRMATION_EMAIL
     # @@@ handle multiple emails per user
     email = form.cleaned_data["email"].strip()
     if not self.primary_email_address:
         user.email = email
         site = get_current_site(self.request)
         EmailAddress.objects.add_email(self.request.user, email, primary=True, confirm=confirm, site=site)
         user.save()
     else:
         if email != self.primary_email_address.email:
             self.primary_email_address.change(email, confirm=confirm)
Ejemplo n.º 7
0
 def form_valid(self, form):
     self.created_user = self.create_user(form, commit=False)
     if settings.ACCOUNT_EMAIL_CONFIRMATION_REQUIRED:
         self.created_user.is_active = False
     # prevent User post_save signal from creating an Account instance
     # we want to handle that ourself.
     self.created_user._disable_account_creation = True
     self.created_user.save()
     self.create_account(form)
     email_kwargs = {
         "primary": True,
         "verified": False,
         "confirm": settings.ACCOUNT_EMAIL_CONFIRMATION_EMAIL,
         "site": get_current_site(self.request),
     }
     if self.signup_code:
         self.signup_code.use(self.created_user)
         if self.signup_code.email and self.created_user.email == self.signup_code.email:
             email_kwargs["verified"] = True
     email_address = EmailAddress.objects.add_email(self.created_user, self.created_user.email, **email_kwargs)
     self.after_signup(form)
     if settings.ACCOUNT_EMAIL_CONFIRMATION_EMAIL and not email_kwargs["verified"]:
         email_address.send_confirmation(**email_kwargs)
     if settings.ACCOUNT_EMAIL_CONFIRMATION_REQUIRED and not email_kwargs["verified"]:
         response_kwargs = {
             "request": self.request,
             "template": self.template_name_email_confirmation_sent,
             "context": {
                 "email": self.created_user.email,
                 "success_url": self.get_success_url(),
             }
         }
         return self.response_class(**response_kwargs)
     else:
         show_message = [
             settings.ACCOUNT_EMAIL_CONFIRMATION_EMAIL,
             self.messages.get("email_confirmation_sent"),
             not email_kwargs["verified"]
         ]
         if all(show_message):
             messages.add_message(
                 self.request,
                 self.messages["email_confirmation_sent"]["level"],
                 self.messages["email_confirmation_sent"]["text"] % {
                     "email": form.cleaned_data["email"]
                 }
             )
         self.login_user()
     return redirect(self.get_success_url())
Ejemplo n.º 8
0
 def send_email(self, user):
     protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
     current_site = get_current_site(self.request)
     ctx = {
         "user": user,
         "protocol": protocol,
         "current_site": current_site,
     }
     subject = render_to_string("account/email/password_change_subject.txt",
                                ctx)
     subject = "".join(subject.splitlines())
     message = render_to_string("account/email/password_change.txt", ctx)
     #send_mail(subject, message, current_site.email, [user.email])
     send_mail(
         subject, message,
         "%s <%s>" % (current_site.school.title, current_site.school.email),
         [user.email])
Ejemplo n.º 9
0
 def update_email(self, form, confirm=None):
     user = self.request.user
     if confirm is None:
         confirm = settings.ACCOUNT_EMAIL_CONFIRMATION_EMAIL
     # @@@ handle multiple emails per user
     email = form.cleaned_data["email"].strip()
     if not self.primary_email_address:
         user.email = email
         site = get_current_site(self.request)
         EmailAddress.objects.add_email(self.request.user,
                                        email,
                                        primary=True,
                                        confirm=confirm,
                                        site=site)
         user.save()
     else:
         if email != self.primary_email_address.email:
             self.primary_email_address.change(email, confirm=confirm)
Ejemplo n.º 10
0
 def send_email(self, email):
     protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
     current_site = get_current_site(self.request)
     for user in User.objects.filter(email__iexact=email):
         uid = int_to_base36(user.id)
         token = self.make_token(user)
         password_reset_url = u"%s://%s%s" % (
             protocol,
             unicode(current_site.domain),
             reverse("account_password_reset_token", kwargs=dict(uidb36=uid, token=token))
         )
         ctx = {
             "user": user,
             "current_site": current_site,
             "password_reset_url": password_reset_url,
         }
         subject = render_to_string("account/email/password_reset_subject.txt", ctx)
         subject = "".join(subject.splitlines())
         message = render_to_string("account/email/password_reset.txt", ctx)
         send_mail(subject, message, "%s <%s>" %(current_site.school.title, current_site.school.email), [user.email])
Ejemplo n.º 11
0
 def send_email(self, email):
     protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
     current_site = get_current_site(self.request)
     for user in User.objects.filter(email__iexact=email):
         uid = int_to_base36(user.id)
         token = self.make_token(user)
         password_reset_url = u"%s://%s%s" % (
             protocol, unicode(current_site.domain),
             reverse("account_password_reset_token",
                     kwargs=dict(uidb36=uid, token=token)))
         ctx = {
             "user": user,
             "current_site": current_site,
             "password_reset_url": password_reset_url,
         }
         subject = render_to_string(
             "account/email/password_reset_subject.txt", ctx)
         subject = "".join(subject.splitlines())
         message = render_to_string("account/email/password_reset.txt", ctx)
         send_mail(
             subject, message, "%s <%s>" %
             (current_site.school.title, current_site.school.email),
             [user.email])
Ejemplo n.º 12
0
 def send_email_confirmation(self, email_address):
     email_address.send_confirmation(site=get_current_site(self.request))