Skip to content

Commit

Permalink
Refactor email validation logic for editing invites
Browse files Browse the repository at this point in the history
  • Loading branch information
minhaminha committed Dec 6, 2024
1 parent 71b8fc6 commit c1f1b8e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
11 changes: 6 additions & 5 deletions corehq/apps/registration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ def __init__(self, data=None, is_add_user=None,
role_choices=(), should_show_location=False, can_edit_tableau_config=False,
custom_data=None, invitation=None, *, domain, **kwargs):
self.custom_data = custom_data
self.invite = invitation
if data and self.custom_data:
data = data.copy()
custom_data_post_dict = self.custom_data.form.data
Expand All @@ -519,12 +520,12 @@ def __init__(self, data=None, is_add_user=None,
programs = Program.by_domain(domain_obj.name)
choices = [('', '')] + list((prog.get_id, prog.name) for prog in programs)
self.fields['program'].choices = choices
if invitation:
if self.invite:
# not sure if this works - remove this comment after testing on staging
self.fields['program'].initial = invitation.program
self.fields['program'].initial = self.invite.program

if self.can_edit_tableau_config:
self._initialize_tableau_fields(data, domain, invitation)
self._initialize_tableau_fields(data, domain, self.invite)

self.helper = FormHelper()
self.helper.form_method = 'POST'
Expand All @@ -534,7 +535,7 @@ def __init__(self, data=None, is_add_user=None,
self.helper.field_class = 'col-sm-9 col-md-8 col-lg-6'

save_button_text = "Send Invite"
if invitation:
if self.invite:
self.fields['email'].widget.attrs["readonly"] = True
save_button_text = "Update Invite"

Expand Down Expand Up @@ -602,7 +603,7 @@ def clean_email(self):
email = self.cleaned_data['email'].strip()

from corehq.apps.registration.validation import AdminInvitesUserFormValidator
error = AdminInvitesUserFormValidator.validate_email(self.domain, email)
error = AdminInvitesUserFormValidator.validate_email(self.domain, email, bool(self.invite))
if error:
raise forms.ValidationError(error)
return email
Expand Down
4 changes: 2 additions & 2 deletions corehq/apps/registration/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def validate_parameters(domain, upload_user, parameters):
return _("This domain does not have locations privileges.")

@staticmethod
def validate_email(domain, email):
def validate_email(domain, email, is_invite_edit=False):
current_users = [user.username.lower() for user in WebUser.by_domain(domain)]
pending_invites = [di.email.lower() for di in Invitation.by_domain(domain)]
current_users_and_pending_invites = current_users + pending_invites

if email.lower() in current_users_and_pending_invites:
if email.lower() in current_users_and_pending_invites and not is_invite_edit:
return _("A user with this email address is already in "
"this project or has a pending invitation.")
web_user = WebUser.get_by_username(email)
Expand Down
3 changes: 2 additions & 1 deletion corehq/apps/users/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,8 @@ def invite_web_user_form(self):
should_show_location=self.request.project.uses_locations,
can_edit_tableau_config=can_edit_tableau_config,
request=self.request,
custom_data=self.custom_data
custom_data=self.custom_data,
invitation=invitation
)
return AdminInvitesUserForm(
initial=initial,
Expand Down

0 comments on commit c1f1b8e

Please sign in to comment.