# Generated by Django 4.2.5 on 2023-10-02 14:31 import os import re from django.db import migrations def add_OIDC_settings_from_env(apps, schema_editor): SocialApp = apps.get_model("socialaccount", "SocialApp") oidc_prefix = "SOCIALACCOUNT_PROVIDERS_openid_connect_SERVERS_" oidc_pattern = re.compile(r"{prefix}\w+".format(prefix=oidc_prefix)) oidc_apps = {} for key, value in { key.replace(oidc_prefix, ""): val for key, val in os.environ.items() if oidc_pattern.match(key) }.items(): number, setting = key.split("_", 1) if number in oidc_apps: oidc_apps[number][setting] = value else: oidc_apps[number] = {setting: value} oidc_apps = [x for x in oidc_apps.values()] for app in oidc_apps: app_id = app.get("id") app_server_url = app.get("server_url") if app_id and app_server_url: db_social_app = SocialApp.objects.filter(provider=app_id).first() if db_social_app and not db_social_app.settings: db_social_app.provider = "openid_connect" db_social_app.provider_id = app_id db_social_app.settings = {"server_url": app_server_url} db_social_app.save() class Migration(migrations.Migration): dependencies = [ ("users", "0009_alter_user_created"), ] operations = [ migrations.RunPython( code=add_OIDC_settings_from_env, reverse_code=migrations.RunPython.noop ) ]