Skip to content

Commit 097c776

Browse files
authored
Add ENABLE_GLOBAL_MODEL_AUTO_REGISTRATION setting (#259)
This is true by default, but when set to false, the GlobalPreferenceModel isn't registered anymore when ready() is called on the app config. This is needed when you want to register your own model in one of your apps (e.g. a subclass of GlobalPreferenceModel).
1 parent dd511ea commit 097c776

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

docs/installation.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ Also, take some time to look at provided settings if you want to customize the p
6565
# want to change, but it's here just in case
6666
'SECTION_KEY_SEPARATOR': '__',
6767
68+
# Use this to disable auto registration of the GlobalPreferenceModel.
69+
# This can be useful to register your own model in the global_preferences_registry.
70+
'ENABLE_GLOBAL_MODEL_AUTO_REGISTRATION': True,
71+
6872
# Use this to disable caching of preference. This can be useful to debug things
6973
'ENABLE_CACHE': True,
7074

dynamic_preferences/apps.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
from django.conf import settings
33
from django.utils.translation import gettext_lazy as _
44
from .registries import preference_models, global_preferences_registry
5+
from .settings import preferences_settings
56

67

78
class DynamicPreferencesConfig(AppConfig):
89
name = 'dynamic_preferences'
910
verbose_name = _("Dynamic Preferences")
1011

1112
def ready(self):
12-
GlobalPreferenceModel = self.get_model('GlobalPreferenceModel')
13+
if preferences_settings.ENABLE_GLOBAL_MODEL_AUTO_REGISTRATION:
14+
GlobalPreferenceModel = self.get_model('GlobalPreferenceModel')
1315

14-
preference_models.register(
15-
GlobalPreferenceModel, global_preferences_registry)
16+
preference_models.register(
17+
GlobalPreferenceModel, global_preferences_registry)
1618

1719
# This will load all dynamic_preferences_registry.py files under
1820
# installed apps

dynamic_preferences/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'SECTION_KEY_SEPARATOR': '__',
2020
'REGISTRY_MODULE': 'dynamic_preferences_registry',
2121
'ADMIN_ENABLE_CHANGELIST_FORM': False,
22+
'ENABLE_GLOBAL_MODEL_AUTO_REGISTRATION': True,
2223
'ENABLE_USER_PREFERENCES': True,
2324
'ENABLE_CACHE': True,
2425
'VALIDATE_NAMES': True,

tests/test_global_preferences.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
from decimal import Decimal
55

66
from datetime import date, timedelta, datetime, time
7+
from django.apps import apps
78
from django.test import LiveServerTestCase, TestCase
9+
from django.test.utils import override_settings
810
from django.urls import reverse
911
from django.core.management import call_command
1012
from django.core.cache import caches
@@ -55,6 +57,17 @@ def test_preference_model_manager_to_dict(self):
5557
u'user__registration_allowed': False}
5658
self.assertDictEqual(manager.all(), expected)
5759

60+
def test_registry_default_preference_model(self):
61+
app_config = apps.app_configs["dynamic_preferences"]
62+
registry.preference_model = None
63+
64+
with override_settings(DYNAMIC_PREFERENCES={'ENABLE_GLOBAL_MODEL_AUTO_REGISTRATION': False}):
65+
app_config.ready()
66+
self.assertIs(registry.preference_model, None)
67+
68+
app_config.ready()
69+
self.assertIs(registry.preference_model, GlobalPreferenceModel)
70+
5871

5972
class TestViews(BaseTest, LiveServerTestCase):
6073

0 commit comments

Comments
 (0)