@@ -47,22 +47,33 @@ def admin(ctx: Context):
4747
4848
4949@admin .command ()
50- def setup ():
50+ @click .option (
51+ "--no-interactive" , is_flag = True , default = False , help = "Run the command in no interactive mode."
52+ )
53+ def setup (no_interactive ):
5154 """Run initialization tasks to configure GrimoireLab.
5255
5356 This command will create the database, tables and apply the
5457 defined migrations and fixtures.
58+
59+ It will also install the static files and prompt for the
60+ creation of the superuser to access the admin interface.
61+
62+ If the command is run with --no-interactive option, the superuser
63+ will be created using the environment variables.
5564 """
56- _setup ()
65+ interactive = not no_interactive
66+ _setup (interactive )
5767
5868
59- def _setup ():
69+ def _setup (interactive : bool ):
6070 """Setup GrimoireLab"""
6171
6272 _create_database ()
6373 _setup_database ()
6474 _install_static_files ()
6575 _create_system_user ()
76+ _create_superuser (interactive )
6677
6778 click .secho ("\n Grimoirelab configuration completed" , fg = "bright_cyan" )
6879
@@ -135,6 +146,35 @@ def _create_system_user():
135146 click .echo ("System user created." )
136147
137148
149+ def _create_superuser (interactive : bool ):
150+ """Create the superuser if it does not exist"""
151+
152+ click .echo ("## GrimoireLab superuser configuration\n " )
153+
154+ env = os .environ
155+ username = env .get ("GRIMOIRELAB_SUPERUSER_USERNAME" )
156+ password = env .get ("GRIMOIRELAB_SUPERUSER_PASSWORD" )
157+ if username and password :
158+ # If both username and password are provided, create the superuser non-interactively
159+ interactive = False
160+ env ["DJANGO_SUPERUSER_USERNAME" ] = username
161+ env ["DJANGO_SUPERUSER_PASSWORD" ] = password
162+ env ["DJANGO_SUPERUSER_EMAIL" ] = "noreply@localhost"
163+
164+ if interactive :
165+ create = input ("Do you want to create a superuser? [y/N]: " )
166+ if create .lower () != "y" :
167+ click .echo ("Superuser creation skipped." )
168+ return
169+ try :
170+ kwargs = {
171+ "interactive" : interactive ,
172+ }
173+ django .core .management .call_command ("createsuperuser" , ** kwargs )
174+ except django .core .management .base .CommandError as error :
175+ click .echo (f"Superuser creation skipped: { error } " )
176+
177+
138178@admin .command ()
139179@click .option ("--username" , help = "Specifies the login for the user." )
140180@click .option (
0 commit comments