From 5a5667f92ee1d571ef88a641f9e34dd75f4dbccd Mon Sep 17 00:00:00 2001
From: Benjamin Ledel <benjamin@schule-plus.com>
Date: Mon, 17 Mar 2025 16:54:28 +0100
Subject: [PATCH] * avoid debug logs

---
 .../commands/check_and_apply_migrations.py    | 42 +++++++++++--------
 src/backend/settings.py                       |  4 +-
 2 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/src/backend/management/commands/check_and_apply_migrations.py b/src/backend/management/commands/check_and_apply_migrations.py
index 92e4c75..cc8d99d 100644
--- a/src/backend/management/commands/check_and_apply_migrations.py
+++ b/src/backend/management/commands/check_and_apply_migrations.py
@@ -3,26 +3,31 @@ from django.core.management import call_command
 from django.db import connections
 from django.db.migrations.executor import MigrationExecutor
 from django.contrib.auth.models import Permission, ContentType, Group
-from users.models import CustomUser  # Adjust this import based on your actual user model
+from django.conf import settings  # Import settings
 
 class Command(BaseCommand):
     help = 'Check and apply necessary migrations and ensure roles, permissions, and groups are set up correctly'
 
     def handle(self, *args, **kwargs):
-        self.stdout.write(self.style.NOTICE('Checking for pending migrations...'))
+        if settings.DEBUG:
+            self.stdout.write(self.style.NOTICE('Checking for pending migrations...'))
 
         connection = connections['default']
         executor = MigrationExecutor(connection)
         targets = executor.loader.graph.leaf_nodes()
         
         if executor.migration_plan(targets):
-            self.stdout.write(self.style.WARNING('Pending migrations found. Applying migrations...'))
+            if settings.DEBUG:
+                self.stdout.write(self.style.WARNING('Pending migrations found. Applying migrations...'))
             call_command('migrate')
-            self.stdout.write(self.style.SUCCESS('All migrations applied successfully.'))
+            if settings.DEBUG:
+                self.stdout.write(self.style.SUCCESS('All migrations applied successfully.'))
         else:
-            self.stdout.write(self.style.SUCCESS('No pending migrations found.'))
+            if settings.DEBUG:
+                self.stdout.write(self.style.SUCCESS('No pending migrations found.'))
 
-        self.stdout.write(self.style.NOTICE('Checking roles and permissions...'))
+        if settings.DEBUG:
+            self.stdout.write(self.style.NOTICE('Checking roles and permissions...'))
 
         # Ensure necessary permissions exist
         permissions = {
@@ -45,12 +50,12 @@ class Command(BaseCommand):
                 defaults={'name': name}
             )
             permission_objects[codename] = permission
-            if created:
-                self.stdout.write(self.style.SUCCESS(f'Permission created: {name}'))
-            else:
-                self.stdout.write(self.style.NOTICE(f'Permission already exists: {name}'))
+            if settings.DEBUG:
+                if created:
+                    self.stdout.write(self.style.SUCCESS(f'Permission created: {name}'))
+                else:
+                    self.stdout.write(self.style.NOTICE(f'Permission already exists: {name}'))
     
-        
         # Ensure auth groups exist and assign permissions
         groups_permissions = {
             "polaris_administrator": ["create_user","edit_user","create_provider","change_provider","manage_provider_keys","manage_analytics_tokens","create_user_consent","request_user_data"],
@@ -61,15 +66,18 @@ class Command(BaseCommand):
         
         for group_name, perms in groups_permissions.items():
             group, created = Group.objects.get_or_create(name=group_name)
-            if created:
-                self.stdout.write(self.style.SUCCESS(f'Group created: {group_name}'))
-            else:
-                self.stdout.write(self.style.NOTICE(f'Group already exists: {group_name}'))
+            if settings.DEBUG:
+                if created:
+                    self.stdout.write(self.style.SUCCESS(f'Group created: {group_name}'))
+                else:
+                    self.stdout.write(self.style.NOTICE(f'Group already exists: {group_name}'))
             
             # Assign permissions to group
             for perm in perms:
                 if perm in permission_objects:
                     group.permissions.add(permission_objects[perm])
-                    self.stdout.write(self.style.SUCCESS(f'Assigned permission {perm} to group {group_name}'))
+                    if settings.DEBUG:
+                        self.stdout.write(self.style.SUCCESS(f'Assigned permission {perm} to group {group_name}'))
         
-        self.stdout.write(self.style.SUCCESS('Roles, permissions, and groups check completed.'))
+        if settings.DEBUG:
+            self.stdout.write(self.style.SUCCESS('Roles, permissions, and groups check completed.'))
diff --git a/src/backend/settings.py b/src/backend/settings.py
index ed1484a..9dcb9c9 100644
--- a/src/backend/settings.py
+++ b/src/backend/settings.py
@@ -34,8 +34,8 @@ mimetypes.add_type("text/javascript", ".js", True)
 SECRET_KEY = "django-insecure-4%wn*pt3vpdgk#==(qbtocdh#-2wr58py(ri=(0a^15&&n*gl9"
 
 # SECURITY WARNING: don't run with debug turned on in production!
-DEBUG = env("DEBUG", default=False) == True
-DEBUG_PROPAGATE_EXCEPTIONS = env("DEBUG_PROPAGATE_EXCEPTIONS", default=False) == True
+DEBUG = (env("DEBUG", default=False) == True)
+DEBUG_PROPAGATE_EXCEPTIONS = (env("DEBUG_PROPAGATE_EXCEPTIONS", default=False) == True)
 
 TESTING = env("TESTING", default="False")
 
-- 
GitLab