Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset settings and fix for AppConfig.ready #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that running this command before migrate on first install will raise an Exception.

- 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this line current tests fail.

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that these tests are not enough and they don't cover well real usage, I try to explain better what I mean:

  • Tests pass because Setting.set_defaults_from_settings() is invoked manually (it should not)
  • Setting.set_defaults_from_settings() should be called automatically at some point at the begininng, but now calling it on app ready is discouraged.
  • Should improve the tests using more EXTRA_SETTINGS_DEFAULTS

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"))
Loading