✨(project) first proof of concept printing pdf from markdown
This is a boilerplate inspired from https://github.com/openfun/joanie
This commit is contained in:
0
src/backend/demo/__init__.py
Normal file
0
src/backend/demo/__init__.py
Normal file
0
src/backend/demo/management/__init__.py
Normal file
0
src/backend/demo/management/__init__.py
Normal file
0
src/backend/demo/management/commands/__init__.py
Normal file
0
src/backend/demo/management/commands/__init__.py
Normal file
45
src/backend/demo/management/commands/createsuperuser.py
Normal file
45
src/backend/demo/management/commands/createsuperuser.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Create a superuser with an email and a password'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
"""Define required arguments "email" and "password"."""
|
||||
parser.add_argument(
|
||||
"--email",
|
||||
help=(
|
||||
"Email for the user."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--password",
|
||||
help='Password for the user.',
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
"""
|
||||
Given an email and a password, create a superuser or upgrade the existing
|
||||
user to superuser status.
|
||||
"""
|
||||
UserModel = get_user_model()
|
||||
email = options.get('email')
|
||||
try:
|
||||
user = UserModel.objects.get(email=email)
|
||||
except UserModel.DoesNotExist:
|
||||
user = UserModel(email=email)
|
||||
message = 'Superuser created successfully.'
|
||||
else:
|
||||
if user.is_superuser and user.is_staff:
|
||||
message = "Superuser already exists."
|
||||
else:
|
||||
message = "User already existed and was upgraded to superuser."
|
||||
|
||||
user.is_superuser = True
|
||||
user.is_staff = True
|
||||
user.set_password(options['password'])
|
||||
user.save()
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(message))
|
||||
23
src/backend/demo/utils.py
Normal file
23
src/backend/demo/utils.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from django.contrib.auth.management.commands.createsuperuser import Command as BaseCommand
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Create a superuser without a username field'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
# Check if a superuser already exists
|
||||
try:
|
||||
self.UserModel._default_manager.db_manager(options['database']).get(
|
||||
is_superuser=True,
|
||||
)
|
||||
except self.UserModel.DoesNotExist:
|
||||
# If not, create a superuser without a username
|
||||
email = options.get('email')
|
||||
password = options.get('password')
|
||||
self.UserModel._default_manager.db_manager(options['database']).create_superuser(
|
||||
email=email,
|
||||
password=password,
|
||||
)
|
||||
self.stdout.write(self.style.SUCCESS('Superuser created successfully.'))
|
||||
except ValidationError as e:
|
||||
self.stderr.write(self.style.ERROR(f'Error creating superuser: {", ".join(e.messages)}'))
|
||||
Reference in New Issue
Block a user