from django.core.exceptions import ValidationError from account.models import User, UserInstitution def add_user(first_name: str, last_name: str, email: str, is_staff: bool, institution_id: str) -> User: try: institution = UserInstitution.objects.get(id=institution_id) except UserInstitution.DoesNotExist: raise AccountServiceException("Institution does not exist") user = User() user.first_name = first_name user.last_name = last_name user.email = email user.is_staff = is_staff user.institution = institution try: user.full_clean() except ValidationError as e: raise AccountServiceException(str(e)) user.save() return user