8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 11:23:10 +00:00
re2o/machines/migrations/0107_fix_lowercase_domain.py

37 lines
1.2 KiB
Python
Raw Normal View History

2020-04-18 17:57:14 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
from django.core.exceptions import ValidationError
2020-04-18 18:21:43 +00:00
import logging
2020-04-18 17:57:14 +00:00
def fix_duplicate(apps, schema_editor):
2020-04-18 18:21:43 +00:00
logger = logging.getLogger(__name__)
2020-04-18 17:57:14 +00:00
db_alias = schema_editor.connection.alias
2020-04-18 18:21:43 +00:00
Domain = apps.get_model("machines", "Domain")
domains_to_fix = filter(lambda m : not m.name.islower(), Domain.objects.using(db_alias).all())
for domain in domains_to_fix:
2020-04-18 17:57:14 +00:00
try:
2020-04-18 18:21:43 +00:00
domain.name = domain.name.lower()
domain.validate_unique()
domain.clean()
2020-04-18 17:57:14 +00:00
except ValidationError:
2020-04-18 18:21:43 +00:00
old_name = domain.name
domain.name = domain.name.lower() + str(domain.interface_parent.id)
domain.clean()
warning_message = "Warning : Domain %s has been renamed %s due to dns uniqueness" % (old_name, domain.name)
logger.warning(warning_message)
domain.save()
2020-04-18 17:57:14 +00:00
def unfix_duplicate(apps, schema_editor):
return
class Migration(migrations.Migration):
dependencies = [("machines", "0106_auto_20191120_0159")]
operations = [
migrations.RunPython(fix_duplicate, unfix_duplicate),
]