Skip to content

Commit 373f6ff

Browse files
authored
Merge pull request django-guardian#900 from django-guardian/release-3-1
Apply style rules to 3.1.0 release
2 parents 3565e88 + 77fe325 commit 373f6ff

File tree

10 files changed

+345
-406
lines changed

10 files changed

+345
-406
lines changed

guardian/management/commands/clean_orphan_obj_perms.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class Command(BaseCommand):
1313
```shell
1414
$ python manage.py clean_orphan_obj_perms
1515
Removed 11 object permission entries with no targets
16-
16+
1717
$ python manage.py clean_orphan_obj_perms --batch-size 100 --max-batches 5
1818
Removed 500 object permission entries with no targets
19-
19+
2020
$ python manage.py clean_orphan_obj_perms --batch-size 50 --skip-batches 2 --max-duration-secs 60
2121
Removed 200 object permission entries with no targets
2222
```
@@ -26,43 +26,39 @@ class Command(BaseCommand):
2626

2727
def add_arguments(self, parser):
2828
parser.add_argument(
29-
'--batch-size',
29+
"--batch-size",
3030
type=int,
31-
help='Number of objects to process per batch. If not specified, all objects are processed at once.'
31+
help="Number of objects to process per batch. If not specified, all objects are processed at once.",
3232
)
3333
parser.add_argument(
34-
'--max-batches',
35-
type=int,
36-
help='Maximum number of batches to process. Use with --batch-size.'
34+
"--max-batches", type=int, help="Maximum number of batches to process. Use with --batch-size."
3735
)
3836
parser.add_argument(
39-
'--max-duration-secs',
40-
type=int,
41-
help='Maximum duration in seconds for the cleanup operation.'
37+
"--max-duration-secs", type=int, help="Maximum duration in seconds for the cleanup operation."
4238
)
4339
parser.add_argument(
44-
'--skip-batches',
40+
"--skip-batches",
4541
type=int,
4642
default=0,
47-
help='Number of batches to skip before starting cleanup. Use with --batch-size.'
43+
help="Number of batches to skip before starting cleanup. Use with --batch-size.",
4844
)
4945

5046
def handle(self, **options):
5147
kwargs = {}
52-
53-
if options['batch_size'] is not None:
54-
kwargs['batch_size'] = options['batch_size']
55-
56-
if options['max_batches'] is not None:
57-
kwargs['max_batches'] = options['max_batches']
58-
59-
if options['max_duration_secs'] is not None:
60-
kwargs['max_duration_secs'] = options['max_duration_secs']
61-
62-
if options['skip_batches'] > 0:
63-
kwargs['skip_batches'] = options['skip_batches']
48+
49+
if options["batch_size"] is not None:
50+
kwargs["batch_size"] = options["batch_size"]
51+
52+
if options["max_batches"] is not None:
53+
kwargs["max_batches"] = options["max_batches"]
54+
55+
if options["max_duration_secs"] is not None:
56+
kwargs["max_duration_secs"] = options["max_duration_secs"]
57+
58+
if options["skip_batches"] > 0:
59+
kwargs["skip_batches"] = options["skip_batches"]
6460

6561
removed = clean_orphan_obj_perms(**kwargs)
66-
62+
6763
if options["verbosity"] > 0:
6864
self.stdout.write("Removed %d object permission entries with no targets" % removed)

guardian/managers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def assign_perm(self, perm: str, user_or_group: Any, obj: Model) -> Any:
4545
kwargs["content_object"] = obj
4646
obj_perm, _ = self.get_or_create(**kwargs)
4747
return obj_perm
48-
49-
def bulk_assign_perm(self, perm: str, user_or_group: Any, queryset: QuerySet, ignore_conflicts: bool=False) -> Any:
48+
49+
def bulk_assign_perm(
50+
self, perm: str, user_or_group: Any, queryset: QuerySet, ignore_conflicts: bool = False
51+
) -> Any:
5052
"""
5153
Bulk assigns permissions with given `perm` for an objects in `queryset` and
5254
`user_or_group`.
@@ -78,7 +80,7 @@ def bulk_assign_perm(self, perm: str, user_or_group: Any, queryset: QuerySet, ig
7880

7981
return assigned_perms
8082

81-
def assign_perm_to_many(self, perm: str, users_or_groups: Any, obj: Model, ignore_conflicts: bool=False) -> Any:
83+
def assign_perm_to_many(self, perm: str, users_or_groups: Any, obj: Model, ignore_conflicts: bool = False) -> Any:
8284
"""
8385
Bulk assigns given `perm` for the object `obj` to a set of users or a set of groups.
8486
"""

guardian/migrations/0003_remove_groupobjectpermission_guardian_gr_content_ae6aec_idx_and_more.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,40 @@
55

66

77
class Migration(migrations.Migration):
8-
98
dependencies = [
10-
('auth', '0012_alter_user_first_name_max_length'),
11-
('contenttypes', '0002_remove_content_type_name'),
12-
('guardian', '0002_generic_permissions_index'),
9+
("auth", "0012_alter_user_first_name_max_length"),
10+
("contenttypes", "0002_remove_content_type_name"),
11+
("guardian", "0002_generic_permissions_index"),
1312
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
1413
]
1514

1615
operations = [
1716
migrations.RemoveIndex(
18-
model_name='groupobjectpermission',
19-
name='guardian_gr_content_ae6aec_idx',
17+
model_name="groupobjectpermission",
18+
name="guardian_gr_content_ae6aec_idx",
2019
),
2120
migrations.RemoveIndex(
22-
model_name='userobjectpermission',
23-
name='guardian_us_content_179ed2_idx',
21+
model_name="userobjectpermission",
22+
name="guardian_us_content_179ed2_idx",
2423
),
2524
migrations.AddIndex(
26-
model_name='groupobjectpermission',
27-
index=models.Index(fields=['permission', 'group', 'content_type', 'object_pk'], name='guardian_gr_permiss_83545c_idx'),
25+
model_name="groupobjectpermission",
26+
index=models.Index(
27+
fields=["permission", "group", "content_type", "object_pk"], name="guardian_gr_permiss_83545c_idx"
28+
),
2829
),
2930
migrations.AddIndex(
30-
model_name='groupobjectpermission',
31-
index=models.Index(fields=['group', 'content_type', 'object_pk'], name='guardian_gr_group_i_9e7d12_idx'),
31+
model_name="groupobjectpermission",
32+
index=models.Index(fields=["group", "content_type", "object_pk"], name="guardian_gr_group_i_9e7d12_idx"),
3233
),
3334
migrations.AddIndex(
34-
model_name='userobjectpermission',
35-
index=models.Index(fields=['permission', 'user', 'content_type', 'object_pk'], name='guardian_us_permiss_e5749c_idx'),
35+
model_name="userobjectpermission",
36+
index=models.Index(
37+
fields=["permission", "user", "content_type", "object_pk"], name="guardian_us_permiss_e5749c_idx"
38+
),
3639
),
3740
migrations.AddIndex(
38-
model_name='userobjectpermission',
39-
index=models.Index(fields=['user', 'content_type', 'object_pk'], name='guardian_us_user_id_8eae14_idx'),
41+
model_name="userobjectpermission",
42+
index=models.Index(fields=["user", "content_type", "object_pk"], name="guardian_us_user_id_8eae14_idx"),
4043
),
4144
]

guardian/shortcuts.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ def get_users_with_perms(
379379
return users
380380

381381

382-
def get_groups_with_perms(obj: Model, attach_perms: bool = False, only_with_perms_in: Optional[list[str]] = None) -> Union[Group, dict]:
382+
def get_groups_with_perms(
383+
obj: Model, attach_perms: bool = False, only_with_perms_in: Optional[list[str]] = None
384+
) -> Union[Group, dict]:
383385
"""Get all groups with *any* object permissions for the given `obj`.
384386
385387
Parameters:
@@ -423,10 +425,14 @@ def get_groups_with_perms(obj: Model, attach_perms: bool = False, only_with_perm
423425
else:
424426
group_filters = {"%s__content_object" % group_rel_name: obj}
425427
if only_with_perms_in is not None:
426-
permission_ids = Permission.objects.filter(content_type=ctype, codename__in=only_with_perms_in).values_list("id", flat=True)
427-
group_filters.update({
428-
"%s__permission_id__in" % group_rel_name: permission_ids,
429-
})
428+
permission_ids = Permission.objects.filter(content_type=ctype, codename__in=only_with_perms_in).values_list(
429+
"id", flat=True
430+
)
431+
group_filters.update(
432+
{
433+
"%s__permission_id__in" % group_rel_name: permission_ids,
434+
}
435+
)
430436

431437
group_rel_model = group_model.group.field.related_model
432438
return group_rel_model.objects.filter(**group_filters).distinct()
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
# Generated by Django 5.2.5 on 2025-08-28 23:39
22

3-
import django.utils.timezone
43
from django.db import migrations, models
4+
import django.utils.timezone
55

66

77
class Migration(migrations.Migration):
8-
98
dependencies = [
10-
('testapp', '0007_genericgroupobjectpermission'),
9+
("testapp", "0007_genericgroupobjectpermission"),
1110
]
1211

1312
operations = [
1413
migrations.AlterField(
15-
model_name='project',
16-
name='created_at',
14+
model_name="project",
15+
name="created_at",
1716
field=models.DateTimeField(default=django.utils.timezone.now),
1817
),
1918
]

guardian/testapp/models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from datetime import datetime
21
import uuid
32

43
from django.contrib.admin.models import LogEntry

0 commit comments

Comments
 (0)