From 8aaf6a2449ee1b7812714ecffac2f16fa0102199 Mon Sep 17 00:00:00 2001 From: Tanner Prestegard <tanner.prestegard@ligo.org> Date: Thu, 7 Jun 2018 15:17:47 -0500 Subject: [PATCH] Adding Django sites app Activate Django sites app and create a migration which adds the LIGO.ORG domain name of the GraceDB instance as a Site. This will be used to build absolute URIs without needed a request object. --- config/settings/base.py | 9 +++- gracedb/migrations/sites/0001_initial.py | 34 +++++++++++++ gracedb/migrations/sites/0002_update_site.py | 52 ++++++++++++++++++++ gracedb/migrations/sites/__init__.py | 0 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 gracedb/migrations/sites/0001_initial.py create mode 100644 gracedb/migrations/sites/0002_update_site.py create mode 100644 gracedb/migrations/sites/__init__.py diff --git a/config/settings/base.py b/config/settings/base.py index 730550ea1..43e30170f 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -279,13 +279,16 @@ MIDDLEWARE = [ # Path to root URLconf ROOT_URLCONF = '{module}.urls'.format(module=os.path.basename(CONFIG_ROOT)) +# Database ID of the current site (for sites framework) +SITE_ID=1 + # List of string designating all applications which are enabled. INSTALLED_APPS = [ 'django.contrib.auth', 'django.contrib.admin', 'django.contrib.contenttypes', 'django.contrib.sessions', -# 'django.contrib.sites', + 'django.contrib.sites', 'django.contrib.staticfiles', 'django.contrib.messages', 'maintenance_mode', @@ -348,10 +351,12 @@ STATICFILES_DIRS = [ BOWER_DIR, ] -# Added in order to perform data migrations on the auth and guardian apps +# Added in order to perform data migrations on Django apps +# and other third-party apps MIGRATION_MODULES = { 'auth': 'migrations.auth', 'guardian': 'migrations.guardian', + 'sites': 'migrations.sites', } # Forces test database to be created with syncdb rather than via diff --git a/gracedb/migrations/sites/0001_initial.py b/gracedb/migrations/sites/0001_initial.py new file mode 100644 index 000000000..43afc44a2 --- /dev/null +++ b/gracedb/migrations/sites/0001_initial.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.5 on 2018-06-04 16:04 +from __future__ import unicode_literals + +import django.contrib.sites.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Site', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('domain', models.CharField(max_length=100, unique=True, validators=[django.contrib.sites.models._simple_domain_name_validator], verbose_name='domain name')), + ('name', models.CharField(max_length=50, verbose_name='display name')), + ], + options={ + 'ordering': ('domain',), + 'db_table': 'django_site', + 'verbose_name': 'site', + 'verbose_name_plural': 'sites', + }, + managers=[ + ('objects', django.contrib.sites.models.SiteManager()), + ], + ), + ] diff --git a/gracedb/migrations/sites/0002_update_site.py b/gracedb/migrations/sites/0002_update_site.py new file mode 100644 index 000000000..b31ca6d50 --- /dev/null +++ b/gracedb/migrations/sites/0002_update_site.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.5 on 2018-06-04 16:05 +from __future__ import unicode_literals + +from django.db import migrations +from django.conf import settings +import socket + +SITES = { + 'old': { + 'domain': 'example.com', + 'name': 'example.com', + }, + 'new': { + 'domain': socket.gethostname() + '.ligo.org', + 'name': 'ligo.org', + }, +} + +def update_site(apps, schema_editor): + Site = apps.get_model('sites', 'Site') + + # Get current site matching SITE_ID, should be example.com at this point + site1 = Site.objects.get(id=settings.SITE_ID) + + # Update with new site name and domain + site1.name = SITES['new']['name'] + site1.domain = SITES['new']['domain'] + site1.save() + + +def revert_site(apps, schema_editor): + Site = apps.get_model('sites', 'Site') + + # Get current site matching SITE_ID should be ligo.org + site1 = Site.objects.get(id=settings.SITE_ID) + + # Revert to original site + site1.name = SITES['old']['name'] + site1.domain = SITES['old']['domain'] + site1.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('sites', '0001_initial'), + ] + + operations = [ + migrations.RunPython(update_site, revert_site), + ] diff --git a/gracedb/migrations/sites/__init__.py b/gracedb/migrations/sites/__init__.py new file mode 100644 index 000000000..e69de29bb -- GitLab