actions: Extract revoke_preregistration_users function.

This commit is contained in:
Mateusz Mandera 2021-02-12 14:43:47 +01:00 committed by Tim Abbott
parent 4b903c5dcd
commit c651bed0d4
1 changed files with 29 additions and 16 deletions

View File

@ -499,23 +499,10 @@ def process_new_human_user(
user=f"{user_profile.full_name} <`{user_profile.email}`>" user=f"{user_profile.full_name} <`{user_profile.email}`>"
), ),
) )
# Mark any other PreregistrationUsers in the realm that are STATUS_ACTIVE as
# inactive so we can keep track of the PreregistrationUser we
# actually used for analytics.
# In the special case of realm creation, there can be no additional PreregistrationUser
# for us to want to modify.
if prereg_user is not None and not realm_creation:
PreregistrationUser.objects.filter(
email__iexact=user_profile.delivery_email, realm=user_profile.realm
).exclude(id=prereg_user.id).update(status=confirmation_settings.STATUS_REVOKED)
if prereg_user.referred_by is not None: revoke_preregistration_users(user_profile, prereg_user, realm_creation)
notify_invites_changed(user_profile) if not realm_creation and prereg_user is not None and prereg_user.referred_by is not None:
elif prereg_user is None: notify_invites_changed(user_profile)
assert not realm_creation
PreregistrationUser.objects.filter(
email__iexact=user_profile.delivery_email, realm=user_profile.realm
).update(status=confirmation_settings.STATUS_REVOKED)
notify_new_user(user_profile) notify_new_user(user_profile)
# Clear any scheduled invitation emails to prevent them # Clear any scheduled invitation emails to prevent them
@ -531,6 +518,32 @@ def process_new_human_user(
send_initial_pms(user_profile) send_initial_pms(user_profile)
def revoke_preregistration_users(
created_user_profile: UserProfile,
used_preregistration_user: Optional[PreregistrationUser],
realm_creation: bool,
) -> None:
if realm_creation and used_preregistration_user is None:
raise AssertionError("realm_creation should only happen with a PreregistrationUser")
# Mark any other PreregistrationUsers in the realm that are STATUS_ACTIVE as
# inactive so we can keep track of the PreregistrationUser we
# actually used for analytics.
# In the special case of realm creation, there can be no additional PreregistrationUser
# for us to want to modify.
if used_preregistration_user is not None and not realm_creation:
PreregistrationUser.objects.filter(
email__iexact=created_user_profile.delivery_email, realm=created_user_profile.realm
).exclude(id=used_preregistration_user.id).update(
status=confirmation_settings.STATUS_REVOKED
)
elif used_preregistration_user is None:
assert not realm_creation
PreregistrationUser.objects.filter(
email__iexact=created_user_profile.delivery_email, realm=created_user_profile.realm
).update(status=confirmation_settings.STATUS_REVOKED)
def notify_created_user(user_profile: UserProfile) -> None: def notify_created_user(user_profile: UserProfile) -> None:
user_row = user_profile_to_user_row(user_profile) user_row = user_profile_to_user_row(user_profile)
person = format_user_row( person = format_user_row(