From 6932ce357a2db75de48f14bc891419eb97902688 Mon Sep 17 00:00:00 2001
From: Noah Jonah Gente <fu2662cw@mi.fu-berlin.de>
Date: Tue, 8 Jun 2021 16:20:45 +0200
Subject: [PATCH] Add seed as a argument to seed_db command

---
 README.md                                        |  3 ++-
 unisportomat/quiz/management/commands/seed_db.py | 10 ++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index b8554f8..5db6a7f 100644
--- a/README.md
+++ b/README.md
@@ -74,9 +74,10 @@ If successful, you can now see the running server in your browser at `http://127
 ## Populate the database with test data
 To populate the database with some test data run
 ```
-python manage.py seed_db
+python manage.py seed_db [-y] [--seed SEED]
 ```
 All the existing data from your database will be lost!
+Run `python manage.py seed_db --help` for more information.
 
 ## Use the django admin interface to view and edit data during development
 If you started the server as described above, you can access the django admin interface on
diff --git a/unisportomat/quiz/management/commands/seed_db.py b/unisportomat/quiz/management/commands/seed_db.py
index 5ad94a2..7e6fc5b 100644
--- a/unisportomat/quiz/management/commands/seed_db.py
+++ b/unisportomat/quiz/management/commands/seed_db.py
@@ -39,6 +39,13 @@ class Command(BaseCommand):
             help="Don't ask to confirm database flushing",
         )
 
+        parser.add_argument(
+            "--seed",
+            type=int,
+            default=42,
+            help="Optional seed for random generator. Defaults to 42",
+        )
+
     def handle(self, *args, **options):
         """Create some objects for all models"""
 
@@ -49,6 +56,9 @@ class Command(BaseCommand):
         admin = get_user_model().objects.create_superuser("admin", password="password")
         admin.save()
 
+        # Seed random generator to make this script deterministic
+        random.seed(options["seed"])
+
         # Create sports
         sports_names = [
             "After Work Fitness",
-- 
GitLab