Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic settings.py

Tags:

python

django

I have used django-constance as a library. Although one thing I notice is that when I tried using ADMIN and MANAGER

CONSTANCE_CONFIG = {
'ADMINS': ([('Errors', '[email protected]')], 'Admin Emails'),
}

sending of emails is not working.

In MANAGER I have tried this:

MANAGER = CONSTANCE_CONFIG['ADMINS'][0]

still sending emails is not working. Am I missing a wrong implementation? Or can you suggest any other library which can override ADMIN and MANAGER in settings.py. I am using Django 1.8.5 and Python 3.

also when trying to import inside settings.py it produces error as well.

like image 275
Bazinga Avatar asked Mar 09 '16 16:03

Bazinga


1 Answers

1# Probably you already know, django-constance does not support tuple. Basically it is really hard to detect a widget for tuple specially in your case. ADMINS can be added/deleted so how possibly you can make it dynamic through a single widget..!!(think about all django widgets). So here CONSTANCE_ADDITIONAL_FIELDS will also not work.

2# I think you are misunderstanding the working of django constance. It does not refresh your django server. So MANAGER = CONSTANCE_CONFIG['ADMINS'][0] is totally wrong(even using CONSTANCE_ADDITIONAL_FIELDS). You accessing constant value here(not dynamic). You need to access it like

from constance import config
print(config.ADMINS)

3# Default logging config uses AdminEmailHandler class for mail_admins, which uses ADMINS value from django settings, not constance config.

So one possible solution might be to create your own handler class which will use ADMINS value from constance config. So change your setting.py to

CONSTANCE_CONFIG = {
    'ADMIN1': ('[email protected]', 'This one will receive error on 500'),
} # you can add as many admins as you want with ADMIN1, ADMIN2 etc(no tuple) 

then create your own handler class which will use CONSTANCE_CONFIG.

from django.utils.log import AdminEmailHandler
from constance import config
from django.conf import settings
from django.core.mail.message import EmailMultiAlternatives


class ConstanceEmailHandler(AdminEmailHandler):
    def send_mail(self, subject, message, html_message=None, fail_silently=False, *args, **kwargs):
        # create a list of ADMIN emails here, if you have more then one ADMIN
        mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
                                  message, settings.SERVER_EMAIL, [config.ADMIN1],
                                  connection=self.connection())
        if html_message:
            mail.attach_alternative(html_message, 'text/html')
        mail.send(fail_silently=fail_silently)

And then change your LOGGER config. I would recommend you to copy default logger config from django.utils.log(DEFAULT_LOGGING) if you do not have your custom LOGGING setup. And change mail_admins to

'mail_admins': {
    'level': 'ERROR',
    'filters': ['require_debug_false'], # change it to require_debug_true if you want to test it locally.
    'class': '<yourproject>.<yourfile>.ConstanceEmailHandler', # path to newly created handler class
    'include_html': True
    },
like image 103
Satyajeet Avatar answered Oct 19 '22 23:10

Satyajeet