Spaces:
Sleeping
Sleeping
Fix: use email instead of username for superuser creation (User model uses email as USERNAME_FIELD)
Browse files- Dockerfile +2 -4
- apps/users/management/commands/create_superuser.py +21 -15
Dockerfile
CHANGED
|
@@ -40,18 +40,16 @@ RUN python manage.py migrate --noinput
|
|
| 40 |
# Initialiser les données par défaut (social links, stats, tools, testimonials, badges, opportunities)
|
| 41 |
RUN python manage.py init_data
|
| 42 |
|
| 43 |
-
|
| 44 |
-
RUN python manage.py create_superuser --username Rino --email admin@edulab.africa --password rinogeek
|
| 45 |
|
| 46 |
# Collecter les fichiers statiques
|
| 47 |
RUN python manage.py collectstatic --noinput
|
| 48 |
|
| 49 |
-
# Créer un script de démarrage
|
| 50 |
RUN echo '#!/bin/bash\n\
|
| 51 |
echo "=== Application Startup ===" \n\
|
| 52 |
python manage.py migrate --noinput\n\
|
| 53 |
python manage.py init_data\n\
|
| 54 |
-
python manage.py create_superuser --
|
| 55 |
exec gunicorn --bind 0.0.0.0:7860 --workers 2 --timeout 120 educonnect.wsgi:application\n\
|
| 56 |
' > /app/start.sh && chmod +x /app/start.sh
|
| 57 |
|
|
|
|
| 40 |
# Initialiser les données par défaut (social links, stats, tools, testimonials, badges, opportunities)
|
| 41 |
RUN python manage.py init_data
|
| 42 |
|
| 43 |
+
RUN python manage.py create_superuser --email admin@edulab.africa --password rinogeek --name "Rino Admin"
|
|
|
|
| 44 |
|
| 45 |
# Collecter les fichiers statiques
|
| 46 |
RUN python manage.py collectstatic --noinput
|
| 47 |
|
|
|
|
| 48 |
RUN echo '#!/bin/bash\n\
|
| 49 |
echo "=== Application Startup ===" \n\
|
| 50 |
python manage.py migrate --noinput\n\
|
| 51 |
python manage.py init_data\n\
|
| 52 |
+
python manage.py create_superuser --email admin@edulab.africa --password rinogeek --name "Rino Admin"\n\
|
| 53 |
exec gunicorn --bind 0.0.0.0:7860 --workers 2 --timeout 120 educonnect.wsgi:application\n\
|
| 54 |
' > /app/start.sh && chmod +x /app/start.sh
|
| 55 |
|
apps/users/management/commands/create_superuser.py
CHANGED
|
@@ -3,6 +3,7 @@ Commande pour créer un superutilisateur par défaut
|
|
| 3 |
"""
|
| 4 |
from django.core.management.base import BaseCommand
|
| 5 |
from django.contrib.auth import get_user_model
|
|
|
|
| 6 |
|
| 7 |
User = get_user_model()
|
| 8 |
|
|
@@ -11,11 +12,6 @@ class Command(BaseCommand):
|
|
| 11 |
help = 'Créer un superutilisateur par défaut si aucun n\'existe'
|
| 12 |
|
| 13 |
def add_arguments(self, parser):
|
| 14 |
-
parser.add_argument(
|
| 15 |
-
'--username',
|
| 16 |
-
default='Rino',
|
| 17 |
-
help='Nom d\'utilisateur du superuser'
|
| 18 |
-
)
|
| 19 |
parser.add_argument(
|
| 20 |
'--email',
|
| 21 |
default='admin@edulab.africa',
|
|
@@ -26,11 +22,16 @@ class Command(BaseCommand):
|
|
| 26 |
default='rinogeek',
|
| 27 |
help='Mot de passe du superuser'
|
| 28 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def handle(self, *args, **options):
|
| 31 |
-
username = options['username']
|
| 32 |
email = options['email']
|
| 33 |
password = options['password']
|
|
|
|
| 34 |
|
| 35 |
# Vérifier si un superuser existe déjà
|
| 36 |
if User.objects.filter(is_superuser=True).exists():
|
|
@@ -40,35 +41,40 @@ class Command(BaseCommand):
|
|
| 40 |
return
|
| 41 |
|
| 42 |
# Vérifier si l'utilisateur existe déjà
|
| 43 |
-
if User.objects.filter(
|
| 44 |
-
user = User.objects.get(
|
| 45 |
if not user.is_superuser:
|
| 46 |
user.is_superuser = True
|
| 47 |
user.is_staff = True
|
|
|
|
| 48 |
user.save()
|
| 49 |
self.stdout.write(
|
| 50 |
-
self.style.SUCCESS(f'Utilisateur "{
|
| 51 |
)
|
| 52 |
else:
|
| 53 |
self.stdout.write(
|
| 54 |
-
self.style.WARNING(f'Utilisateur "{
|
| 55 |
)
|
| 56 |
return
|
| 57 |
|
| 58 |
# Créer le superutilisateur
|
| 59 |
user = User.objects.create_superuser(
|
| 60 |
-
username=username,
|
| 61 |
email=email,
|
| 62 |
-
password=password
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
self.stdout.write(
|
| 68 |
self.style.SUCCESS(
|
| 69 |
f'✓ Superutilisateur créé avec succès!\n'
|
| 70 |
-
f' Username: {username}\n'
|
| 71 |
f' Email: {email}\n'
|
|
|
|
| 72 |
f' Password: {password}'
|
| 73 |
)
|
| 74 |
)
|
|
|
|
| 3 |
"""
|
| 4 |
from django.core.management.base import BaseCommand
|
| 5 |
from django.contrib.auth import get_user_model
|
| 6 |
+
from apps.users.models import UserProfile
|
| 7 |
|
| 8 |
User = get_user_model()
|
| 9 |
|
|
|
|
| 12 |
help = 'Créer un superutilisateur par défaut si aucun n\'existe'
|
| 13 |
|
| 14 |
def add_arguments(self, parser):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
parser.add_argument(
|
| 16 |
'--email',
|
| 17 |
default='admin@edulab.africa',
|
|
|
|
| 22 |
default='rinogeek',
|
| 23 |
help='Mot de passe du superuser'
|
| 24 |
)
|
| 25 |
+
parser.add_argument(
|
| 26 |
+
'--name',
|
| 27 |
+
default='Rino Admin',
|
| 28 |
+
help='Nom complet du superuser'
|
| 29 |
+
)
|
| 30 |
|
| 31 |
def handle(self, *args, **options):
|
|
|
|
| 32 |
email = options['email']
|
| 33 |
password = options['password']
|
| 34 |
+
name = options['name']
|
| 35 |
|
| 36 |
# Vérifier si un superuser existe déjà
|
| 37 |
if User.objects.filter(is_superuser=True).exists():
|
|
|
|
| 41 |
return
|
| 42 |
|
| 43 |
# Vérifier si l'utilisateur existe déjà
|
| 44 |
+
if User.objects.filter(email=email).exists():
|
| 45 |
+
user = User.objects.get(email=email)
|
| 46 |
if not user.is_superuser:
|
| 47 |
user.is_superuser = True
|
| 48 |
user.is_staff = True
|
| 49 |
+
user.role = 'ADMIN'
|
| 50 |
user.save()
|
| 51 |
self.stdout.write(
|
| 52 |
+
self.style.SUCCESS(f'Utilisateur "{email}" promu superutilisateur.')
|
| 53 |
)
|
| 54 |
else:
|
| 55 |
self.stdout.write(
|
| 56 |
+
self.style.WARNING(f'Utilisateur "{email}" existe déjà.')
|
| 57 |
)
|
| 58 |
return
|
| 59 |
|
| 60 |
# Créer le superutilisateur
|
| 61 |
user = User.objects.create_superuser(
|
|
|
|
| 62 |
email=email,
|
| 63 |
+
password=password
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Créer le profil utilisateur
|
| 67 |
+
UserProfile.objects.create(
|
| 68 |
+
user=user,
|
| 69 |
+
name=name,
|
| 70 |
+
is_current=True
|
| 71 |
)
|
| 72 |
|
| 73 |
self.stdout.write(
|
| 74 |
self.style.SUCCESS(
|
| 75 |
f'✓ Superutilisateur créé avec succès!\n'
|
|
|
|
| 76 |
f' Email: {email}\n'
|
| 77 |
+
f' Name: {name}\n'
|
| 78 |
f' Password: {password}'
|
| 79 |
)
|
| 80 |
)
|