# Generated by Django 2.2.15 on 2020-09-05 10:10
from django.db import migrations


def add_perms(apps, schema_editor):
    ContentType = apps.get_model('contenttypes', 'ContentType')
    User = apps.get_model('auth', 'User')
    Permission = apps.get_model('auth', 'Permission')
    Resitel = apps.get_model('seminar', 'Resitel')

    c = ContentType.objects.get_for_model(User)
    org_perm = Permission.objects.filter(codename__exact='org').first()
    if not org_perm:
        org_perm = Permission.objects.create(codename='org', name='org', content_type=c)
    resitel_perm = Permission.objects.filter(codename__exact='resitel').first()
    if not resitel_perm:
        resitel_perm = Permission.objects.create(codename='resitel', name='resitel', content_type=c)
    for r in Resitel.objects.all():
        u = r.osoba.user
        if u:
            u.user_permissions.add(resitel_perm)
    for org in User.objects.all():
        if org and org.is_staff:
            org.user_permissions.add(org_perm)
            org.user_permissions.remove(resitel_perm)


class Migration(migrations.Migration):

    dependencies = [
        ('seminar', '0086_auto_20200819_0959'),
    ]

    operations = [
        migrations.RunPython(add_perms, migrations.RunPython.noop),
    ]