From ce58974c3584bcb6023d8d2a7091716f5eb4c1b5 Mon Sep 17 00:00:00 2001 From: Marcelo Cardoso Date: Thu, 4 Jun 2020 22:58:54 -0300 Subject: [PATCH] feat: default superuser migration --- .../0004_create_default_superuser.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 backend/migrations/0004_create_default_superuser.py diff --git a/backend/migrations/0004_create_default_superuser.py b/backend/migrations/0004_create_default_superuser.py new file mode 100644 index 0000000..f83c263 --- /dev/null +++ b/backend/migrations/0004_create_default_superuser.py @@ -0,0 +1,29 @@ +import os +from django.db import migrations + +class Migration(migrations.Migration): + dependencies = [ + ('backend', '0003_auto_20200605_0041'), + ] + + def generate_superuser(apps, schema_editor): + from django.contrib.auth.models import User + + SU_NAME = 'admin' + + try: + User.objects.get(username=SU_NAME) + except User.DoesNotExist: + SU_EMAIL = 'admin@example.com' + SU_PASSWORD = 'admin' + superuser = User.objects.create_superuser( + username=SU_NAME, + email=SU_EMAIL, + password=SU_PASSWORD) + superuser.is_superuser = True + superuser.is_staff = True + superuser.save() + + operations = [ + migrations.RunPython(generate_superuser), + ] \ No newline at end of file