Mitja Nikolaus | e7af956 | 2018-12-10 16:31:46 +0100 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | """Migrations to add a default site which is required for the allauth plugin.""" |
| 3 | # pylint: disable=invalid-name |
| 4 | import logging |
| 5 | |
| 6 | from django.conf import settings |
| 7 | from django.contrib.sites.models import Site |
| 8 | from django.db import migrations, IntegrityError |
| 9 | |
| 10 | LOGGER = logging.getLogger(__name__) |
| 11 | |
| 12 | |
| 13 | def create_default_site(apps, schema_editor): |
| 14 | """Create a default site instance if it does not exist.""" |
| 15 | # pylint: disable=unused-argument |
| 16 | |
| 17 | if not Site.objects.filter(id=settings.SITE_ID).exists(): |
| 18 | try: |
| 19 | Site.objects.create(id=settings.SITE_ID) |
| 20 | except IntegrityError as e: |
| 21 | LOGGER.error( |
| 22 | "Failed to create a site with id %d. Either adapt the SITE_ID " |
| 23 | "setting or remove the currently existing site.", |
| 24 | settings.SITE_ID, |
| 25 | ) |
| 26 | raise e |
| 27 | |
| 28 | |
| 29 | class Migration(migrations.Migration): |
| 30 | """Run the migration script.""" |
| 31 | |
| 32 | dependencies = [ |
| 33 | ("sites", "0002_alter_domain_unique"), |
| 34 | ("crashreport_stats", "0005_remove_manual_default_value"), |
| 35 | ] |
| 36 | |
| 37 | operations = [migrations.RunPython(create_default_site)] |