def _create_user(request, is_staff=False): request_json = json.loads(request.body) if not request_json.get('email'): raise CreateUserException('Email is required') existing_user = User.objects.filter( email__iexact=request_json['email']).first() if existing_user: raise CreateUserException('This user already exists', existing_user=existing_user) username = User.objects.make_random_password() user = User.objects.create_user( username, email=request_json['email'], first_name=request_json.get('firstName') or '', last_name=request_json.get('lastName') or '', is_staff=is_staff, ) try: send_welcome_email(user, request.user) except AnymailError as e: raise CreateUserException(str(e), status_code=getattr(e, 'status_code', None) or 400) return user
def create_project_collaborator(request, project_guid): project = get_project_and_check_permissions(project_guid, request.user, can_edit=True) if project.workspace_name: raise PermissionDenied( 'Adding collaborators directly in seqr is disabled. Users can be managed from the associated AnVIL workspace' ) request_json = json.loads(request.body) if not request_json.get('email'): return create_json_response({'error': 'Email is required'}, status=400) existing_user = User.objects.filter( email__iexact=request_json['email']).first() if existing_user: return _update_existing_user(existing_user, project, request_json) username = User.objects.make_random_password() user = User.objects.create_user( username, email=request_json['email'], first_name=request_json.get('firstName') or '', last_name=request_json.get('lastName') or '', ) logger.info('Created user {} (local)'.format(request_json['email']), extra={'user': request.user}) try: send_welcome_email(user, request.user) except AnymailError as e: return create_json_response({'error': str(e)}, status=getattr(e, 'status_code', None) or 400, reason=str(e)) project.can_view_group.user_set.add(user) return create_json_response({ 'projectsByGuid': { project_guid: { 'collaborators': get_json_for_project_collaborator_list(request.user, project) } } })
def handle(self, *args, **options): user = User.objects.get(email__iexact=options['email_address']) referrer = User.objects.get(email__iexact=options['referrer']) send_welcome_email(user, referrer)