seed.py 821 B

123456789101112131415161718192021222324
  1. from rest_framework_api_key.models import APIKey
  2. from webservice_running.handlers.config import BOTLIST
  3. def create_webservice_seed():
  4. create_api_key_for_handlers()
  5. def create_api_key_for_handlers():
  6. api_key_list = []
  7. for botname in BOTLIST:
  8. if not APIKey.objects.filter(name=f"api-key-for-{botname}").exists():
  9. api_key, key = APIKey.objects.create_key(name=f"api-key-for-{botname}")
  10. api_key_list.append([botname, key])
  11. print(f"Initial API Key for {botname} handler created.")
  12. else:
  13. print(f"Initial API Key for {botname} handler already exists.")
  14. api_key_list = list(map(lambda x: x[0] + '_key=' + x[1], api_key_list))
  15. txt = '\n'.join(api_key_list)
  16. with open('webservice_running/handlers/.env', 'w') as f:
  17. f.write(txt)