Skip to end of metadata
Go to start of metadata

Configuration

Booktype is a typical Django application and uses standard way to set default configuration. This document describes two additional methods that are used for configuration.

This is the workflow if new configuration API is used for accessing variables:

  • First check the dynamic configuration
  • Then look in the settings.py file
  • Still can't find it? Then look at the constants.
  • If it is not defined anywhere then just return predefined value provided with API call (default it is None)

Not every access to variables defined in settings should use this new API.

Because of this new API we will not have to handle configuration variables like this:

try:
    OBJAVI_URL = settings.OBJAVI_URL
except AttributeError:
    OBJAVI_URL = constants.OBJAVI_URL

but rather like this:

  OBJAVI_URL = config.getConfiguration("OBJAVI_URL")

Constants

Constants are default values provided with Booktype source. Thy are defined inside of file booki/constants.py. Constants help us provide predefined values for variables but also help us during the upgrade process. If user does not upgrade settings.py file with new configuration options Booktype will still be aware of the new configuration options because they will be defined as constants.

Settings.py

This is a default configuration file and is placed inside of Booktype project. Dynamic configuration is loaded from this file. If it is not loaded (for instance, user did not upgrade his settings.py with provided function calls) API will still work for accessing the configuration (but it will not be able to work with dynamic configuration).

Dynamic configuration

Content of dynamic configuration is saved as JSON encoded file "configuration.json" in Booktype project directory. Dynamic configuration will help us configure Booktype from external scripts of from Booktype Control Center (web interface for administrators). Content of variable can be any value that can be serialized as JSON string.

API

List of functions:

  • loadConfiguration()
  • getConfiguration(name, default_value)
  • setConfiguration(name, value)
  • saveConfiguration()

When using "getConfiguration" function it will look inside of "dynamic configuration", "constants values" and settings.py but when using "setConfiguration" it will only change values in "dynamic configuration". We can have easily the same configuration variables in "dynamic configuration" and settings.py but content of "dynamic configuration" will always have higher priority.

Part of the settings.py file:

 
 from booki.utils import config

 try:
    BOOKTYPE_CONFIG = config.loadConfiguration()
 except config.ConfigurationError:
    BOOKTYPE_CONFIG = None

Used in the code:

 from booki.utils import config

 name = config.getConfiguration('BOOKTYPE_SITE_NAME', None)
 
 if name:
    config.setConfiguration('BOOKTYPE_SITE_NAME', 'My site')

 config.saveConfiguration()    

Command line tools

List all configuration values
  django-admin.py  conflist 
  django-admin.py  conflist --values
Set variable value
  django-admin.py  confset  BOOKTYPE_SITE_NAME "This is my site"
  django-admin.py  confset --append --as_json MY_LIST "[2, 3, 1]"
  django-admin.py  confset --remove --integer MY_LIST 2
  django-admin.py  confset --integer BOOKTYPE_NUMBER 21
  django-admin.py  confset --float BOOKTYPE_NUMBER 21.2
Get variable value
  django-admin.py  confget BOOKTYPE_NUMBER
  django-admin.py  confget --as_json BOOKTYPE_NUMBER
Delete variable
  django-admin.py  confdel BOOKTYPE_NUMBER
  • No labels