models: Extract a helper function for whether emails are realm-public.

This is also a useful preparatory refactor for having a user setting
controlling whether one's own email address is publicly available
within the organization.
This commit is contained in:
Tim Abbott 2019-09-23 13:38:13 -07:00
parent 6bd977f9e4
commit 6e5c99328a
3 changed files with 10 additions and 5 deletions

View File

@ -837,7 +837,7 @@ def do_change_user_delivery_email(user_profile: UserProfile, new_email: str) ->
delete_user_profile_caches([user_profile])
user_profile.delivery_email = new_email
if user_profile.realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE:
if user_profile.email_address_is_realm_public():
user_profile.email = new_email
user_profile.save(update_fields=["email", "delivery_email"])
else:
@ -850,7 +850,7 @@ def do_change_user_delivery_email(user_profile: UserProfile, new_email: str) ->
event = dict(type='realm_user', op='update', person=payload)
send_event(user_profile.realm, event, [user_profile.id])
if user_profile.realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE:
if user_profile.email_address_is_realm_public():
# Additionally, if we're also changing the publicly visible
# email, we send a new_email event as well.
send_user_email_update_event(user_profile)

View File

@ -32,7 +32,7 @@ def copy_user_settings(source_profile: UserProfile, target_profile: UserProfile)
copy_hotpots(source_profile, target_profile)
def get_display_email_address(user_profile: UserProfile, realm: Realm) -> str:
if realm.email_address_visibility != Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE:
if not user_profile.email_address_is_realm_public():
return "user%s@%s" % (user_profile.id, get_fake_email_domain())
return user_profile.delivery_email
@ -68,7 +68,7 @@ def create_user_profile(realm: Realm, email: str, password: Optional[str],
if bot_type or not active:
password = None
if realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE:
if user_profile.email_address_is_realm_public():
# If emails are visible to everyone, we can set this here and save a DB query
user_profile.email = get_display_email_address(user_profile, realm)
user_profile.set_password(password)
@ -113,7 +113,7 @@ def create_user(email: str, password: Optional[str], realm: Realm,
else:
user_profile.save()
if realm.email_address_visibility != Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE:
if not user_profile.email_address_is_realm_public():
# With restricted access to email addresses, we can't generate
# the fake email addresses we use for display purposes without
# a User ID, which isn't generated until the .save() above.

View File

@ -1039,6 +1039,11 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
rows = UserProfile.objects.filter(id__in=user_ids).values('id', 'email')
return {row['id']: row['email'] for row in rows}
def email_address_is_realm_public(self) -> bool:
if self.realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE:
return True
return False
def can_create_streams(self) -> bool:
if self.is_realm_admin:
return True