refactor code

This commit is contained in:
adamscmRH 2018-07-05 10:54:54 -04:00
parent 3e1aaec9fe
commit 9ac92c0ee0
2 changed files with 13 additions and 17 deletions

View File

@ -9,6 +9,11 @@ def copy_session_settings(apps, schema_editor):
_rename_setting.rename_setting(apps, schema_editor, old_key='AUTH_TOKEN_EXPIRATION', new_key='SESSIONS_PER_USER')
def reverse_copy_session_settings(apps, schema_editor):
_rename_setting.rename_setting(apps, schema_editor, old_key='SESSION_COOKIE_AGE', new_key='AUTH_TOKEN_PER_USER')
_rename_setting.rename_setting(apps, schema_editor, old_key='SESSIONS_PER_USER', new_key='SESSIONS_PER_USER''AUTH_TOKEN_EXPIRATION')
class Migration(migrations.Migration):
dependencies = [
@ -16,6 +21,6 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RunPython(copy_session_settings),
migrations.RunPython(copy_session_settings, reverse_copy_session_settings),
]

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
from django.utils.timezone import now
logger = logging.getLogger('awx.conf.settings')
@ -12,26 +13,16 @@ def rename_setting(apps, schema_editor, old_key, new_key):
Setting = apps.get_model('conf', 'Setting')
if Setting.objects.filter(key=new_key).exists():
logger.error('Setting', new_key, 'unexpectedly exists before this migration, \
it will be replaced by the value of the', old_key, 'setting.')
logger.info('Setting ' + new_key + ' unexpectedly exists before this migration, it will be replaced by the value of the ' + old_key + ' setting.')
Setting.objects.filter(key=new_key).delete()
old_setting = Setting.objects.filter(key=old_key).first()
if old_setting is not None:
Setting.objects.create(key=new_key, value=old_setting.value)
Setting.objects.create(key=new_key,
value=old_setting.value,
created=now(),
modified=now()
)
Setting.objects.filter(key=old_key).delete()
def reverse_rename_setting(apps, schema_editor, old_key, new_key):
Setting = apps.get_model('conf', 'Setting')
if Setting.objects.filter(key=old_key).exists():
logger.error('Setting', old_key, 'unexpectedly exists before this migration, \
it will be replaced by the value of the', new_key, 'setting.')
Setting.objects.filter(key=old_key).delete()
new_setting = Setting.objects.filter(key=new_key).first()
if new_setting is not None:
Setting.objects.create(key=old_key, value=new_setting.value)
Setting.objects.filter(key=new_key).delete()