| 39 | |
| 40 | |
| 41 | def create_permissions( |
| 42 | app_config, |
| 43 | verbosity=2, |
| 44 | interactive=True, |
| 45 | using=DEFAULT_DB_ALIAS, |
| 46 | apps=global_apps, |
| 47 | **kwargs, |
| 48 | ): |
| 49 | if not app_config.models_module: |
| 50 | return |
| 51 | |
| 52 | try: |
| 53 | Permission = apps.get_model("auth", "Permission") |
| 54 | except LookupError: |
| 55 | return |
| 56 | if not router.allow_migrate_model(using, Permission): |
| 57 | return |
| 58 | |
| 59 | # Ensure that contenttypes are created for this app. Needed if |
| 60 | # 'django.contrib.auth' is in INSTALLED_APPS before |
| 61 | # 'django.contrib.contenttypes'. |
| 62 | create_contenttypes( |
| 63 | app_config, |
| 64 | verbosity=verbosity, |
| 65 | interactive=interactive, |
| 66 | using=using, |
| 67 | apps=apps, |
| 68 | **kwargs, |
| 69 | ) |
| 70 | |
| 71 | app_label = app_config.label |
| 72 | try: |
| 73 | app_config = apps.get_app_config(app_label) |
| 74 | ContentType = apps.get_model("contenttypes", "ContentType") |
| 75 | except LookupError: |
| 76 | return |
| 77 | |
| 78 | models = list(app_config.get_models()) |
| 79 | |
| 80 | # Grab all the ContentTypes. |
| 81 | ctypes = ContentType.objects.db_manager(using).get_for_models( |
| 82 | *models, for_concrete_models=False |
| 83 | ) |
| 84 | |
| 85 | # Find all the Permissions that have a content_type for a model we're |
| 86 | # looking for. We don't need to check for codenames since we already have |
| 87 | # a list of the ones we're going to create. |
| 88 | all_perms = set( |
| 89 | Permission.objects.using(using) |
| 90 | .filter( |
| 91 | content_type__in=set(ctypes.values()), |
| 92 | ) |
| 93 | .values_list("content_type", "codename") |
| 94 | ) |
| 95 | |
| 96 | perms = [] |
| 97 | for model in models: |
| 98 | ctype = ctypes[model] |