Skip to content

Commit

Permalink
improve docs and naming of helper methods on the settings base model
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Nov 13, 2019
1 parent 985c31f commit b6e8e13
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
12 changes: 9 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ implementation that you can use, the abstract model ``SettingsModel`` can be use
construct your own settings model(s), and things like webserver restarts are handled in
the abstract model class.

**The Problem**: Sometimes you want to build an app that can run on an arbitrary piece
of equipment, and things like timezone, hostname, or SMTP settings may need to be
**The Problem**: Sometimes you want to build an app that can be managed by
non-developers, and things like timezone, hostname, or SMTP settings may need to be
editable from the UI.

**The Solution**: This app implements a base ``SettingsModel`` class that allows you to
expose settings to a user interface via the database.
expose settings to a user interface via the database. It writes these stub settings to
separate files which you try/include at the end of your main settings file. This way
you have sensible defaults, but when the user created/edits a model file in the UI,
those settings override. There is a mechanism to touch the ``wsgi.py``/``manage.py``
files when saving, so the only thing left to do is either configure your web server to
watch those files and reboot when they are touched, or use a tool like ``incrond`` to
watch those files and trigger your webserver to reboot when they are touched.


How to Use
Expand Down
2 changes: 1 addition & 1 deletion settings_model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.4.0"
__version__ = "0.5.0"
default_app_config = "settings_model.apps.CustomConfig"
10 changes: 5 additions & 5 deletions settings_model/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ def save(self, *args, **kwargs):
# if we are saving an active settings instance, reboot the web server
if self.is_active:
if reboot:
transaction.on_commit(lambda: self.write_and_reboot())
transaction.on_commit(lambda: self.write_and_signal_reboot())
return super().save(*args, **kwargs)

@staticmethod
def touch_reboot_files():
def signal_reboot():
"""
Find and touch the reboot files to signal the server to restart.
"""
Expand Down Expand Up @@ -176,16 +176,16 @@ def touch_reboot_files():
else:
print("settings_model: - {} (skipped, doesn't exist)".format(f))

def write_and_reboot(self, commit=True):
def write_and_signal_reboot(self, commit=True):
"""
Commit the configuration to disk and call `touch_reboot_files`.
Commit the configuration to disk and call `signal_reboot`.
"""
if commit:
print("settings_model: committing config to disk...")
self.write_settings()

# signal reboot
self.touch_reboot_files()
self.signal_reboot()


class Settings(SettingsModel):
Expand Down
2 changes: 1 addition & 1 deletion settings_model/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_change_settings(self):
self.assertEqual(self.settings.debug_mode, not old_debug)

def test_write_and_reboot(self):
self.settings.write_and_reboot()
self.settings.write_and_signal_reboot()


class SettingsModelAdminTestCase(TestCase):
Expand Down

0 comments on commit b6e8e13

Please sign in to comment.