utils.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. from apps.config.models import Config, Service
  5. from apps.app.models import App
  6. import json
  7. def compose_configs(app, env_id, no_prefix=False):
  8. configs = dict()
  9. # app own configs
  10. for item in Config.objects.filter(type='app', o_id=app.id, env_id=env_id).only('key', 'value'):
  11. key = item.key if no_prefix else f'{app.key}_{item.key}'
  12. configs[key] = item.value
  13. # relation app public configs
  14. if app.rel_apps:
  15. app_ids = json.loads(app.rel_apps)
  16. if app_ids:
  17. id_key_map = {x.id: x.key for x in App.objects.filter(id__in=app_ids)}
  18. for item in Config.objects.filter(type='app', o_id__in=app_ids, env_id=env_id, is_public=True) \
  19. .only('key', 'value'):
  20. key = item.key if no_prefix else f'{id_key_map[item.o_id]}_{item.key}'
  21. configs[key] = item.value
  22. # relation service configs
  23. if app.rel_services:
  24. src_ids = json.loads(app.rel_services)
  25. if src_ids:
  26. id_key_map = {x.id: x.key for x in Service.objects.filter(id__in=src_ids)}
  27. for item in Config.objects.filter(type='src', o_id__in=src_ids, env_id=env_id).only('key', 'value'):
  28. key = item.key if no_prefix else f'{id_key_map[item.o_id]}_{item.key}'
  29. configs[key] = item.value
  30. return configs