aboutsummaryrefslogtreecommitdiffstats
path: root/rrs/migrations/0015_rmh_layerbranch_populate.py
blob: 5c3d5e3335031919b534fc7e2290a71e15d961b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import settings


def populate_rmh_layerbranch(apps, schema_editor):
    RecipeMaintainerHistory = apps.get_model('rrs', 'RecipeMaintainerHistory')
    LayerBranch = apps.get_model('layerindex', 'LayerBranch')
    if not settings.CORE_LAYER_NAME:
        raise Exception('Please set CORE_LAYER_NAME in settings.py')
    core_layerbranch = LayerBranch.objects.filter(layer__name=settings.CORE_LAYER_NAME).first()
    if not core_layerbranch:
        raise Exception('Unable to find core layer "%s" specified in CORE_LAYER_NAME in settings.py - please set up the layerindex application first' % settings.CORE_LAYER_NAME)
    for row in RecipeMaintainerHistory.objects.all():
        row.layerbranch = core_layerbranch
        row.save()


class Migration(migrations.Migration):

    dependencies = [
        ('rrs', '0014_reup_layerbranch_nonnull'),
    ]

    operations = [
        migrations.RunPython(populate_rmh_layerbranch, reverse_code=migrations.RunPython.noop),
    ]