0001_create_admin_access_permissions.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. from django.db import migrations
  3. def create_admin_access_permissions(apps, schema_editor):
  4. ContentType = apps.get_model('contenttypes.ContentType')
  5. Permission = apps.get_model('auth.Permission')
  6. Group = apps.get_model('auth.Group')
  7. # Add a fake content type to hang the 'can access Wagtail admin' permission off.
  8. # The fact that this doesn't correspond to an actual defined model shouldn't matter, I hope...
  9. wagtailadmin_content_type, created = ContentType.objects.get_or_create(
  10. app_label='wagtailadmin',
  11. model='admin'
  12. )
  13. # Create admin permission
  14. admin_permission, created = Permission.objects.get_or_create(
  15. content_type=wagtailadmin_content_type,
  16. codename='access_admin',
  17. name='Can access Wagtail admin'
  18. )
  19. # Assign it to Editors and Moderators groups
  20. for group in Group.objects.filter(name__in=['Editors', 'Moderators']):
  21. group.permissions.add(admin_permission)
  22. def remove_admin_access_permissions(apps, schema_editor):
  23. """Reverse the above additions of permissions."""
  24. ContentType = apps.get_model('contenttypes.ContentType')
  25. Permission = apps.get_model('auth.Permission')
  26. wagtailadmin_content_type = ContentType.objects.get(
  27. app_label='wagtailadmin',
  28. model='admin',
  29. )
  30. # This cascades to Group
  31. Permission.objects.filter(
  32. content_type=wagtailadmin_content_type,
  33. codename='access_admin',
  34. ).delete()
  35. class Migration(migrations.Migration):
  36. dependencies = [
  37. # We cannot apply and unapply this migration unless GroupCollectionPermission
  38. # is created. #2529
  39. ('wagtailcore', '0026_group_collection_permission'),
  40. ]
  41. operations = [
  42. migrations.RunPython(create_admin_access_permissions, remove_admin_access_permissions),
  43. ]