mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 03:17:07 +00:00
61
people/migrations/0021_refactor_person_disciplines.py
Normal file
61
people/migrations/0021_refactor_person_disciplines.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# Generated by Django 2.2.10 on 2020-06-24 14:19
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def migrate_forward(apps, schema_editor):
|
||||
Person = apps.get_model('people', 'Person')
|
||||
|
||||
for person in Person.objects.all():
|
||||
try:
|
||||
person.disciplines = person.discipline.name
|
||||
person.save()
|
||||
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
def migrate_backward(apps, schema_editor):
|
||||
Person = apps.get_model('people', 'Person')
|
||||
Discipline = apps.get_model('people', 'Discipline')
|
||||
|
||||
for person in Person.objects.all():
|
||||
try:
|
||||
discipline_str = person.disciplines.split(',')[0]
|
||||
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
else:
|
||||
# Returns None if not found - doesn't raise exception
|
||||
discipline = Discipline.objects.filter(name=discipline_str).first()
|
||||
|
||||
if not discipline:
|
||||
discipline = Discipline.objects.create(
|
||||
name=discipline_str,
|
||||
code=discipline_str
|
||||
if len(discipline_str) < 15 else discipline_str[:15])
|
||||
|
||||
person.discipline = discipline
|
||||
person.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('people', '0020_person_organisation_started_date'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='person',
|
||||
name='disciplines',
|
||||
field=models.CharField(blank=True, max_length=255, null=True),
|
||||
),
|
||||
migrations.RunPython(migrate_forward, migrate_backward),
|
||||
migrations.RemoveField(
|
||||
model_name='person',
|
||||
name='discipline',
|
||||
),
|
||||
migrations.DeleteModel(name='Discipline', ),
|
||||
]
|
||||
Reference in New Issue
Block a user