Explorar o código

fix generate api key for handlers

ShariX Developer hai 1 mes
pai
achega
9df5a85d0c
Modificáronse 1 ficheiros con 13 adicións e 7 borrados
  1. 13 7
      lib/seed.py

+ 13 - 7
lib/seed.py

@@ -8,16 +8,22 @@ def create_api_key_for_handlers():
     api_key_list = []
 
     for botname in BOTLIST:
-        if not APIKey.objects.filter(name=botname).exists():
-            api_key, key = APIKey.objects.create_key(name=botname)
-            api_key_list.append([botname, key])
-            print(f"Initial API Key for {botname} handler created.")
-        else:
-            print(f"Initial API Key for {botname} handler already exists.")
+        existing_keys = APIKey.objects.filter(name=botname, revoked=False)
+        if existing_keys.exists():
+            # Делаем все старые ключи для этого бота неактивными (revoked)
+            for old_key in existing_keys:
+                old_key.revoked = True
+                old_key.save()
+            print(f"Старые ключи для {botname} устарели (revoked).")
 
+        # Создаем новый ключ
+        api_key, key = APIKey.objects.create_key(name=botname)
+        api_key_list.append([botname, key])
+        print(f"Новый API Key для {botname} создан.")
+
+    # Формируем строки вида botname_key=ключ
     api_key_list = list(map(lambda x: x[0] + '_key=' + x[1], api_key_list))
     txt = '\n'.join(api_key_list)
 
     with open('platform_running/handlers/.env', 'w') as f:
         f.write(txt)
-