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/README.md b/README.md
index e43b2b3..992978c 100644
--- a/README.md
+++ b/README.md
@@ -63,6 +63,20 @@
 See the end of the next section to add a super-user.
 
 
+### Configuring the Google OAuth authentication
+
+For accessing the statistics pages, Hiccup uses Django allauth for single
+sign on authentication with Google. Everybody with an email address issued
+by Fairphone is allowed to create an account.
+
+This step has to be run before running the migrations.
+
+In you local `local_settings.py`, overwrite the values for
+`SOCIALACCOUNT_GOOGLE_CLIENT_ID` and `SOCIALACCOUNT_GOOGLE_SECRET` with the
+correct values. These credentials can be created at the
+[Google developers console](https://console.developers.google.com/apis/credentials).
+
+
 ### Run Hiccup server
 
 The first time you run the server, the database will be empty and the model migrations have yet to
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)]
diff --git a/hiccup/settings.py b/hiccup/settings.py
index f5703c7..8a06b15 100644
--- a/hiccup/settings.py
+++ b/hiccup/settings.py
@@ -178,6 +178,11 @@
     }
 }
 
+# To use the Google OAuth feature, overwrite these values with the correct
+# client id and secret in your local settings before running the migrations.
+SOCIALACCOUNT_GOOGLE_CLIENT_ID = ""
+SOCIALACCOUNT_GOOGLE_SECRET = ""
+
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/1.9/howto/static-files/