Debian Codegraph @

guardian/management/commands/clean_orphan_obj_perms.py

raw
1 from django.core.management.base import BaseCommand
2
3 from guardian.utils import clean_orphan_obj_perms
4
5
6 class Command(BaseCommand):
7 """A wrapper around `guardian.utils.clean_orphan_obj_perms`.
8
9 Seeks and removes all object permissions entries pointing at non-existing targets.
10 Returns the number of objects removed.
11
12 Example:
13 ```shell
14 $ python manage.py clean_orphan_obj_perms
15 Removed 11 object permission entries with no targets
16
17 $ python manage.py clean_orphan_obj_perms --batch-size 100 --max-batches 5
18 Removed 500 object permission entries with no targets
19
20 $ python manage.py clean_orphan_obj_perms --batch-size 50 --skip-batches 2 --max-duration-secs 60
21 Removed 200 object permission entries with no targets
22 ```
23 """
24
25 help = "Removes object permissions with not existing targets"
26
27 def add_arguments(self, parser):
28 parser.add_argument(
29 "--batch-size",
30 type=int,
31 help="Number of objects to process per batch. If not specified, all objects are processed at once.",
32 )
33 parser.add_argument(
34 "--max-batches", type=int, help="Maximum number of batches to process. Use with --batch-size."
35 )
36 parser.add_argument(
37 "--max-duration-secs", type=int, help="Maximum duration in seconds for the cleanup operation."
38 )
39 parser.add_argument(
40 "--skip-batches",
41 type=int,
42 default=0,
43 help="Number of batches to skip before starting cleanup. Use with --batch-size.",
44 )
45
46 def handle(self, **options):
47 kwargs = {}
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"]
60
61 removed = clean_orphan_obj_perms(**kwargs)
62
63 if options["verbosity"] > 0:
64 self.stdout.write("Removed %d object permission entries with no targets" % removed)