-
-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Custom Data to Web User Invite #35124
base: master
Are you sure you want to change the base?
Changes from all commits
deae220
e1e4800
f7cfae7
2a17686
9515780
5a2e552
a5f848e
9945897
6c798c6
8347c90
307933f
e0fc01e
a177ab0
63273ea
8405864
df8af4e
aec22cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,14 @@ | |
from crispy_forms import layout as crispy | ||
from crispy_forms.helper import FormHelper | ||
|
||
from corehq import privileges | ||
from corehq.apps.accounting.utils import domain_has_privilege | ||
from corehq.apps.analytics.tasks import track_workflow | ||
from corehq.apps.custom_data_fields.models import PROFILE_SLUG | ||
from corehq.apps.custom_data_fields.edit_entity import add_prefix, get_prefixed, with_prefix | ||
from corehq.apps.domain.forms import NoAutocompleteMixin, clean_password | ||
from corehq.apps.domain.models import Domain | ||
from corehq.apps.hqwebapp import crispy as hqcrispy | ||
from corehq.apps.programs.models import Program | ||
from corehq.toggles import WEB_USER_INVITE_ADDITIONAL_FIELDS | ||
from corehq.apps.users.forms import SelectUserLocationForm, BaseTableauUserForm | ||
from corehq.apps.users.models import CouchUser | ||
|
||
|
@@ -490,23 +491,22 @@ class AdminInvitesUserForm(SelectUserLocationForm): | |
|
||
def __init__(self, data=None, excluded_emails=None, is_add_user=None, | ||
role_choices=(), should_show_location=False, can_edit_tableau_config=False, | ||
*, domain, **kwargs): | ||
custom_data=None, *, domain, **kwargs): | ||
self.custom_data = custom_data if WEB_USER_INVITE_ADDITIONAL_FIELDS.enabled(domain) else None | ||
if data and self.custom_data: | ||
data = data.copy() | ||
custom_data_post_dict = self.custom_data.form.data | ||
data.update({k: v for k, v in custom_data_post_dict.items() if k not in data}) | ||
self.request = kwargs.get('request') | ||
super(AdminInvitesUserForm, self).__init__(domain=domain, data=data, **kwargs) | ||
self.can_edit_tableau_config = can_edit_tableau_config | ||
domain_obj = Domain.get_by_name(domain) | ||
self.fields['role'].choices = [('', _("Select a role"))] + role_choices | ||
if domain_obj: | ||
if domain_has_privilege(domain_obj.name, privileges.APP_USER_PROFILES): | ||
self.fields['profile'] = forms.ChoiceField(choices=(), label="Profile", required=False) | ||
from corehq.apps.users.views.mobile import UserFieldsView | ||
self.valid_profiles = UserFieldsView.get_user_accessible_profiles( | ||
self.domain, self.request.couch_user | ||
) | ||
if len(self.valid_profiles) > 0: | ||
self.fields['profile'].choices = [('', '')] + [ | ||
(profile.id, profile.name) for profile in self.valid_profiles | ||
] | ||
if self.custom_data: | ||
prefixed_fields = [] | ||
prefixed_fields = add_prefix(self.custom_data.form.fields, self.custom_data.prefix) | ||
self.fields.update(prefixed_fields) | ||
if domain_obj.commtrack_enabled: | ||
self.fields['program'] = forms.ChoiceField(label="Program", choices=(), required=False) | ||
programs = Program.by_domain(domain_obj.name) | ||
|
@@ -536,6 +536,10 @@ def __init__(self, data=None, excluded_emails=None, is_add_user=None, | |
'profile' if ('profile' in self.fields and len(self.fields['profile'].choices) > 0) else None, | ||
) | ||
] | ||
if self.custom_data: | ||
custom_data_fieldset = self.custom_data.make_fieldsets(prefixed_fields, data is not None, | ||
field_name_includes_prefix=True) | ||
fields.extend(custom_data_fieldset) | ||
if should_show_location: | ||
fields.append( | ||
crispy.Fieldset( | ||
|
@@ -579,13 +583,12 @@ def __init__(self, data=None, excluded_emails=None, is_add_user=None, | |
), | ||
) | ||
|
||
def clean_profile(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this not used anywhere else? I don't see any renaming where it is called. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
profile_id = self.cleaned_data['profile'] | ||
if profile_id and profile_id not in {str(p.id) for p in self.valid_profiles}: | ||
def _validate_profile(self, profile_id): | ||
valid_profile_ids = {choice[0] for choice in self.custom_data.form.fields[PROFILE_SLUG].widget.choices} | ||
if profile_id not in valid_profile_ids: | ||
raise forms.ValidationError( | ||
_('Invalid profile selected. Please select a valid profile.'), | ||
) | ||
return profile_id | ||
|
||
def clean_email(self): | ||
email = self.cleaned_data['email'].strip() | ||
|
@@ -611,6 +614,18 @@ def clean(self): | |
for field in cleaned_data: | ||
if isinstance(cleaned_data[field], str): | ||
cleaned_data[field] = cleaned_data[field].strip() | ||
|
||
if self.custom_data: | ||
prefixed_profile_key = with_prefix(PROFILE_SLUG, self.custom_data.prefix) | ||
prefixed_field_names = add_prefix(self.custom_data.form.fields, self.custom_data.prefix).keys() | ||
custom_user_data = {key: cleaned_data.pop(key) for key in prefixed_field_names if key in cleaned_data} | ||
|
||
if prefixed_profile_key in custom_user_data: | ||
profile_id = custom_user_data.pop(prefixed_profile_key) | ||
self._validate_profile(profile_id) | ||
cleaned_data['profile'] = profile_id | ||
cleaned_data['custom_user_data'] = get_prefixed(custom_user_data, self.custom_data.prefix) | ||
|
||
return cleaned_data | ||
|
||
def _initialize_tableau_fields(self, data, domain): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -552,7 +552,7 @@ def add_domain_membership(self, domain, timezone=None, **kwargs): | |
|
||
def add_as_web_user(self, domain, role, primary_location_id=None, | ||
assigned_location_ids=None, program_id=None, profile=None, | ||
tableau_role=None, tableau_group_ids=None): | ||
tableau_role=None, tableau_group_ids=None, custom_user_data=None): | ||
if assigned_location_ids is None: | ||
assigned_location_ids = [] | ||
domain_obj = Domain.get_by_name(domain) | ||
|
@@ -564,9 +564,11 @@ def add_as_web_user(self, domain, role, primary_location_id=None, | |
if primary_location_id: | ||
self.set_location(domain, primary_location_id, commit=False) | ||
self.reset_locations(domain, assigned_location_ids, commit=False) | ||
user_data = self.get_user_data(domain_obj.name) | ||
if domain_has_privilege(domain_obj.name, privileges.APP_USER_PROFILES) and profile: | ||
user_data = self.get_user_data(domain_obj.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does still require the domain privilege check for profiles and I believe it does do the check. Or maybe i'm misunderstanding your comment |
||
user_data.update({}, profile_id=profile.id) | ||
if custom_user_data: | ||
user_data.update(custom_user_data) | ||
if TABLEAU_USER_SYNCING.enabled(domain) and (tableau_role or tableau_group_ids): | ||
if tableau_group_ids is None: | ||
tableau_group_ids = [] | ||
|
@@ -2853,6 +2855,7 @@ def accept_invitation_and_join_domain(self, web_user): | |
assigned_location_ids=list(self.assigned_locations.all().values_list('location_id', flat=True)), | ||
program_id=self.program, | ||
profile=self.profile, | ||
custom_user_data=self.custom_user_data, | ||
tableau_role=self.tableau_role, | ||
tableau_group_ids=self.tableau_group_ids | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be missing something but why is this FF not being used anymore? Is it effectively putting all this behind the new flag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should still be behind the domain privilege check, but it's now done from the
CustomDataEditor
form here If that conditional isfalse
, then theprofile
field here won't be added to the fields meaning thatself.custom_data.form.fields
will also not contain the profile field.