From 00ffa3e8706e76a2c6ebd0c563f10de4b207185b Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Wed, 15 Mar 2023 18:16:55 +0530 Subject: [PATCH] registration: Remove redundant code. Since we have updated the registration code to use PreregistrationRealm objects for realm creation in previous commits, some of the code has become redundant and this commit removes it. We remove the following code - - The modification to PreregistrationUser objects in process_new_human_user can now be done unconditionally because prereg_user is passed only during user creation and not realm creation. And we anyway do not expect any PreregistrationUser objects inside the realm during the creation. - There is no need of "realm_creation" parameter in create_preregistration_user function, since we now use create_preregistration_realm during realm creation. Fixes part of #24307. --- zerver/actions/create_user.py | 30 ++++++++++-------------- zerver/views/auth.py | 5 ---- zerver/views/development/registration.py | 4 +--- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.py index ff8442d479..25d84348fe 100644 --- a/zerver/actions/create_user.py +++ b/zerver/actions/create_user.py @@ -228,24 +228,20 @@ def process_new_human_user( prereg_user.created_user = user_profile prereg_user.save(update_fields=["status", "created_user"]) - # In the special case of realm creation, there can be no additional PreregistrationUser - # for us to want to modify - because other realm_creation PreregistrationUsers should be - # left usable for creating different realms. - if not realm_creation: - # Mark any other PreregistrationUsers in the realm that are STATUS_USED as - # inactive so we can keep track of the PreregistrationUser we - # actually used for analytics. - if prereg_user is not None: - PreregistrationUser.objects.filter( - email__iexact=user_profile.delivery_email, realm=user_profile.realm - ).exclude(id=prereg_user.id).update(status=confirmation_settings.STATUS_REVOKED) - else: - PreregistrationUser.objects.filter( - email__iexact=user_profile.delivery_email, realm=user_profile.realm - ).update(status=confirmation_settings.STATUS_REVOKED) + # Mark any other PreregistrationUsers in the realm that are STATUS_USED as + # inactive so we can keep track of the PreregistrationUser we + # actually used for analytics. + if prereg_user is not None: + PreregistrationUser.objects.filter( + email__iexact=user_profile.delivery_email, realm=user_profile.realm + ).exclude(id=prereg_user.id).update(status=confirmation_settings.STATUS_REVOKED) + else: + PreregistrationUser.objects.filter( + email__iexact=user_profile.delivery_email, realm=user_profile.realm + ).update(status=confirmation_settings.STATUS_REVOKED) - if prereg_user is not None and prereg_user.referred_by is not None: - notify_invites_changed(user_profile.realm) + if prereg_user is not None and prereg_user.referred_by is not None: + notify_invites_changed(user_profile.realm) notify_new_user(user_profile) # Clear any scheduled invitation emails to prevent them diff --git a/zerver/views/auth.py b/zerver/views/auth.py index 5a8fce7bc0..eda081806b 100644 --- a/zerver/views/auth.py +++ b/zerver/views/auth.py @@ -118,18 +118,13 @@ def get_safe_redirect_to(url: str, redirect_host: str) -> str: def create_preregistration_user( email: str, realm: Optional[Realm], - realm_creation: bool = False, password_required: bool = True, full_name: Optional[str] = None, full_name_validated: bool = False, multiuse_invite: Optional[MultiuseInvite] = None, ) -> PreregistrationUser: - assert not (realm_creation and realm is not None) - assert not (realm is None and not realm_creation) - return PreregistrationUser.objects.create( email=email, - realm_creation=realm_creation, password_required=password_required, realm=realm, full_name=full_name, diff --git a/zerver/views/development/registration.py b/zerver/views/development/registration.py index 63d568d347..e4750eab07 100644 --- a/zerver/views/development/registration.py +++ b/zerver/views/development/registration.py @@ -49,9 +49,7 @@ def register_development_user(request: HttpRequest) -> HttpResponse: count = UserProfile.objects.count() name = f"user-{count}" email = f"{name}@zulip.com" - prereg = create_preregistration_user( - email, realm, realm_creation=False, password_required=False - ) + prereg = create_preregistration_user(email, realm, password_required=False) activation_url = create_confirmation_link(prereg, Confirmation.USER_REGISTRATION) key = activation_url.split("/")[-1] # Need to add test data to POST request as it doesn't originally contain the required parameters