Advertisement
Guest User

Untitled

a guest
Jun 10th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from __future__ import unicode_literals
  4. from functools import partial
  5. from django.conf import settings
  6. from django.contrib.contenttypes.management import update_contenttypes
  7. from django.contrib.contenttypes.models import ContentType
  8. from django.db import migrations
  9.  
  10.  
  11.  
  12. def update_all_contenttypes(apps):
  13.     for app_config in apps.get_app_configs():
  14.         update_contenttypes(app_config)
  15.  
  16.  
  17. def make_permission(apps, schema_editor, with_create_permissions = True, codename = None):
  18.     """
  19.         Make permission object on User model with name codename (which should not be left None).
  20.  
  21.         Note: AUTH_USER_MODEL doesn't reflect settings here for some reason
  22.     """
  23.     app_label, model = settings.AUTH_USER_MODEL.split('.')
  24.     update_all_contenttypes(apps)
  25.     UserType = ContentType.objects.get(app_label = app_label, model = model)
  26.     Permission = apps.get_model('auth', 'Permission')
  27.     try:
  28.         Permission.objects.get(
  29.             codename = codename,
  30.             content_type = UserType
  31.         )
  32.     except Permission.DoesNotExist:
  33.         perm = Permission.objects.create(
  34.             codename = codename,
  35.             content_type = UserType
  36.         )
  37.  
  38.  
  39. class Migration(migrations.Migration):
  40.  
  41.     dependencies = [
  42.         ('sessions', '0001_initial'),
  43.         ('auth', '0006_require_contenttypes_0002'),
  44.         ('contenttypes', '0002_remove_content_type_name'),
  45.     ]
  46.  
  47.     operations = [
  48.         migrations.RunPython(partial(make_permission, codename = 'can_make_backup')),
  49.         migrations.RunPython(partial(make_permission, codename = 'can_upload_backup')),
  50.     ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement