App-Parameter is a very simple Django app to save application's parameter in the database.
Project description
Django-app-parameter
App-Parameter is a very simple Django app to save some application's parameters in the database. Those parameters can be updated by users at run time (no need to new deployment or any restart). It can be used to store the website's title or default e-mail expeditor...
Install
pip install django-app-parameter
Settings
- Add "django_app_parameter" to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [
...
"django_app_parameter",
]
If you want global parameters to be available in templates, set provided context processor:
TEMPLATES = [
...
"OPTIONS": {
"context_processors": [
...
"django_app_parameter.context_processors.add_global_parameter_context",
],
},
]
-
Run
python manage.py migrate
to create the django_app_parameter's table. -
Start development server and visit http://127.0.0.1:8000/admin/ to create parameters (you'll need the Admin app enabled).
Usage
Add new parameters
Use admin interface to add parameters. You can access a parameter in your code use the "slug" field. Slug is built at first save with: slugify(self.name).upper().replace("-", "_")
.
Examples:
self.name ==> self.slug
blog title ==> BLOG_TITLE
sender e-mail ==> SENDER_E_MAIL
##weird@Na_me ==> WERIDNA_ME
See Django's slugify function for more informations.
Access parameter in python code
You can read parameter anywhere in your code:
from django.views.generic import TemplateView
from django_app_parameter import app_parameter
class RandomView(TemplateView):
def get_context_data(self, **kwargs):
kwargs.update({"blog_title": app_parameter.BLOG_TITLE})
return super().get_context_data(**kwargs)
In case you try to read a non existent parameter, an ImproperlyConfigured exception is raised.
Access parameter in templates
You can also access "global" parameters from every templates:
<head>
<title>{{ BLOG_TITLE }}</title>
</head>
A to make a parameter global, you only need to check is_global in admin.
Bulk load parameter with management command
A management command is provided to let you easily load new parameters: load_param
.
It will create or update, the key for matching is the SLUG.
It accepts 3 parameters: file, json and no-update.
Option --file
Add all parameters listed in the provided file.
load_param --file /path/to/json.file
Example of file content:
[
{"name": "hello ze world", "value": "yes", "description": "123", "is_global": true},
{"slug": "A8B8C", "name": "back on test", "value": "yes", "value_type": "INT" }
]
Here all available property you can add to the json:
- name
- slug
- value_type
- value
- description
- is_global
If slug is not provided it will be built. Default value_type is STR (string) and default is_global is False. Name is always required, others properties are optionnals.
Option --json
Add parameters in one shot.
load_param --json "[{'name': 'param1'}, {'name': 'param2'},]"
The provided json needs to match same rules as for --file option above.
You can't use --json and --file together.
Option --no-update
This option is provided to disable 'update' if parameter with same SLUG already exists. It can be used with --json and --file. It's useful to ensure all parameters are created in all environments and can be executed altogether with migrate. It avoid replacing already existing parameters' values which could lead to breaking environments.
load_param --no-update --file required_parameters.json
I use it in my starting container script:
#!/bin/bash
# Execute migrations
python manage.py migrate
# load new parameters if any
python manage.py load_param --no-update --file required_parameters.json
# launch webserver
gunicorn config.wsgi
Enjoy.
Ideas which could come later (or not)
- A migration process to keep a list of your parameters in a file and automatically add them in each environment
- Shortcut to use Parameter.str(slug) (skip 'objects' key word)
- Management command to add a new parameter
- [] Check correctness of value type on save
- [] modifications history
- Boolean type
- [] Datetime types
If you have new idea you would like to see, feel free to open a new issue in this repo.
Help developping
If you want to participate to the development, there are (only) 2 constraints:
- Format all your code with black
- All unit test must pass and new code must be covered
Because tests require a whole django environment, to run them I use https://github.com/Swannbm/runtest_on_dj_packages ; if you know a better way to do it I am all ears :D
Why Django-App-Parameter
Because I wanted to try packaging a Django app and I used this one in most of my projects so it seemed a good idea.
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
Built Distribution
File details
Details for the file django_app_parameter-1.1.3.tar.gz
.
File metadata
- Download URL: django_app_parameter-1.1.3.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.5 Linux/4.19.128-microsoft-standard
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d30e34f1db49b9b9f5a7ac09c7b39d7f8c77cc87a912831843b1465a142fac13 |
|
MD5 | 76e3f75f8d731ee87ec47892191d2956 |
|
BLAKE2b-256 | f281cb98e294dc2d3dfc8e69263ec7c0d919fa33a756ca03f1449abea4faa97c |
File details
Details for the file django_app_parameter-1.1.3-py3-none-any.whl
.
File metadata
- Download URL: django_app_parameter-1.1.3-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.5 Linux/4.19.128-microsoft-standard
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17367c92e8f664dd9c61de2ad1f3f25efd5082e8b5ea36a3983b32d36c671bf4 |
|
MD5 | 3f430b4a2b9377b85321098b23fadd1f |
|
BLAKE2b-256 | 3c4af5bca86030a58b0d08572436aae00359e1c2487500a45adf1a44e7bbc22a |