Example #1
0
 def invite_web_user_form(self):
     role_choices = get_editable_role_choices(self.domain,
                                              self.request.couch_user,
                                              allow_admin_role=True)
     loc = None
     domain_request = DomainRequest.objects.get(
         id=self.request_id) if self.request_id else None
     initial = {
         'email': domain_request.email if domain_request else None,
     }
     if 'location_id' in self.request.GET:
         from corehq.apps.locations.models import SQLLocation
         loc = SQLLocation.objects.get(
             location_id=self.request.GET.get('location_id'))
     if self.request.method == 'POST':
         current_users = [
             user.username for user in WebUser.by_domain(self.domain)
         ]
         pending_invites = [
             di.email for di in Invitation.by_domain(self.domain)
         ]
         return AdminInvitesUserForm(self.request.POST,
                                     excluded_emails=current_users +
                                     pending_invites,
                                     role_choices=role_choices,
                                     domain=self.domain)
     return AdminInvitesUserForm(initial=initial,
                                 role_choices=role_choices,
                                 domain=self.domain,
                                 location=loc)
Example #2
0
def get_domain_info(domain,
                    upload_domain,
                    user_specs,
                    domain_info_by_domain,
                    upload_user=None,
                    group_memoizer=None,
                    is_web_upload=False):
    from corehq.apps.users.views.mobile.custom_data_fields import UserFieldsView
    domain_info = domain_info_by_domain.get(domain)
    if domain_info:
        return domain_info
    if domain == upload_domain:
        domain_group_memoizer = group_memoizer or GroupMemoizer(domain)
    else:
        domain_group_memoizer = GroupMemoizer(domain)
    domain_group_memoizer.load_all()
    can_assign_locations = domain_has_privilege(domain, privileges.LOCATIONS)
    location_cache = None
    if can_assign_locations:
        location_cache = SiteCodeToLocationCache(domain)

    domain_obj = Domain.get_by_name(domain)
    allowed_group_names = [
        group.name for group in domain_group_memoizer.groups
    ]
    profiles_by_name = {}
    domain_user_specs = [
        spec for spec in user_specs
        if spec.get('domain', upload_domain) == domain
    ]
    if is_web_upload:
        roles_by_name = {
            role[1]: role[0]
            for role in get_editable_role_choices(
                domain, upload_user, allow_admin_role=True)
        }
        validators = get_user_import_validators(
            Domain.get_by_name(domain),
            domain_user_specs,
            True,
            allowed_roles=list(roles_by_name),
            upload_domain=upload_domain,
        )
    else:
        roles_by_name = {
            role.name: role
            for role in UserRole.by_domain(domain)
        }
        definition = CustomDataFieldsDefinition.get(domain,
                                                    UserFieldsView.field_type)
        if definition:
            profiles_by_name = {
                profile.name: profile
                for profile in definition.get_profiles()
            }
        validators = get_user_import_validators(domain_obj, domain_user_specs,
                                                False, allowed_group_names,
                                                list(roles_by_name),
                                                list(profiles_by_name),
                                                upload_domain)

    domain_info = DomainInfo(validators, can_assign_locations, location_cache,
                             roles_by_name, profiles_by_name,
                             domain_group_memoizer)
    domain_info_by_domain[domain] = domain_info
    return domain_info
Example #3
0
 def user_role_choices(self):
     return get_editable_role_choices(self.domain,
                                      self.request.couch_user,
                                      allow_admin_role=True)