tags:

views:

142

answers:

4
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 <[email protected]>", "email_use_tls": "False", "email_port": "25","email_host_user": "[email protected]", "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 for they own configuration such as Mail.

everyone here Could give the good choice for do this?

+2  A: 

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.

James Polley
Not for every user but it's for the whole systems.
python
1-I build form 2-from user input I write to local_setting.py as dict.3-in setting I make the settings mail params dynamic changed from user.
python
Oh - you're looking for something like a way to have the settings for the whole site configurable in the Admin interface, rather than in settings.py, so that you don't have to edit settings.py each time you start setting up a new site?
James Polley
yes of cause.Your understand is what I want.
python
A: 

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.

Gattster
Did you guide me to make it works with your snippet.
python
+1  A: 

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()
T. Stone
/settings.py", line 10, in __getattr__ return getattr(defaults, key)AttributeError: 'NoneType' object has no attribute '__file__'
python
+3  A: 

I like django-dbsettings for dealing with settings that are configurable are runtime.

Daniel Roseman
Will this allow you to change variables in `settings` where you get it as `from django.conf import settings`? I think that would be a requirement since he needs to configure the main settings.py email configuration variables.
Gattster
I think a combination of django-dbsettings (to handle the admin/DB storage), and some type of a hack to get this data dynamically into the main django settings(like my `_DynamicMailSettings` answer) would do the trick.
Gattster
I did something similar. The only downside I found was that the site owner would try to get clever ... and then get terribly confused about which settings were in settings.py and which they accessed via admin. I mostly solved this by telling them that if they edited `settings.py` and broke the production site my rates would be double to fix it -- they got the message and stuck to the admin interface.
Peter Rowell