Implement template files, create attribute

This commit is contained in:
Kumi 2022-01-09 09:38:03 +01:00
parent bd0449632b
commit 21e4b195ea
3 changed files with 40 additions and 12 deletions

View file

@ -8,12 +8,16 @@ and storing it outside of your project's settings.py
1. In your project's settings.py, import the app like so:
```from autosecretkey import AutoSecretKey```
```python
from autosecretkey import AutoSecretKey
```
2. Still in the settings.py file, replace the existing SECRET_KEY line with
something like this:
```SECRET_KEY = AutoSecretKey(BASE_DIR / "config.ini").secret_key```
```python
SECRET_KEY = AutoSecretKey(BASE_DIR / "config.ini").secret_key
```
(N.B.: Of course, this line has to be after the BASE_DIR line.)
@ -30,14 +34,16 @@ For additional security, you may want to store your secret key in a different
location than your project's base directory. You could, for example, do
something like this:
```AutoSecretKey("/etc/your_project/configuration")```
```python
AutoSecretKey("/etc/your_project/config.ini")
```
You need to manually make sure that the user your Django project runs as has
the permission to read and write this file. Running something like this as
root should do the trick in Linux (replacing "djangouser" with the actual user
name):
```
```bash
mkdir /etc/your_project/
touch /etc/your_project/configuration
chown djangouser /etc/your_project/configuration
@ -50,7 +56,7 @@ AutoSecretKey object.
This is a simple example you could have in your `settings.py`:
```
```python
from autosecretkey import AutoSecretKey
my_config_file = AutoSecretKey(BASE_DIR / "config.ini")
SECRET_KEY = my_config_file.secret_key
@ -59,7 +65,7 @@ TIME_ZONE = my_config_file.config["MY_SETTINGS"]["TIME_ZONE"]
For reference, the corresponding `config.ini` might look like this:
```
```ini
[AutoSecretKey]
SecretKey = WellThisIsWhereYouWillFindYourSecretKey
@ -67,6 +73,24 @@ SecretKey = WellThisIsWhereYouWillFindYourSecretKey
TIME_ZONE = UTC
```
You can pass the path to an .ini file to use as a template when first creating
the file creating your secret key. This file may contain any additional
settings you want to have in your config file, the SecretKey will then be added
to that. Note that you must not define a secret key within that template file.
```python
AutoSecretKey("/etc/myproject/config.ini", template=BASE_DIR/"config.dist.ini")
```
You can also set the `create` attribute to `False` if you need to make sure
the config file already exists - you may want to use this to make sure that
custom settings have already been made. If the file exists but no secret key
is defined within it, a new secret key will be added to the file.
```python
AutoSecretKey("config.ini", create=False)
```
All methods you can use on any other ConfigParser object can be used on that
object as well, of course, like get(), getboolean(), etc. For convenience, you
can use the AutoSecretKey object's update() method to re-read the contents of

View file

@ -9,7 +9,7 @@ class AutoSecretKey:
config.write(outfile)
@classmethod
def read_config_file(cls, path, create=True):
def read_config_file(cls, path, template=None, create=False):
config = configparser.ConfigParser(interpolation=None)
try:
@ -18,6 +18,9 @@ class AutoSecretKey:
except FileNotFoundError:
if not create:
raise
if template:
config.read(template)
cls.write_config_file(path, config)
@ -26,8 +29,8 @@ class AutoSecretKey:
def write(self):
self.__class__.write_config_file(self.path, self.config)
def update(self):
self.config = self.__class__.read_config_file(self.path)
def update(self, template=None, create=False):
self.config = self.__class__.read_config_file(self.path, template, create)
@property
def secret_key(self):
@ -41,8 +44,8 @@ class AutoSecretKey:
return new_key
def __init__(self, path, section="AutoSecretKey", config_key="SecretKey"):
def __init__(self, path, section="AutoSecretKey", config_key="SecretKey", template=None, create=True):
self.path = path
self.section = section
self.config_key = config_key
self.update()
self.update(template, create)

View file

@ -1,6 +1,6 @@
[metadata]
name = django-autosecretkey
version = 0.9
version = 0.9.1
description = A simple Django app to store secret keys outside of settings.py
long_description = file: README.md
long_description_content_type = text/markdown
@ -22,6 +22,7 @@ classifiers =
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Topic :: Internet :: WWW/HTTP
Topic :: Internet :: WWW/HTTP :: Dynamic Content