• Stars
    star
    359
  • Rank 118,537 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

โš™๏ธ config and manage typed extra settings using just the django admin.

django-extra-settings

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 migrate
  • Run python manage.py collectstatic
  • Restart your application server
  • Just go to the admin where you can create, update and delete your settings.

Usage

Settings

All these settings are optional, if not defined in settings.py the default values (listed below) will be used.

# the name of the installed app for registering the extra settings admin.
EXTRA_SETTINGS_ADMIN_APP = "extra_settings"
# the name of the cache to use, if not found the "default" cache will be used.
EXTRA_SETTINGS_CACHE_NAME = "extra_settings"
# a list of settings that will be available by default, each item must contain "name", "type" and "value".
# check the #types section to see all the supported settings types.
EXTRA_SETTINGS_DEFAULTS = [
    {
        "name": "SETTING_NAME",
        "type": "string",
        "value": "Hello World",
    },
    # ...
]
# if True, settings names will be forced to honor the standard django settings format
EXTRA_SETTINGS_ENFORCE_UPPERCASE_SETTINGS = True
# if True, the template tag will fallback to django.conf.settings,
# very useful to retrieve conf settings such as DEBUG.
EXTRA_SETTINGS_FALLBACK_TO_CONF_SETTINGS = True
# the upload_to path value of settings of type 'file'
EXTRA_SETTINGS_FILE_UPLOAD_TO = "files"
# the upload_to path value of settings of type 'image'
EXTRA_SETTINGS_IMAGE_UPLOAD_TO = "images"
# if True, settings name prefix list filter will be shown in the admin changelist
EXTRA_SETTINGS_SHOW_NAME_PREFIX_LIST_FILTER = False
# if True, settings type list filter will be shown in the admin changelist
EXTRA_SETTINGS_SHOW_TYPE_LIST_FILTER = False
# the package name displayed in the admin
EXTRA_SETTINGS_VERBOSE_NAME = "Settings"

Admin

You can display the settings model admin in another installed app group by using the EXTRA_SETTINGS_ADMIN_APP setting.

You can also have a more advanced control, by registering the settings admin with multiple installed apps and filtering each app settings using the queryset_processor argument.

โš ๏ธ If you do either of the above, you must run migrations for each app that will display extra_settings model admin in its admin (because django creates migrations even for proxy models).

Admin advanced configuration example

In your custom app photos.admin module:

from extra_settings.admin import register_extra_settings_admin

register_extra_settings_admin(
    app=__name__,
    queryset_processor=lambda qs: qs.filter(name__istartswith="PHOTOS_"),
    unregister_default=True,
)

In your custom app videos.admin module:

from extra_settings.admin import register_extra_settings_admin

register_extra_settings_admin(
    app=__name__,
    queryset_processor=lambda qs: qs.filter(name__istartswith="VIDEOS_"),
    unregister_default=True,
)

By default the "extra_settings" app has its own admin app group.

Caching

You can customise the app caching options using settings.CACHES["extra_settings"] setting, otherwise the "default" cache will be used:

CACHES = {
    # ...
    "extra_settings": {
        "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
        "TIMEOUT": 60,
    },
    # ...
}

By default the "extra_settings" cache is used, if you want to use another cache you can set it using the EXTRA_SETTINGS_CACHE_NAME setting.

Python

You can create, read, update and delete settings programmatically:

Types

This is the list of the currently supported setting types you may need to use:

  • Setting.TYPE_BOOL
  • Setting.TYPE_DATE
  • Setting.TYPE_DATETIME
  • Setting.TYPE_DECIMAL
  • Setting.TYPE_DURATION
  • Setting.TYPE_EMAIL
  • Setting.TYPE_FILE
  • Setting.TYPE_FLOAT
  • Setting.TYPE_IMAGE
  • Setting.TYPE_INT
  • Setting.TYPE_JSON
  • Setting.TYPE_STRING
  • Setting.TYPE_TEXT
  • Setting.TYPE_TIME
  • Setting.TYPE_URL

Create

from extra_settings.models import Setting

setting_obj = Setting(
    name="SETTING_NAME",
    value_type=Setting.TYPE_STRING,
    value="django-extra-settings",
)
setting_obj.save()

Read

from extra_settings.models import Setting

value = Setting.get("SETTING_NAME", default="django-extra-settings")

Update

from extra_settings.models import Setting

setting_obj = Setting(
    name="SETTING_NAME",
    value_type=Setting.TYPE_BOOL,
    value=True,
)
setting_obj.value = False
setting_obj.save()

Delete

from extra_settings.models import Setting

Setting.objects.filter(name="SETTING_NAME").delete()

Validators

You can define a custom validator for each setting:

  • Validators must be defined using full python path, eg. myapp.mymodule.my_validator.
  • Validators are called passing a single argument (the value of the setting) and if the value is valid, they should return True, otherwise returning False or None a ValidationError is raised.

Templates

You can retrieve settings in templates:

{% load extra_settings %}

{% get_setting 'SETTING_NAME' default='django-extra-settings' %}

Tests

You can override specific settings during tests using extra_settings.test.override_settings.

It can be used both as decorator and as context-manager:

from extra_settings.test import override_settings

# decorator
@override_settings(SETTING_NAME_1="value for testing 1", SETTING_NAME_2="value for testing 2")
def test_with_custom_settings(self):
    pass

# context manager
def test_with_custom_settings(self):
    with override_settings(SETTING_NAME_1="value for testing 1", SETTING_NAME_2="value for testing 2"):
        pass

Testing

# clone repository
git clone https://github.com/fabiocaccamo/django-extra-settings.git && cd django-extra-settings

# create virtualenv and activate it
python -m venv venv && . venv/bin/activate

# upgrade pip
python -m pip install --upgrade pip

# install requirements
pip install -r requirements.txt -r requirements-test.txt

# install pre-commit to run formatters and linters
pre-commit install --install-hooks

# run tests
tox
# or
python runtests.py
# or
python -m django test --settings "tests.settings"

License

Released under MIT License.


Supporting

See also

  • django-admin-interface - the default admin interface made customizable by the admin itself. popup windows replaced by modals. ๐Ÿง™ โšก

  • django-colorfield - simple color field for models with a nice color-picker in the admin. ๐ŸŽจ

  • django-maintenance-mode - shows a 503 error page when maintenance-mode is on. ๐Ÿšง ๐Ÿ› ๏ธ

  • django-redirects - redirects with full control. โ†ช๏ธ

  • django-treenode - probably the best abstract model / admin for your tree based stuff. ๐ŸŒณ

  • python-benedict - dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. ๐Ÿ“˜

  • python-codicefiscale - encode/decode Italian fiscal codes - codifica/decodifica del Codice Fiscale. ๐Ÿ‡ฎ๐Ÿ‡น ๐Ÿ’ณ

  • python-fontbro - friendly font operations. ๐Ÿงข

  • python-fsutil - file-system utilities for lazy devs. ๐ŸงŸโ€โ™‚๏ธ

More Repositories

1

FCUUID

๐Ÿ“ฑ ๐Ÿ†” iOS UUID / Universally Unique Identifiers library as alternative to UDID and identifierForVendor.
Objective-C
1,574
star
2

django-admin-interface

๐Ÿฆธ โšก django's default admin interface with superpowers - customizable themes, popup windows replaced by modals and many other features.
CSS
1,546
star
3

python-benedict

๐Ÿ“˜ dict subclass with keylist/keypath support, built-in I/O operations (base64, csv, ini, json, pickle, plist, query-string, toml, xls, xml, yaml), s3 support and many utilities.
Python
1,111
star
4

FCFileManager

๐Ÿ“ฑ ๐Ÿ“‚ iOS file manager on top of NSFileManager for simplifying files management.
Objective-C
899
star
5

django-colorfield

๐ŸŽจ color field for django models with a nice color-picker in the admin.
Python
519
star
6

django-treenode

๐ŸŒณ probably the best abstract model/admin for your tree based stuff.
Python
481
star
7

django-maintenance-mode

๐Ÿšง ๐Ÿ› ๏ธ shows a 503 error page when maintenance-mode is on.
Python
408
star
8

FCCurrentLocationGeocoder

๐Ÿ“ฑ ๐Ÿ“ iOS geocoder for forward / reverse geocode user's current location using a block-based syntax.
Objective-C
262
star
9

python-fsutil

๐Ÿ’ป ๐Ÿ”ง high-level file-system operations for lazy devs.
Python
147
star
10

FCIPAddressGeocoder

๐Ÿ“ฑ ๐ŸŒ iOS geocoder for geocode device IP Address location using GeoIP service(s) and a block-based syntax.
Objective-C
114
star
11

django-freeze

๐ŸงŠ convert your dynamic django site to a static one with one line of code.
Python
89
star
12

python-codicefiscale

๐Ÿ‡ฎ๐Ÿ‡น ๐Ÿ’ณ italian fiscal codes encoding, decoding and validation - codifica, decodifica e validazione del Codice Fiscale italiano.
Python
62
star
13

django-redirects

โ†ช๏ธ โœ… redirects as they should be, with full control.
Python
55
star
14

python-fontbro

๐Ÿงข friendly font operations on top of fontTools.
Python
41
star
15

utils.js

๐Ÿ‘ท ๐Ÿ”ง zero dependencies vanilla JavaScript utils.
JavaScript
18
star
16

create-matrix-action

๐Ÿงช ๐Ÿ’ฅ GitHub action that creates a non-square matrix parsing a readable config.
Python
9
star
17

layers.css

๐Ÿ“‘ pure (s)css independent stacked layers, no js at all.
CSS
8
star
18

wall.css

๐Ÿงฑ pure (s)css block-grid implementation with extra features.
SCSS
8
star
19

FCMapsApp

๐Ÿ“ฑ ๐Ÿ—บ๏ธ iOS utility for launching external maps applications (Apple Maps, Google Maps, Waze, Yandex Maps), showing locations and getting directions.
Objective-C
7
star
20

FCUtils

๐Ÿ“ฑ ๐Ÿ”ง iOS utilities collection.
Objective-C
6
star
21

python-imageutil

๐ŸŽฉ ๐Ÿช„ high-level image operations, with a bit of magic. ๐Ÿฐ
Python
5
star
22

django-cache-cleaner

๐Ÿงนโœจ clear the entire cache or individual caches easily using the admin panel or management command.
Python
2
star
23

xhrm.js

JavaScript XMLHttpRequest manager built on top of jQuery $.ajax.
JavaScript
2
star
24

tabbo.js

๐Ÿซ โŒจ๏ธ enhanced keyboard tabbing usability on any website / webapp with one line of code.
JavaScript
1
star