blob: b7a06aca92096f6e98647ceb22325c06201f8b6c [file] [log] [blame]
Mitja Nikolaus8b801092018-12-10 10:36:44 +01001# -*- coding: utf-8 -*-
2"""Migrations to add the Google social account for the allauth plugin."""
3# pylint: disable=invalid-name
4import logging
5
6from allauth.socialaccount.models import SocialApp
7
8from django.conf import settings
9from django.contrib.sites.models import Site
10from django.db import migrations
11
12LOGGER = logging.getLogger(__name__)
13
14SOCIALACCOUNT_GOOGLE_NAME = "Google"
15SOCIALACCOUNT_GOOGLE_PROVIDER = "google"
16
17
18def create_google_socialaccount(apps, schema_editor):
19 """Create the Google social account.
20
21 The account is only created if it does not exist yet and the
22 SOCIALACCOUNT_GOOGLE_CLIENT_ID and the SOCIALACCOUNT_GOOGLE_SECRET
23 settings are set.
24 """
25 # pylint: disable=unused-argument
26
27 if not SocialApp.objects.filter(name=SOCIALACCOUNT_GOOGLE_NAME).exists():
28 if (
29 settings.SOCIALACCOUNT_GOOGLE_CLIENT_ID
30 and settings.SOCIALACCOUNT_GOOGLE_SECRET
31 ):
32 google_socialapp = SocialApp.objects.create(
33 name=SOCIALACCOUNT_GOOGLE_NAME,
34 provider=SOCIALACCOUNT_GOOGLE_PROVIDER,
35 client_id=settings.SOCIALACCOUNT_GOOGLE_CLIENT_ID,
36 secret=settings.SOCIALACCOUNT_GOOGLE_SECRET,
37 )
38 google_socialapp.sites.add(Site.objects.get(id=settings.SITE_ID))
39 google_socialapp.save()
40 else:
41 LOGGER.info(
42 "The Google socialaccount configuration was not created. Set "
43 "the SOCIALACCOUNT_GOOGLE_CLIENT_ID and "
44 "SOCIALACCOUNT_GOOGLE_SECRET settings and re-run the "
45 "migration to create it."
46 )
47
48
49class Migration(migrations.Migration):
50 """Run the migration script."""
51
52 dependencies = [
53 ("socialaccount", "0003_extra_data_default_dict"),
54 ("crashreport_stats", "0006_add_default_site"),
55 ]
56
57 operations = [migrations.RunPython(create_google_socialaccount)]