Skip to content

Commit 10dadc9

Browse files
Use stdout in checkpreferences (#252)
* Change logging for stdout in checkpreferences command * Remove nose print stdout
1 parent d28130d commit 10dadc9

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed
Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
from django.core.management.base import BaseCommand, CommandError
1+
from django.core.management.base import BaseCommand
22
from dynamic_preferences.exceptions import NotFoundInRegistry
33
from dynamic_preferences.models import GlobalPreferenceModel
4-
from dynamic_preferences.registries import preference_models, global_preferences_registry
5-
6-
import logging
7-
logger = logging.getLogger(__name__)
4+
from dynamic_preferences.registries import (global_preferences_registry,
5+
preference_models)
86

97

108
def delete_preferences(queryset):
119
"""
12-
Delete preferences objects if they are not present in registry. Return a list of deleted objects
10+
Delete preferences objects if they are not present in registry.
11+
Return a list of deleted objects
1312
"""
1413
deleted = []
1514

16-
# Iterate through preferences. If an error is raised when accessing preference object, just delete it
15+
# Iterate through preferences. If an error is raised when accessing
16+
# preference object, just delete it
1717
for p in queryset:
1818
try:
19-
pref = p.registry.get(section=p.section, name=p.name, fallback=False)
19+
p.registry.get(
20+
section=p.section, name=p.name, fallback=False)
2021
except NotFoundInRegistry:
2122
p.delete()
2223
deleted.append(p)
@@ -25,26 +26,42 @@ def delete_preferences(queryset):
2526

2627

2728
class Command(BaseCommand):
28-
help = "Find and delete preferences from database if they don't exist in registries. Create preferences that are " \
29-
"not present in database"
29+
help = (
30+
"Find and delete preferences from database if they don't exist in "
31+
"registries. Create preferences that are not present in database"
32+
)
3033

3134
def handle(self, *args, **options):
3235

3336
# Create needed preferences
3437
# Global
35-
logger.info('Creating missing global preferences...')
38+
self.stdout.write("Creating missing global preferences...")
3639
manager = global_preferences_registry.manager()
3740
manager.all()
3841

3942
deleted = delete_preferences(GlobalPreferenceModel.objects.all())
40-
logger.info("Deleted {0} global preferences".format(len(deleted)))
43+
message = "Deleted {deleted} global preferences".format(
44+
deleted=len(deleted)
45+
)
46+
self.stdout.write(message)
4147

4248
for preference_model, registry in preference_models.items():
4349
deleted = delete_preferences(preference_model.objects.all())
44-
logger.info("Deleted {0} {1} preferences".format(len(deleted), preference_model.__name__))
50+
message = (
51+
"Deleted {deleted} {model} preferences".format(
52+
deleted=len(deleted),
53+
model=preference_model.__name__,
54+
)
55+
)
56+
self.stdout.write(message)
4557
if not hasattr(preference_model, 'get_instance_model'):
4658
continue
4759

48-
logger.info('Creating missing preferences for {0} model...'.format(preference_model.get_instance_model().__name__))
60+
message = (
61+
"Creating missing preferences for {model} model...".format(
62+
model=preference_model.get_instance_model().__name__,
63+
)
64+
)
65+
self.stdout.write(message)
4966
for instance in preference_model.get_instance_model().objects.all():
5067
instance.preferences.all()

tests/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
STATIC_URL = '/static/'
2626
MEDIA_URL = '/media/'
2727
MEDIA_ROOT = tempfile.mkdtemp()
28-
NOSE_ARGS = ['-s']
2928
MIDDLEWARE = (
3029
'django.contrib.sessions.middleware.SessionMiddleware',
3130
'django.middleware.common.CommonMiddleware',
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from io import StringIO
2+
3+
from django.core.cache import caches
4+
from django.core.management import call_command
5+
from django.test import TestCase
6+
7+
8+
class BaseTest(object):
9+
10+
def tearDown(self):
11+
caches["default"].clear()
12+
13+
14+
class TestCheckPreferencesCommand(BaseTest, TestCase):
15+
def call_command(self, *args, **kwargs):
16+
out = StringIO()
17+
call_command(
18+
"checkpreferences",
19+
*args,
20+
stdout=out,
21+
stderr=StringIO(),
22+
**kwargs,
23+
)
24+
return out.getvalue().strip()
25+
26+
def test_dry_run(self):
27+
out = self.call_command(verbosity=0)
28+
expected_output = "\n".join([
29+
"Creating missing global preferences...",
30+
"Deleted 0 global preferences",
31+
"Deleted 0 GlobalPreferenceModel preferences",
32+
"Deleted 0 UserPreferenceModel preferences",
33+
"Creating missing preferences for User model...",
34+
])
35+
self.assertEqual(out, expected_output)

0 commit comments

Comments
 (0)