Skip to content

Commit ba4305d

Browse files
authored
Allow to skip preference creation when checkpreferences is invoked (#257)
In some case you might not want to prefill the cache, only to cleanup the removed preferences. Thus it would be convenient to skip the creation steps.
1 parent 07362d9 commit ba4305d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

dynamic_preferences/management/commands/checkpreferences.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,25 @@ class Command(BaseCommand):
2929
help = (
3030
"Find and delete preferences from database if they don't exist in "
3131
"registries. Create preferences that are not present in database"
32+
"(except when invoked with --skip_create)."
3233
)
3334

35+
def add_arguments(self, parser):
36+
parser.add_argument(
37+
"--skip_create",
38+
action="store_true",
39+
help="Forces to skip the creation step for missing preferences",
40+
)
41+
3442
def handle(self, *args, **options):
43+
skip_create = options["skip_create"]
3544

3645
# Create needed preferences
3746
# Global
38-
self.stdout.write("Creating missing global preferences...")
39-
manager = global_preferences_registry.manager()
40-
manager.all()
47+
if not skip_create:
48+
self.stdout.write("Creating missing global preferences...")
49+
manager = global_preferences_registry.manager()
50+
manager.all()
4151

4252
deleted = delete_preferences(GlobalPreferenceModel.objects.all())
4353
message = "Deleted {deleted} global preferences".format(
@@ -57,6 +67,9 @@ def handle(self, *args, **options):
5767
if not hasattr(preference_model, 'get_instance_model'):
5868
continue
5969

70+
if skip_create:
71+
continue
72+
6073
message = (
6174
"Creating missing preferences for {model} model...".format(
6275
model=preference_model.get_instance_model().__name__,

tests/test_checkpreferences_command.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ def test_dry_run(self):
3333
"Creating missing preferences for User model...",
3434
])
3535
self.assertEqual(out, expected_output)
36+
37+
def test_skip_create(self):
38+
out = self.call_command("--skip_create", verbosity=0)
39+
expected_output = "\n".join([
40+
"Deleted 0 global preferences",
41+
"Deleted 0 GlobalPreferenceModel preferences",
42+
"Deleted 0 UserPreferenceModel preferences",
43+
])
44+
self.assertEqual(out, expected_output)

0 commit comments

Comments
 (0)