Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- from functools import partial
- from django.conf import settings
- from django.contrib.contenttypes.management import update_contenttypes
- from django.contrib.contenttypes.models import ContentType
- from django.db import migrations
- def update_all_contenttypes(apps):
- for app_config in apps.get_app_configs():
- update_contenttypes(app_config)
- def make_permission(apps, schema_editor, with_create_permissions = True, codename = None):
- """
- Make permission object on User model with name codename (which should not be left None).
- Note: AUTH_USER_MODEL doesn't reflect settings here for some reason
- """
- app_label, model = settings.AUTH_USER_MODEL.split('.')
- update_all_contenttypes(apps)
- UserType = ContentType.objects.get(app_label = app_label, model = model)
- Permission = apps.get_model('auth', 'Permission')
- try:
- Permission.objects.get(
- codename = codename,
- content_type = UserType
- )
- except Permission.DoesNotExist:
- perm = Permission.objects.create(
- codename = codename,
- content_type = UserType
- )
- class Migration(migrations.Migration):
- dependencies = [
- ('sessions', '0001_initial'),
- ('auth', '0006_require_contenttypes_0002'),
- ('contenttypes', '0002_remove_content_type_name'),
- ]
- operations = [
- migrations.RunPython(partial(make_permission, codename = 'can_make_backup')),
- migrations.RunPython(partial(make_permission, codename = 'can_upload_backup')),
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement