diff --git a/README.md b/README.md index cc63cdd..e0d3594 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ config and manage typed extra settings using just the django admin. ## Installation - Run `pip install django-extra-settings` - Add `extra_settings` to `settings.INSTALLED_APPS` +- Run `python manage.py reset_extra_settings` - Run `python manage.py migrate` - Run `python manage.py collectstatic` - Restart your application server @@ -234,6 +235,11 @@ def test_with_custom_settings(self): pass ``` +### Reset +You can remove all settings and revert to default values described in `settings.py`. +For that you could run this command `python manage.py reset_extra_settings`. + + ## Testing ```bash # clone repository diff --git a/extra_settings/apps.py b/extra_settings/apps.py index b1e564a..0bcf0fc 100644 --- a/extra_settings/apps.py +++ b/extra_settings/apps.py @@ -1,6 +1,5 @@ from django.apps import AppConfig from django.conf import settings -from django.db.models.signals import post_migrate class ExtraSettingsConfig(AppConfig): @@ -10,6 +9,3 @@ class ExtraSettingsConfig(AppConfig): def ready(self): from extra_settings import signals # noqa: F401 - from extra_settings.models import Setting - - post_migrate.connect(Setting.set_defaults_from_settings, sender=self) diff --git a/extra_settings/management/__init__.py b/extra_settings/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/extra_settings/management/commands/__init__ .py b/extra_settings/management/commands/__init__ .py new file mode 100644 index 0000000..e69de29 diff --git a/extra_settings/management/commands/reset_extra_settings.py b/extra_settings/management/commands/reset_extra_settings.py new file mode 100644 index 0000000..5599544 --- /dev/null +++ b/extra_settings/management/commands/reset_extra_settings.py @@ -0,0 +1,27 @@ +""" +Command to reset extra settings. +""" + +import logging + +from django.core.management import BaseCommand +from extra_settings.models import Setting + +log = logging.getLogger(__name__) + + +class Command(BaseCommand): + """Django-command for refreshing extra settings to default values""" + + help = """ + This command will remove all extra settings and recreate only those + that described in `settings.EXTRA_SETTINGS_DEFAULTS`. + """ + + def handle(self, *_, **__) -> None: + """ + Handle command. + """ + log.info("Start refreshing extra settings...") + Setting.reset_to_default() + log.info("Refreshing of extra settings is done.") diff --git a/extra_settings/models.py b/extra_settings/models.py index 9e3a2b0..f5c0569 100644 --- a/extra_settings/models.py +++ b/extra_settings/models.py @@ -101,6 +101,12 @@ def set_defaults(cls, defaults): def set_defaults_from_settings(cls, *args, **kwargs): cls.set_defaults(settings.EXTRA_SETTINGS_DEFAULTS) + @classmethod + def reset_to_default(cls) -> None: + """Reset all settings to default values""" + cls.objects.all().delete() + cls.set_defaults_from_settings() + TYPE_BOOL = "bool" # TYPE_COLOR = "color" # TODO TYPE_DATE = "date" diff --git a/tests/test_models.py b/tests/test_models.py index 09b90c9..1abab94 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -4,7 +4,11 @@ class ExtraSettingsModelsTestCase(TestCase): - def setUp(self): + @classmethod + def setUpClass(cls) -> None: + """setup tests""" + super().setUpClass() + Setting.set_defaults_from_settings() Setting.objects.bulk_create( [ Setting( @@ -79,9 +83,6 @@ def setUp(self): ] ) - def tearDown(self): - pass - def test_create_setting(self): # bool setting_value = True @@ -185,11 +186,19 @@ def test_repr(self): self.assertEqual(f"{setting_obj}", setting_repr) def test_set_defaults_from_settings(self): + Setting.set_defaults_from_settings() self.assertEqual( Setting.get("TEST_DEFAULT_URL"), "https://github.com/fabiocaccamo/django-extra-settings", ) + def test_set_defaults_from_settings_do_not_update_updated_value(self): + obj = Setting.objects.get(name="TEST_DEFAULT_URL") + obj.value = "foo" + obj.save() + Setting.set_defaults_from_settings() + self.assertEqual(Setting.get("TEST_DEFAULT_URL"), "foo") + def test_set_defaults(self): Setting.set_defaults([]) defaults = [ @@ -246,3 +255,23 @@ def test_setting_type_json(self): setting_obj.save() setting_obj = Setting.objects.get(name="TEST_SETTING_JSON") self.assertEqual(setting_obj.value, {"level": "L2", "role": "Admin"}) + + def test_reset_settings_to_default_values(self) -> None: + """Should reset all settings to default values from settings""" + obj = Setting.objects.get(name="TEST_DEFAULT_URL") + obj.value = "foo" + obj.save() + Setting.objects.create(name="TEST", value_type=Setting.TYPE_BOOL) + self.assertEqual(Setting.get("TEST"), False) + Setting.reset_to_default() + self.assertEqual(Setting.objects.count(), 1) + obj = Setting.objects.get(name="TEST_DEFAULT_URL") + self.assertEqual( + obj.value, "https://github.com/fabiocaccamo/django-extra-settings" + ) + # ensure cache was cleaned + self.assertEqual( + Setting.get(name="TEST_DEFAULT_URL"), + "https://github.com/fabiocaccamo/django-extra-settings", + ) + self.assertIsNone(Setting.get("TEST"))