def create_user(self, name, email, password): """ Creates a new user with the provided information and creates a new group and application for him. :param name: The name of the new user. :param email: The e-mail address of the user, this is also the username. :param password: The password of the user. :raises: UserAlreadyExist: When a user with the provided username (email) already exists. :return: The user object. :rtype: User """ try: email = normalize_email_address(email) user = User(first_name=name, email=email, username=email) user.set_password(password) user.save() except IntegrityError: raise UserAlreadyExist( f'A user with username {email} already exists.') # Insert some initial data for the newly created user. core_handler = CoreHandler() group_user = core_handler.create_group(user=user, name=f"{name}'s group") # Call the user_created method for each plugin that is un the registry. for plugin in plugin_registry.registry.values(): plugin.user_created(user, group_user.group) return user
def create_user(self, name, email, password, group_invitation_token=None): """ Creates a new user with the provided information and creates a new group and application for him. If the optional group invitation is provided then the user joins that group without creating a new one. :param name: The name of the new user. :type name: str :param email: The e-mail address of the user, this is also the username. :type email: str :param password: The password of the user. :type password: str :param group_invitation_token: If provided and valid, the invitation will be accepted and and initial group will not be created. :type group_invitation_token: str :raises: UserAlreadyExist: When a user with the provided username (email) already exists. :raises GroupInvitationEmailMismatch: If the group invitation email does not match the one of the user. :return: The user object. :rtype: User """ email = normalize_email_address(email) if User.objects.filter(Q(email=email) | Q(username=email)).exists(): raise UserAlreadyExist( f'A user with username {email} already exists.') core_handler = CoreHandler() group_invitation = None group_user = None if group_invitation_token: group_invitation = core_handler.get_group_invitation_by_token( group_invitation_token) if email != group_invitation.email: raise GroupInvitationEmailMismatch( 'The email address of the invitation does not match the one of the ' 'user.') user = User(first_name=name, email=email, username=email) user.set_password(password) user.save() if group_invitation_token: group_user = core_handler.accept_group_invitation( user, group_invitation) if not group_user: group_user = core_handler.create_group(user=user, name=f"{name}'s group") # Call the user_created method for each plugin that is un the registry. for plugin in plugin_registry.registry.values(): plugin.user_created(user, group_user.group, group_invitation) return user
def test_create_group(data_fixture): user = data_fixture.create_user() handler = CoreHandler() handler.create_group(user=user, name='Test group') group = Group.objects.all().first() user_group = GroupUser.objects.all().first() assert group.name == 'Test group' assert user_group.user == user assert user_group.group == group assert user_group.order == 1 handler.create_group(user=user, name='Test group 2') assert Group.objects.all().count() == 2 assert GroupUser.objects.all().count() == 2
def test_create_group(send_mock, data_fixture): user = data_fixture.create_user() handler = CoreHandler() group_user = handler.create_group(user=user, name='Test group') send_mock.assert_called_once() assert send_mock.call_args[1]['group'].id == group_user.group.id assert send_mock.call_args[1]['user'].id == user.id group = Group.objects.all().first() user_group = GroupUser.objects.all().first() assert group.name == 'Test group' assert user_group.user == user assert user_group.group == group assert user_group.order == 1 assert user_group.permissions == GROUP_USER_PERMISSION_ADMIN handler.create_group(user=user, name='Test group 2') assert Group.objects.all().count() == 2 assert GroupUser.objects.all().count() == 2
def create_user(self, name, email, password, group_invitation_token=None, template=None): """ Creates a new user with the provided information and creates a new group and application for him. If the optional group invitation is provided then the user joins that group without creating a new one. :param name: The name of the new user. :type name: str :param email: The e-mail address of the user, this is also the username. :type email: str :param password: The password of the user. :type password: str :param group_invitation_token: If provided and valid, the invitation will be accepted and and initial group will not be created. :type group_invitation_token: str :param template: If provided, that template will be installed into the newly created group. :type template: Template :raises: UserAlreadyExist: When a user with the provided username (email) already exists. :raises GroupInvitationEmailMismatch: If the group invitation email does not match the one of the user. :raises SignupDisabledError: If signing up is disabled. :return: The user object. :rtype: User """ core_handler = CoreHandler() if not core_handler.get_settings().allow_new_signups: raise DisabledSignupError("Sign up is disabled.") email = normalize_email_address(email) if User.objects.filter(Q(email=email) | Q(username=email)).exists(): raise UserAlreadyExist( f"A user with username {email} already exists.") group_invitation = None group_user = None if group_invitation_token: group_invitation = core_handler.get_group_invitation_by_token( group_invitation_token) if email != group_invitation.email: raise GroupInvitationEmailMismatch( "The email address of the invitation does not match the one of the " "user.") user = User(first_name=name, email=email, username=email) user.set_password(password) if not User.objects.exists(): # This is the first ever user created in this baserow instance and # therefore the administrator user, lets give them staff rights so they # can set baserow wide settings. user.is_staff = True user.save() if group_invitation_token: group_user = core_handler.accept_group_invitation( user, group_invitation) if not group_user: group_user = core_handler.create_group(user=user, name=f"{name}'s group") if not group_invitation_token and template: core_handler.install_template(user, group_user.group, template) # Call the user_created method for each plugin that is un the registry. for plugin in plugin_registry.registry.values(): plugin.user_created(user, group_user.group, group_invitation, template) return user