Optimize create_extra_permissions
Showing 1 of 1 files from the diff.
src/adminactions/perms.py
changed.
@@ -24,14 +24,26 @@
Loading
24 | 24 | # ('adminactions_export', 'adminactions_massupdate', |
|
25 | 25 | # 'adminactions_merge', 'adminactions_chart', |
|
26 | 26 | # 'adminactions_byrowsupdate') |
|
27 | - | for model in apps.get_models(): |
|
27 | + | perm_suffix = 'adminactions_' |
|
28 | + | existing_perms = set( |
|
29 | + | Permission.objects.filter(codename__startswith=perm_suffix).values_list( |
|
30 | + | 'codename', 'content_type_id' |
|
31 | + | ) |
|
32 | + | ) |
|
33 | + | models = list(apps.get_models()) |
|
34 | + | content_types = ContentType.objects.get_for_models(*models) |
|
35 | + | ||
36 | + | new_permissions = [] |
|
37 | + | for model in models: |
|
28 | 38 | for action in aa: |
|
29 | 39 | opts = model._meta |
|
30 | - | codename = get_permission_codename(action.base_permission, opts) |
|
31 | - | label = 'Can {} {} (adminactions)'.format(action.base_permission.replace('adminactions_', ""), |
|
40 | + | codename = get_permission_codename(action.base_permission, opts)[:100] |
|
41 | + | ct = content_types[model] |
|
42 | + | if (codename, ct.id) in existing_perms: |
|
43 | + | continue |
|
44 | + | label = 'Can {} {} (adminactions)'.format(action.base_permission.replace(perm_suffix, ""), |
|
32 | 45 | opts.verbose_name_raw) |
|
33 | - | ct = ContentType.objects.get_for_model(model) |
|
34 | - | params = dict(codename=codename, |
|
35 | - | content_type=ct, |
|
36 | - | defaults={'name': label[:50]}) |
|
37 | - | Permission.objects.get_or_create(**params) |
|
46 | + | permission = Permission(codename=codename, content_type=ct, name=label[:255]) |
|
47 | + | new_permissions.append(permission) |
|
48 | + | ||
49 | + | Permission.objects.bulk_create(new_permissions, ignore_conflicts=True) |
Files | Coverage |
---|---|
src/adminactions | 84.84% |
Project Totals (23 files) | 84.84% |
1435498935
1435498935
1435498935
1435498935
1435498935
1435498935
1435498935
1435498935
1435498935
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file.
The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files.
The size and color of each slice is representing the number of statements and the coverage, respectively.