From dd3dddc86feda049b43a22c3f82a3a3fa9253072 Mon Sep 17 00:00:00 2001
From: James <james39.smith@live.uwe.ac.uk>
Date: Wed, 27 Mar 2024 19:24:09 +0000
Subject: [PATCH]  added script to auto populate the DB with 1 of each user
 type including superuser

---
 myproject/Dockerfile                          |  1 +
 myproject/debug.log                           |  3 +++
 myproject/entrypoint.sh                       |  1 +
 .../myapp/management/commands/__init__.py     |  1 +
 .../myapp/management/commands/make_users.py   | 22 +++++++++++++++++++
 5 files changed, 28 insertions(+)
 create mode 100644 myproject/myapp/management/commands/__init__.py
 create mode 100644 myproject/myapp/management/commands/make_users.py

diff --git a/myproject/Dockerfile b/myproject/Dockerfile
index df224fd..4cbaea8 100644
--- a/myproject/Dockerfile
+++ b/myproject/Dockerfile
@@ -50,3 +50,4 @@ RUN python manage.py collectstatic --noinput
 
 COPY entrypoint.sh /entrypoint.sh
 RUN chmod +x /entrypoint.sh
+
diff --git a/myproject/debug.log b/myproject/debug.log
index 421ec9a..3e46aa6 100644
--- a/myproject/debug.log
+++ b/myproject/debug.log
@@ -1158,3 +1158,6 @@ Traceback (most recent call last):
   File "/usr/local/lib/python3.11/site-packages/django/template/base.py", line 555, in invalid_block_tag
     raise self.error(
 django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 262: 'endblock', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?
+Watching for file changes with StatReloader
+Not Found: /favicon.ico
+Watching for file changes with StatReloader
diff --git a/myproject/entrypoint.sh b/myproject/entrypoint.sh
index e882418..40cb5fe 100644
--- a/myproject/entrypoint.sh
+++ b/myproject/entrypoint.sh
@@ -6,5 +6,6 @@ python manage.py makemigrations
 
 echo "Applying migrations"
 python manage.py migrate
+python manage.py make_users
 
 python manage.py runserver 0.0.0.0:8000
\ No newline at end of file
diff --git a/myproject/myapp/management/commands/__init__.py b/myproject/myapp/management/commands/__init__.py
new file mode 100644
index 0000000..203562b
--- /dev/null
+++ b/myproject/myapp/management/commands/__init__.py
@@ -0,0 +1 @@
+# __init__.py
\ No newline at end of file
diff --git a/myproject/myapp/management/commands/make_users.py b/myproject/myapp/management/commands/make_users.py
new file mode 100644
index 0000000..8ab8b98
--- /dev/null
+++ b/myproject/myapp/management/commands/make_users.py
@@ -0,0 +1,22 @@
+# app/management/commands/create_users.py
+from django.core.management.base import BaseCommand
+from django.contrib.auth.hashers import make_password
+from django.contrib.auth import get_user_model
+
+class Command(BaseCommand):
+    def handle(self, *args, **options):
+        Profile = get_user_model()
+        if not Profile.objects.filter(username="superuser").exists():
+            Profile.objects.create_superuser("superuser", password="placeholder")
+
+        if not Profile.objects.filter(username="admin").exists():
+            Profile.objects.create_user("admin", password="placeholder")
+        
+        if not Profile.objects.filter(username="base_user").exists():
+            Profile.objects.create_user("base_user", password="placeholder")
+        
+        if not Profile.objects.filter(username="ml_engineer").exists():
+            Profile.objects.create_user("ml_engineer", password="placeholder")
+        
+        if not Profile.objects.filter(username="accountant").exists():
+            Profile.objects.create_user("accountant", password="placeholder")
\ No newline at end of file
-- 
GitLab