Skip to content

Commit

Permalink
Merge pull request #65 from intuitem/CA-198-mail-error-resilience
Browse files Browse the repository at this point in the history
fix: handle backend crash on startup if mailer failed
  • Loading branch information
eric-intuitem authored Feb 22, 2024
2 parents 1770764 + c6caa6b commit 6796dfa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions backend/core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,12 @@ def startup():
CISO_ASSISTANT_SUPERUSER_EMAIL
and not User.objects.filter(email=CISO_ASSISTANT_SUPERUSER_EMAIL).exists()
):
User.objects.create_superuser(
email=CISO_ASSISTANT_SUPERUSER_EMAIL, is_superuser=True
)
try:
User.objects.create_superuser(
email=CISO_ASSISTANT_SUPERUSER_EMAIL, is_superuser=True
)
except Exception as e:
print(e) #NOTE: Add this exception in the logger
# if root folder does not exist, then create it
if not Folder.objects.filter(content_type=Folder.ContentType.ROOT).exists():
Folder.objects.create(
Expand Down
6 changes: 3 additions & 3 deletions backend/iam/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def _create_user(self, email, password, mailing=True, **extra_fields):
subject=_("Welcome to Ciso Assistant!"),
)
except Exception as exception:
print(f"sending email to {email} failed")
raise exception
return user

Expand All @@ -257,14 +258,13 @@ def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault("is_superuser", True)
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")
if not (EMAIL_HOST or EMAIL_HOST_RESCUE):
extra_fields.setdefault("mailing", False)
extra_fields.setdefault("mailing", not(password) and (EMAIL_HOST or EMAIL_HOST_RESCUE))
superuser = self._create_user(email, password, **extra_fields)
# when possible, add superuser to admin group
try:
UserGroup.objects.get(name="BI-UG-ADM").user_set.add(superuser)
except ObjectDoesNotExist:
pass
print("cannot add superuser to admin group - will be done on next startup")
return superuser


Expand Down

0 comments on commit 6796dfa

Please sign in to comment.