Mitja Nikolaus | 8b80109 | 2018-12-10 10:36:44 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """Migrations to add the Google social account for the allauth plugin.""" |
| 3 | # pylint: disable=invalid-name |
| 4 | import logging |
| 5 | |
| 6 | from allauth.socialaccount.models import SocialApp |
| 7 | |
| 8 | from django.conf import settings |
| 9 | from django.contrib.sites.models import Site |
| 10 | from django.db import migrations |
| 11 | |
| 12 | LOGGER = logging.getLogger(__name__) |
| 13 | |
| 14 | SOCIALACCOUNT_GOOGLE_NAME = "Google" |
| 15 | SOCIALACCOUNT_GOOGLE_PROVIDER = "google" |
| 16 | |
| 17 | |
| 18 | def 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 | |
| 49 | class 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)] |