Skip to main content

Python 3 port of django-livesettings

Project description

django-livesettings3

Tests PyPI version codecov

This is a Python 3 Port of django-livesettings that has been tested with Python 3.8-3.13 and Django 3.2-5.2.

django-livesettings3 provides the ability to configure settings via an admin interface, rather than by editing settings.py. In addition, livesettings allows you to set sane defaults so that your site can be perfectly functional without any changes. Livesettings uses caching to make sure this has minimal impact on your site’s performance. Finally, if you wish to lock down your site and disable the settings, you can export your livesettings and store them in your settings.py. This allows you have flexibility in deciding how various users interact with your app.

Requirements

Install

$ pip install git+https://github.com/kunaldeo/django-livesettings3

Quickstart

Adding livesettings to your project

Add livesettings to settings.py

INSTALLED_APPS = (
  'django.contrib.admin',
  'livesettings',
  'myapp'
)

It is high recommended to configure a global cache (like Redis) in production for multiprocess servers or you will see the outdated data.

Add livesettings.urls to urlpatterns in urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^settings/', include('livesettings.urls')),
]

Checkout the Sample screenshot of livesetting editor

Create config.py

Create config.py within your app directory where your models.py is located. We will use config.py to register the settings value.

config.py

from django.utils.translation import gettext_lazy as _
from livesettings.functions import config_register
from livesettings.values import ConfigurationGroup, PositiveIntegerValue, MultipleStringValue


# Config group to hold all the configs
MYAPP_GROUP = ConfigurationGroup('MyApp', _('My App Settings'), ordering=0)

# When ordering parameter is not passed, all inputs are sorted by name
config_register(PositiveIntegerValue(
    MYAPP_GROUP,
    'NUM_IMAGES',
    description=_('Number of images to display'),
    help_text=_("How many images to display on front page."),
    # if no help_text is given, Default falue is displayed
    default=5
))

# Another example of allowing the user to select from several values
config_register(MultipleStringValue(
    MYAPP_GROUP,
    'MEASUREMENT_SYSTEM',
    description=_("Measurement System"),
    help_text=_("Default measurement system to use."),
    choices=[('metric', _('Metric')),
             ('imperial', _('Imperial'))],
    default="imperial"
))

Activate config.py

To activate the config.py you need to import config from models.py file.

from * import config

Linter Warning: Becareful of PyCharm's Optimize Imports, it may remove this line as it is never called directly!

Accessing Values in view

You can use config_value method to read the values store in livesttings

from django.shortcuts import render
from livesettings.functions import config_value


def index(request):
    image_count = config_value('MyApp', 'NUM_IMAGES')
    # Note, the measurement_system will return a list of selected values
    # in this case, we use the first one
    measurement_system = config_value('MyApp', 'MEASUREMENT_SYSTEM')
    return render(request, 'myapp/index.html',
                  {'image_count': image_count,
                   'measurement_system': measurement_system[0]})

Security and Permissions

In order to give non-superusers access to the /settings/ views, open Django Admin Auth screen and give the user or to its group the permission livesettings|setting|Can change settting.

Checkout the permissions screenshot

The same permission is needed to view the form and submit. Permissions for insert or delete and any permissions for “long setting” are ignored.

Superusers will have access to this setting without enabling any specific permissions.

If you want to save a sensitive information to livesettings on production site (e.g. a password for logging into other web service) it is recommended not to grant permissions to livesettings to users which are logging in everyday. The most secure method is to export the settings and disable web access to livesettings as described below. Exporting settings itself is allowed only by the superuser.

Because of the security significance of livesettings, all views in livesettings support CSRF regardless of whether or not the CsrfViewMiddleware is enabled or disabled.

Exporting Settings

Settings can be exported by the http://127.0.0.1:8000/settings/export/ . After exporting the file, the entire output can be manually copied and pasted to settings.py.

Supported Data Types

Following Data Types are supported:

  • Boolean
  • Decimal
  • Duration
  • Float
  • Integer
  • Positive Integer (non negative)
  • String
  • Long string
  • Multiple strings
  • Long multiple strings
  • Module values
  • Password

List is not implemented. But it is easy to use with comma separated string. When using the value you can simply use csvstring.split(',')

Fork of https://bitbucket.org/bkroeze/django-livesettings.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_livesettings3-1.7.0.tar.gz (52.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_livesettings3-1.7.0-py3-none-any.whl (72.5 kB view details)

Uploaded Python 3

File details

Details for the file django_livesettings3-1.7.0.tar.gz.

File metadata

  • Download URL: django_livesettings3-1.7.0.tar.gz
  • Upload date:
  • Size: 52.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for django_livesettings3-1.7.0.tar.gz
Algorithm Hash digest
SHA256 24ab9b23852559639aeb51efe0721c7e8454d039a87576b989bf8438fcc7c4c7
MD5 ce4dda8e2f59094cffc9fc3728e17049
BLAKE2b-256 efbf46f0f384c4888fa039552e2fa244ca957a622813474d27e2db06da24bb2c

See more details on using hashes here.

File details

Details for the file django_livesettings3-1.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_livesettings3-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0e28dd309f65d3e7234990af9808d0149451957e4e155ee4d19770548d65939
MD5 34587a30445a0b715834c6ce90bd96fd
BLAKE2b-256 7c1e27228443ece1d3a64a483f0c75655851e194337753943899a40cd53d5c11

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page