🔧 chore(utils.py): refactor setup_superuser and teardown_superuser functions to improve readability and remove unnecessary code duplication

🔧 chore(utils.py): update teardown_superuser function to only remove default superuser if AUTO_LOGIN is set to False
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-10-20 18:25:42 -03:00
commit f060155fe6

View file

@ -102,6 +102,9 @@ def get_or_create_super_user(session: Session, username, password, is_default):
def setup_superuser(settings_service, session: Session):
if settings_service.auth_settings.AUTO_LOGIN:
logger.debug("AUTO_LOGIN is set to True. Creating default superuser.")
else:
# Remove the default superuser if it exists
teardown_superuser(settings_service, session)
username = settings_service.auth_settings.SUPERUSER
password = settings_service.auth_settings.SUPERUSER_PASSWORD
@ -132,10 +135,12 @@ def teardown_superuser(settings_service, session):
# If AUTO_LOGIN is True, we will remove the default superuser
# from the database.
if settings_service.auth_settings.AUTO_LOGIN:
if not settings_service.auth_settings.AUTO_LOGIN:
try:
logger.debug("AUTO_LOGIN is set to True. Removing default superuser.")
username = settings_service.auth_settings.SUPERUSER
logger.debug(
"AUTO_LOGIN is set to False. Removing default superuser if exists."
)
username = DEFAULT_SUPERUSER
from langflow.services.database.models.user.user import User
user = session.query(User).filter(User.username == username).first()