kers/users/migrations/0002_auto_20200724_2340.py

58 lines
1.5 KiB
Python
Raw Normal View History

# Generated by Django 3.0.8 on 2020-07-24 21:40
import logging
from django.contrib.auth import get_user_model
from django.core.management.sql import emit_post_migrate_signal
from django.db import migrations
from django.utils import timezone
logger = logging.getLogger(__name__)
def create_superuser(apps, schema_editor):
superuser = get_user_model()(
is_superuser=True,
is_staff=True,
username="admin", # os.environ['ADMIN_USERNAME'],
last_login=timezone.now(),
)
# superuser.set_password(os.environ['ADMIN_PASSWORD'])
superuser.set_password('admin')
superuser.save()
kers_group_permissions = {
"Bestuur": [
"add_event",
"delete_event",
"change_event",
]
}
def add_group_permissions(apps, schema_editor):
db_alias = schema_editor.connection.alias
emit_post_migrate_signal(2, False, db_alias)
Group = apps.get_model('auth', 'Group')
Permission = apps.get_model('auth', 'Permission')
for group in kers_group_permissions:
role, created = Group.objects.get_or_create(name=group)
logger.info(f'{group} Group created')
for perm in kers_group_permissions[group]:
role.permissions.add(Permission.objects.get(codename=perm))
logger.info(f'Permitting {group} to {perm}')
role.save()
class Migration(migrations.Migration):
dependencies = [
('users', '0001_initial'),
]
operations = [
migrations.RunPython(create_superuser),
migrations.RunPython(add_group_permissions),
]