# Generated by Django 3.1.2 on 2020-10-21 19:50

import json
import os.path
from django.db import migrations

from ..models import Machine, Owner

def initialiseMachines(apps, _schemaEditor):
    with open(os.path.dirname(__file__) + '/../../../config.json') as f:
        config = json.loads(f.read())
    machines = config['machines']
    for machine in machines:
        newMachine = Machine(name=machine['fullName'])
        newMachine.save()

def initialiseOwners(apps, _schemaEditor):
    with open(os.path.dirname(__file__) + '/../../../config.json') as f:
        config = json.loads(f.read())
    for stair, apts in config['apartments'].items():
        for apt in apts:
            ownerStr = f"{stair}{config['ownerDelimiter']}{apt}"
            newOwner = Owner(name=ownerStr)
            newOwner.save()


class Migration(migrations.Migration):

    dependencies = [
        ('laundry', '0001_declare'),
    ]

    operations = [
        migrations.RunPython(initialiseMachines),
        migrations.RunPython(initialiseOwners)
    ]
