services.py 718 B

123456789101112131415161718192021222324252627
  1. from django.core.exceptions import ValidationError
  2. from account.models import User, UserInstitution
  3. def add_user(first_name: str, last_name: str, email: str, is_staff: bool, institution_id: str) -> User:
  4. try:
  5. institution = UserInstitution.objects.get(id=institution_id)
  6. except UserInstitution.DoesNotExist:
  7. raise AccountServiceException("Institution does not exist")
  8. user = User()
  9. user.first_name = first_name
  10. user.last_name = last_name
  11. user.email = email
  12. user.is_staff = is_staff
  13. user.institution = institution
  14. try:
  15. user.full_clean()
  16. except ValidationError as e:
  17. raise AccountServiceException(str(e))
  18. user.save()
  19. return user