"""
Setup DB with example data for tests
"""

from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User, Group
from django.core.management.base import BaseCommand

from api import models


class Command(BaseCommand):
    help = 'Setup DB with example data for tests'

    def handle(self, *args, **options):
        print('----    Creating Users    ----')
        User.objects.get_or_create(username='thomastest', password=make_password('imatestin'))
        thomas = User.objects.get(username='thomastest')

        User.objects.get_or_create(username='norbert', password=make_password('netzwerk'))
        norbert = User.objects.get(username='norbert')

        User.objects.get_or_create(username='stefan', password=make_password('helldesk'))
        stefan = User.objects.get(username='stefan')

        superuser = Group.objects.get(name='superuser')
        superuser.user_set.add(thomas)

        netadmin = Group.objects.get(name='netadmin')
        netadmin.user_set.add(norbert)

        support = Group.objects.get(name='support')
        support.user_set.add(stefan)

        print('----    Creating Inventory    ----')
        models.Inventory.objects.create(name='Example', hosts_file='web_nornir/nornir_config/example_config/hosts.yaml',
                                        groups_file='web_nornir/nornir_config/example_config/groups.yaml', type=1)
        models.Inventory.objects.create(name='INS Lab', hosts_file='web_nornir/nornir_config/inslab_config/hosts.yaml',
                                        groups_file='web_nornir/nornir_config/inslab_config/groups.yaml', type=1)

        print('----    Creating Job Templates    ----')
        models.JobTemplate.objects.create(name='hello_world', description='This prints a hello world',
                                          file_name='hello_world.py', created_by_id=1)
        models.JobTemplate.objects.create(name='Get CDP Neighbors', description='Lists all CDP neighbors',
                                          file_name='get_cdp_neighbors.py', created_by_id=1)
        models.JobTemplate.objects.create(name='Get Interfaces',
                                          description='Gets brief information about all interfaces, sh ip int br',
                                          file_name='get_interfaces.py', created_by_id=1)
        models.JobTemplate.objects.create(name='Ping Device',
                                          description='Pings a chosen network device and reports if reachable',
                                          file_name='ping.py', variables=['target'], created_by_id=1)
        models.JobTemplate.objects.create(name='Get Configuration', description='Gets all configuration from device',
                                          file_name='get_configuration.py', created_by_id=1)

        print('----    Creating Tasks    ----')
        models.Task.objects.create(name='Get Hello World', created_by_id=1, template_id=1, inventory_id=1)
        models.Task.objects.create(name='Get CDP neighbors of INS lab', created_by_id=2, template_id=2, inventory_id=2)
        models.Task.objects.create(name='Get interfaces of INS lab', created_by_id=2, template_id=3, inventory_id=2)

        print('----    ALL DONE!!    ----')

[((18, 17, 18, 56), 'django.contrib.auth.models.User.objects.get', 'User.objects.get', (), '', False, 'from django.contrib.auth.models import User, Group\n'), ((21, 18, 21, 54), 'django.contrib.auth.models.User.objects.get', 'User.objects.get', (), '', False, 'from django.contrib.auth.models import User, Group\n'), ((24, 17, 24, 52), 'django.contrib.auth.models.User.objects.get', 'User.objects.get', (), '', False, 'from django.contrib.auth.models import User, Group\n'), ((26, 20, 26, 55), 'django.contrib.auth.models.Group.objects.get', 'Group.objects.get', (), '', False, 'from django.contrib.auth.models import User, Group\n'), ((29, 19, 29, 53), 'django.contrib.auth.models.Group.objects.get', 'Group.objects.get', (), '', False, 'from django.contrib.auth.models import User, Group\n'), ((32, 18, 32, 51), 'django.contrib.auth.models.Group.objects.get', 'Group.objects.get', (), '', False, 'from django.contrib.auth.models import User, Group\n'), ((36, 8, 37, 114), 'api.models.Inventory.objects.create', 'models.Inventory.objects.create', (), '', False, 'from api import models\n'), ((38, 8, 39, 113), 'api.models.Inventory.objects.create', 'models.Inventory.objects.create', (), '', False, 'from api import models\n'), ((42, 8, 43, 86), 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', (), '', False, 'from api import models\n'), ((44, 8, 45, 92), 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', (), '', False, 'from api import models\n'), ((46, 8, 48, 89), 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', (), '', False, 'from api import models\n'), ((49, 8, 51, 101), 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', (), '', False, 'from api import models\n'), ((52, 8, 53, 92), 'api.models.JobTemplate.objects.create', 'models.JobTemplate.objects.create', (), '', False, 'from api import models\n'), ((56, 8, 56, 106), 'api.models.Task.objects.create', 'models.Task.objects.create', (), '', False, 'from api import models\n'), ((57, 8, 57, 119), 'api.models.Task.objects.create', 'models.Task.objects.create', (), '', False, 'from api import models\n'), ((58, 8, 58, 116), 'api.models.Task.objects.create', 'models.Task.objects.create', (), '', False, 'from api import models\n'), ((17, 67, 17, 93), 'django.contrib.auth.hashers.make_password', 'make_password', ({(17, 81, 17, 92): '"""imatestin"""'}, {}), "('imatestin')", False, 'from django.contrib.auth.hashers import make_password\n'), ((20, 64, 20, 89), 'django.contrib.auth.hashers.make_password', 'make_password', ({(20, 78, 20, 88): '"""netzwerk"""'}, {}), "('netzwerk')", False, 'from django.contrib.auth.hashers import make_password\n'), ((23, 63, 23, 88), 'django.contrib.auth.hashers.make_password', 'make_password', ({(23, 77, 23, 87): '"""helldesk"""'}, {}), "('helldesk')", False, 'from django.contrib.auth.hashers import make_password\n')]