Make settings.py customizable from user input?
def save_local_setting_JSON(dest, content):
fwrite = open(dest, 'wb')
dict_json = eval(json.dumps(content))
string_json =json.dumps(dict(dict_json))
fwrite.write('EMAIL_DEVELOPMENT='+string_json+"\n")
fwrite.close()
def config_mail_show(request, template='admin/config_mail.html'):
form = forms.ConfigMailForm()# for for user input their mail config
context = {
'site_host' : settings.SITE_HOST,
'default_from_email' : settings.DEFAULT_FROM_EMAIL,
'email_host' : settings.EMAIL_HOST,
'email_port' : settings.EMAIL_PORT,
'email_host_user' : settings.EMAIL_HOST_USER,
'email_host_password' : settings.EMAIL_HOST_PASSWORD,
'email_use_tls' : settings.EMAIL_USE_TLS,
'form':form
}
if request.method == "POST":
form = forms.ConfigMailForm(request.method)
save_local_setting_JSON('local_setting.py', request.POST)
return render_to_response(template, context)
local_setting.py will be in this format:
EMAIL_DEVELOPMENT={"email_host_password": "xxxxxx", "site_host": "127.0.0.1:8000", "default_from_email": "xxx xxx <xxx@xxx.xxx>", "email_use_tls": "False", "email_port": "25","email_host_user": "xxx@xxx.xxx", "email_host": "smtp.xxx.xx"}
In settings.py I Added
from local_setting import *
if EMAIL_DEVELOPMENT.has_key('save'):
SITE_HOST = EMAIL_DEVELOPMENT['site_host']
DEFAULT_FROM_EMAIL = EMAIL_DEVELOPMENT['default_from_email']
EMAIL_HOST = EMAIL_DEVELOPMENT['email_host']
EMAIL_PORT =EMAIL_DEVELOPMENT['email_port']
EMAIL_HOST_USER = EMAIL_DEVELOPMENT['email_host_user']
EMAIL_HOST_PASSWORD = EMAIL_DEVELOPMENT['email_host_password']
EMAIL_USE_TLS = EMAIL_DEVELOPMENT['email_use_tls']
else:# My Development
SITE_HOST = '127.0.0.1:8000'
DEFAULT_FROM_EMAIL = 'xxxxx'
EMAIL_HOST = 'xxxxx'
EMAIL_PORT = 25
EMAIL_HOST_USER ='xxxx'
EMAIL_HOST_PASSWORD = 'xxxxx'
EMAIL_USE_TLS = False
I Am not sure it's good ways to make User Interface that provide user fo开发者_Go百科r they own configuration such as Mail.
everyone here Could give the good choice for do this?
I like django-dbsettings for dealing with settings that are configurable are runtime.
settings.py is for site-wide settings. If you want each user to have their own settings, create a Model called user_settings (with an FK to the Users model of course) and use it to store the per-user settings.
Perhaps something like the following would work:
class _DynamicMailSettings(object):
@property
def get_mail_host(self):
result = 'smtp.mydomain.com' #replace with DB query
return result
_dynamic_mail_settings = _DynamicMailSettings()
EMAIL_HOST = _dynamic_mail_settings.get_mail_host
I haven't tested it in a django settings.py but it works in a standalone python file.
Whichever route you go, it will end up being hacky -- I guarantee.
That said, I think you're on the right track. Storing the settings in the database seems like a bad idea because the database connection settings are found in the settings.py file itself which could lead to some really bad problems.
Your idea for using JSON and storing the data as a disk file is probably the best method because how else would it be stored if not in the database?
My only reaction to the way you were attempting to achieve this was that it seems like it would be really lame to hand-code all of the lines like this EMAIL_HOST = EMAIL_DEVELOPMENT['email_host']
. It would be easier to just check if that value exists in the local settings JSON and return that automatically.
Here's some hacky and untested concept code. Copy settings.py to settings_default.py and this becomes the new settings.py:
settings.py
from local_settings import EMAIL_DEVELOPMENT
import settings_default as defaults
import sys
class DynamicSettings(object):
def __getattr__(self, key):
if hasattr(EMAIL_DEVELOPMENT, key):
return getattr(EMAIL_DEVELOPMENT, key.lower())
else:
return getattr(defaults, key)
sys.modules[__name__] = DynamicSettings()
精彩评论