[Patchew-devel] [PATCH 4/6] Make default group creation a migration

Fam Zheng famz at redhat.com
Wed Oct 31 01:28:54 UTC 2018


The ready hook isn't the right point to use DB.

Signed-off-by: Fam Zheng <famz at redhat.com>
---
 api/apps.py                                   | 14 ++----------
 .../0037_populate_default_groups.py           | 22 +++++++++++++++++++
 2 files changed, 24 insertions(+), 12 deletions(-)
 create mode 100644 api/migrations/0037_populate_default_groups.py

diff --git a/api/apps.py b/api/apps.py
index c7fc162..1210091 100644
--- a/api/apps.py
+++ b/api/apps.py
@@ -13,19 +13,9 @@
 from django.apps import AppConfig
 import mod
 
-_default_groups = ['maintainers', 'testers', 'importers']
-
-def create_default_groups():
-    from django.contrib.auth.models import Group
-    for grp in _default_groups:
-        Group.objects.get_or_create(name=grp)
-
 class ApiConfig(AppConfig):
     name = 'api'
     verbose_name = "Patchew Core"
     def ready(self):
-        try:
-            mod.load_modules()
-            create_default_groups()
-        except Exception as e:
-            print("Error while loading modules:", e)
+        from mod import load_modules
+        load_modules()
diff --git a/api/migrations/0037_populate_default_groups.py b/api/migrations/0037_populate_default_groups.py
new file mode 100644
index 0000000..c0befa6
--- /dev/null
+++ b/api/migrations/0037_populate_default_groups.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations
+from django.db.models import Count
+import json
+
+
+def populate_default_groups(apps, schema_editor):
+    from django.contrib.auth.models import Group
+    for grp in ['maintainers', 'testers', 'importers']:
+        Group.objects.get_or_create(name=grp)
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('api', '0036_populate_message_tags'),
+    ]
+
+    operations = [
+        migrations.RunPython(populate_default_groups),
+    ]
-- 
2.17.2




More information about the Patchew-devel mailing list