mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	confirmation: Remove Confirmation.objects.send_confirmation.
I think it makes sense to wrest the email sending from confirmation, now
that we have a clean email-sending interface in send_email. A few other
reasons:
* send_confirmation is get_link_for_object followed by send_email, but those
  two functions have no arguments in common.
* Sending email through confirmation obfuscates the context dict, and is a
  relatively complicated piece of the codebase anyone trying to deal with
  the email system has to understand.
* The three emails previously being sent through confirmation don't have
  that much in common, other than that they happen to have a confirmation
  link in them.
The .split('/')[-1] in registration.py is a hack, but a hack used several
places in the codebase, so maybe one day get_link_for_object will also
return the confirmation_key.
			
			
This commit is contained in:
		@@ -55,7 +55,6 @@ def generate_realm_creation_url():
 | 
			
		||||
                                kwargs={'creation_key': key}))
 | 
			
		||||
 | 
			
		||||
class ConfirmationManager(models.Manager):
 | 
			
		||||
 | 
			
		||||
    def confirm(self, confirmation_key):
 | 
			
		||||
        # type: (str) -> Union[bool, PreregistrationUser, EmailChangeStatus]
 | 
			
		||||
        if B16_RE.search(confirmation_key):
 | 
			
		||||
@@ -90,19 +89,6 @@ class ConfirmationManager(models.Manager):
 | 
			
		||||
        # type: () -> int
 | 
			
		||||
        return getattr(settings, 'EMAIL_CONFIRMATION_DAYS', 10)
 | 
			
		||||
 | 
			
		||||
    def send_confirmation(self, obj, template_prefix, to_email, additional_context=None, host=None):
 | 
			
		||||
        # type: (ContentType, str, Text, Optional[Dict[str, Any]], Optional[str]) -> Confirmation
 | 
			
		||||
        confirmation_key = generate_key()
 | 
			
		||||
        activate_url = self.get_activation_url(confirmation_key, host=host)
 | 
			
		||||
        context = {
 | 
			
		||||
            'activate_url': activate_url,
 | 
			
		||||
        }
 | 
			
		||||
        if additional_context is not None:
 | 
			
		||||
            context.update(additional_context)
 | 
			
		||||
 | 
			
		||||
        send_email(template_prefix, to_email, from_email=settings.DEFAULT_FROM_EMAIL, context=context)
 | 
			
		||||
        return self.create(content_object=obj, date_sent=timezone_now(), confirmation_key=confirmation_key)
 | 
			
		||||
 | 
			
		||||
class EmailChangeConfirmationManager(ConfirmationManager):
 | 
			
		||||
    def get_activation_url(self, key, host=None):
 | 
			
		||||
        # type: (Text, Optional[str]) -> Text
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,7 @@ from zerver.lib.message import (
 | 
			
		||||
)
 | 
			
		||||
from zerver.lib.realm_icon import realm_icon_url
 | 
			
		||||
from zerver.lib.retention import move_message_to_archive
 | 
			
		||||
from zerver.lib.send_email import send_email
 | 
			
		||||
from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, \
 | 
			
		||||
    RealmDomain, \
 | 
			
		||||
    Subscription, Recipient, Message, Attachment, UserMessage, RealmAuditLog, \
 | 
			
		||||
@@ -630,22 +631,14 @@ def do_start_email_change_process(user_profile, new_email):
 | 
			
		||||
    # type: (UserProfile, Text) -> None
 | 
			
		||||
    old_email = user_profile.email
 | 
			
		||||
    user_profile.email = new_email
 | 
			
		||||
    obj = EmailChangeStatus.objects.create(new_email=new_email, old_email=old_email,
 | 
			
		||||
                                           user_profile=user_profile, realm=user_profile.realm)
 | 
			
		||||
 | 
			
		||||
    context = {'realm': user_profile.realm,
 | 
			
		||||
               'old_email': old_email,
 | 
			
		||||
               'new_email': new_email,
 | 
			
		||||
               }
 | 
			
		||||
 | 
			
		||||
    obj = EmailChangeStatus.objects.create(new_email=new_email,
 | 
			
		||||
                                           old_email=old_email,
 | 
			
		||||
                                           user_profile=user_profile,
 | 
			
		||||
                                           realm=user_profile.realm)
 | 
			
		||||
 | 
			
		||||
    EmailChangeConfirmation.objects.send_confirmation(
 | 
			
		||||
        obj, 'zerver/emails/confirm_new_email', new_email,
 | 
			
		||||
        additional_context=context,
 | 
			
		||||
        host=user_profile.realm.host,
 | 
			
		||||
    )
 | 
			
		||||
    activation_url = EmailChangeConfirmation.objects.get_link_for_object(obj, host=user_profile.realm.host)
 | 
			
		||||
    context = {'realm': user_profile.realm, 'old_email': old_email, 'new_email': new_email,
 | 
			
		||||
               'activate_url': activation_url}
 | 
			
		||||
    send_email('zerver/emails/confirm_new_email', new_email, from_email=settings.DEFAULT_FROM_EMAIL,
 | 
			
		||||
               context=context)
 | 
			
		||||
 | 
			
		||||
def compute_irc_user_fullname(email):
 | 
			
		||||
    # type: (NonBinaryStr) -> NonBinaryStr
 | 
			
		||||
@@ -3053,12 +3046,10 @@ def do_send_confirmation_email(invitee, referrer, body):
 | 
			
		||||
    `invitee` is a PreregistrationUser.
 | 
			
		||||
    `referrer` is a UserProfile.
 | 
			
		||||
    """
 | 
			
		||||
    context = {'referrer': referrer, 'custom_body': body}
 | 
			
		||||
 | 
			
		||||
    template_prefix = 'zerver/emails/invitation'
 | 
			
		||||
    Confirmation.objects.send_confirmation(
 | 
			
		||||
        invitee, template_prefix, invitee.email, additional_context=context,
 | 
			
		||||
        host=referrer.realm.host)
 | 
			
		||||
    activation_url = Confirmation.objects.get_link_for_object(invitee, host=referrer.realm.host)
 | 
			
		||||
    context = {'referrer': referrer, 'custom_body': body, 'activate_url': activation_url}
 | 
			
		||||
    send_email('zerver/emails/invitation', invitee.email, from_email=settings.DEFAULT_FROM_EMAIL,
 | 
			
		||||
               context=context)
 | 
			
		||||
 | 
			
		||||
def is_inactive(email):
 | 
			
		||||
    # type: (Text) -> None
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,7 @@ from zerver.models import UserProfile, Realm, PreregistrationUser, \
 | 
			
		||||
    name_changes_disabled, email_to_username, \
 | 
			
		||||
    completely_open, get_unique_open_realm, email_allowed_for_realm, \
 | 
			
		||||
    get_realm, get_realm_by_email_domain
 | 
			
		||||
from zerver.lib.send_email import send_email_to_user
 | 
			
		||||
from zerver.lib.send_email import send_email, send_email_to_user
 | 
			
		||||
from zerver.lib.events import do_events_register
 | 
			
		||||
from zerver.lib.actions import do_change_password, do_change_full_name, do_change_is_admin, \
 | 
			
		||||
    do_activate_user, do_create_user, do_create_realm, set_default_streams, \
 | 
			
		||||
@@ -298,10 +298,11 @@ def send_registration_completion_email(email, request, realm_creation=False):
 | 
			
		||||
    can complete their registration.
 | 
			
		||||
    """
 | 
			
		||||
    prereg_user = create_preregistration_user(email, request, realm_creation)
 | 
			
		||||
    confirmation = Confirmation.objects.send_confirmation(
 | 
			
		||||
        prereg_user, 'zerver/emails/confirm_registration', email, host=request.get_host())
 | 
			
		||||
    activation_url = Confirmation.objects.get_link_for_object(prereg_user, host=request.get_host())
 | 
			
		||||
    send_email('zerver/emails/confirm_registration', email, from_email=settings.DEFAULT_FROM_EMAIL,
 | 
			
		||||
               context={'activate_url': activation_url})
 | 
			
		||||
    if settings.DEVELOPMENT and realm_creation:
 | 
			
		||||
        request.session['confirmation_key'] = {'confirmation_key': confirmation.confirmation_key}
 | 
			
		||||
        request.session['confirmation_key'] = {'confirmation_key': activation_url.split('/')[-1]}
 | 
			
		||||
 | 
			
		||||
def redirect_to_email_login_url(email):
 | 
			
		||||
    # type: (str) -> HttpResponseRedirect
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user