Skip to content

Commit

Permalink
added command to reset settings to default, removed app ready hook
Browse files Browse the repository at this point in the history
renamed method, added unit tests
  • Loading branch information
ar0ne committed Apr 15, 2024
1 parent 2ebf4f3 commit 7608c7e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions extra_settings/apps.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions extra_settings/management/commands/reset_extra_settings.py
Original file line number Diff line number Diff line change
@@ -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.")
6 changes: 6 additions & 0 deletions extra_settings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 33 additions & 4 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -79,9 +83,6 @@ def setUp(self):
]
)

def tearDown(self):
pass

def test_create_setting(self):
# bool
setting_value = True
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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"))

0 comments on commit 7608c7e

Please sign in to comment.