Add data migration for adding the google socialaccount
The data migration adds the configuration for the Google
socialaccount to the database if the required settings are set.
Issue: HIC-289
Change-Id: I9e849f1c73a26ca4f5475844c3eeddd2196f0d45
diff --git a/crashreport_stats/migrations/0007_add_google_socialaccount.py b/crashreport_stats/migrations/0007_add_google_socialaccount.py
new file mode 100644
index 0000000..b7a06ac
--- /dev/null
+++ b/crashreport_stats/migrations/0007_add_google_socialaccount.py
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+"""Migrations to add the Google social account for the allauth plugin."""
+# pylint: disable=invalid-name
+import logging
+
+from allauth.socialaccount.models import SocialApp
+
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.db import migrations
+
+LOGGER = logging.getLogger(__name__)
+
+SOCIALACCOUNT_GOOGLE_NAME = "Google"
+SOCIALACCOUNT_GOOGLE_PROVIDER = "google"
+
+
+def create_google_socialaccount(apps, schema_editor):
+ """Create the Google social account.
+
+ The account is only created if it does not exist yet and the
+ SOCIALACCOUNT_GOOGLE_CLIENT_ID and the SOCIALACCOUNT_GOOGLE_SECRET
+ settings are set.
+ """
+ # pylint: disable=unused-argument
+
+ if not SocialApp.objects.filter(name=SOCIALACCOUNT_GOOGLE_NAME).exists():
+ if (
+ settings.SOCIALACCOUNT_GOOGLE_CLIENT_ID
+ and settings.SOCIALACCOUNT_GOOGLE_SECRET
+ ):
+ google_socialapp = SocialApp.objects.create(
+ name=SOCIALACCOUNT_GOOGLE_NAME,
+ provider=SOCIALACCOUNT_GOOGLE_PROVIDER,
+ client_id=settings.SOCIALACCOUNT_GOOGLE_CLIENT_ID,
+ secret=settings.SOCIALACCOUNT_GOOGLE_SECRET,
+ )
+ google_socialapp.sites.add(Site.objects.get(id=settings.SITE_ID))
+ google_socialapp.save()
+ else:
+ LOGGER.info(
+ "The Google socialaccount configuration was not created. Set "
+ "the SOCIALACCOUNT_GOOGLE_CLIENT_ID and "
+ "SOCIALACCOUNT_GOOGLE_SECRET settings and re-run the "
+ "migration to create it."
+ )
+
+
+class Migration(migrations.Migration):
+ """Run the migration script."""
+
+ dependencies = [
+ ("socialaccount", "0003_extra_data_default_dict"),
+ ("crashreport_stats", "0006_add_default_site"),
+ ]
+
+ operations = [migrations.RunPython(create_google_socialaccount)]