用于生成配置文件的django交互命令。
django-settings-custom的Python项目详细描述
django设置自定义
用于生成配置文件的django交互命令。
得到它
项目在pypi(https://pypi.org/project/django-settings-custom/)上
pip install django-settings-custom
安装它
要在项目中启用django_settings_custom
,需要将其添加到项目中的INSTALLED_APPS
settings.py
文件:
INSTALLED_APPS = (
...
'django_settings_custom',
...
)
使用它
为目标conf.ini创建一个模板,如
[DATABASE]NAME={ USER_VALUE }HOST={ USER_VALUE }PORT={ USER_VALUE }[DATABASE_CREDENTIALS]USER={ USER_VALUE }PASSWORD={ ENCRYPTED_USER_VALUE }[DJANGO]KEY={ DJANGO_SECRET_KEY }# A constant field[LDAP]URL='ldaps://myldap'
在django设置中配置
添加settings.py
文件
SETTINGS_TEMPLATE_FILE='PATH_TO_YOUR_TEMPLATE_CONFIGURATION_FILE'SETTINGS_FILE_PATH='TARGET_FOR_CONFIGURATION_FILE'
在命令行中启动
python manage.py generate_settings
或全部在命令行
python manage.py generate_settings path/to/template/settings.ini target/path/of/settings.ini
结果
命令要求用户填写模板中缺少的值:
[user@localhost a_project]$ ./manage.py generate_conf
** Configuration file generation: **
** Configuration file generation: **
Do you want to generate the secret key for Django ? (Y/n) : y
Django secret key generated
** Enter values for configuration file content **
Value for [DATABASE] NAME: database_name
Value for [DATABASE] HOST: database_host
Value for [DATABASE] PORT: 900
Value for [DATABASE_CREDENTIALS] USER: my_user
Value for [DATABASE_CREDENTIALS] PASSWORD (will be encrypted):
Writing file at /home/user/a_project/conf.ini:
Configuration file successfully generated.
[user@localhost a_project]$
它生成文件/home/user/a_project/conf.ini:
[DATABASE]NAME=database_nameHOST=database_hostPORT=900[DATABASE_CREDENTIALS]USER=my_userPASSWORD=JbAwLj5Zwz8lMrvcUZq5sP/v6eaUFY5E7U8Fmg63vxI=# A constant field[LDAP]URL='ldaps://monldap'[DJANGO]KEY=w)r13ne4=id9_8xdojir)3)%%5m3r$co#jwj_)4d*_%%!0+f#sro
要解密代码中的值(例如在settings.py中),可以使用django_settings_custom.encryption.decrypt
:
importconfigparserfromdjango_settings_customimportencryptionconfig=configparser.ConfigParser()config.read(SETTINGS_FILE_PATH)database_password=encryption.decrypt(config.get('DATABASE_CREDENTIALS','PASSWORD'))
要解密值,函数使用django secret_密钥(必须在之前设置)。
其他
如果不想使用django设置
如果不想将特定变量添加到django设置文件中,可以继承generate_settings.Command
来指定命令选项:
fromdjango_settings_custom.management.commandsimportgenerate_settingsclassCommand(generate_settings.Command):settings_template_file='The/settings/template/file_path.ini'settings_file_path='The/target/settings/file_path.ini'
添加自定义标记
要添加自定义标记,可以继承generate_settings.Command
,并重写方法get_value
:
importrandomfromdjango_settings_custom.management.commandsimportgenerate_settingsclassCommand(generate_settings.Command):@staticmethoddefget_value(section,key,value_type,secret_key):ifvalue_type=='RANDOM_VALUE':returnrandom.uniform(0,100)returnsuper(Command,Command).get_value(section,key,value_type,secret_key)
或者更复杂的例子:
fromdjango.core.management.baseimportCommandErrorfromdjango_settings_custom.management.commandsimportgenerate_settingsclassCommand(generate_settings.Command):@staticmethoddefget_value(section,key,value_type,secret_key):int_less_10=value_type=='INT_LESS_THAN_10'ifint_less_10:value_type='USER_VALUE'value=super(Command,Command).get_value(section,key,value_type,secret_key)ifint_less_10:try:value=int(value)ifvalue>=10:raiseCommandError('This field needs an int less than 10.')exceptValueError:raiseCommandError('This field needs an int.')returnvalue