8

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', 'admin@gmail.com')], '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.

Community
  • 1
  • 1
Bazinga
  • 2,456
  • 33
  • 76
  • What do you mean by "sending of emails is not working"? Any errors? Thanks. – alecxe Mar 14 '16 at 20:42
  • 1
    Are you trying to change django settings dynamically? Have you looked at http://stackoverflow.com/questions/6528723/changing-django-settings-at-runtime? – Peter Brittain Mar 14 '16 at 21:38
  • @alecxe, I used Admin and Manager for sending error log in my email, when not using django-constance it is sending errors in the emails(Which is the right way) But when I used the way of django-constance, no errors but It doesn't send my emails. – Bazinga Mar 15 '16 at 05:50
  • @PeterBrittain Yes I tried a couple of them here as well https://www.djangopackages.com/grids/g/live-setting/, But cant seem to find a way to make the Admin and Manager work for them. – Bazinga Mar 15 '16 at 05:51
  • 1
    Did you also see the answer (http://stackoverflow.com/a/6528794/4994021) that says you must not change the settings? Yes there are some you can change (as noted in the comments to the answer), but it is not documented or supported... – Peter Brittain Mar 15 '16 at 23:03

1 Answers1

5

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': ('admin@gmail.com', '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
    },
Satyajeet
  • 2,004
  • 15
  • 22
  • What if you want to have multiple admins like: ( ('admin1', 'admin1@domain'), ('admin1', 'admin2@domain.tld'), ) How will you be able to use it in constance as well as the function you implement? – Bazinga Mar 21 '16 at 07:24
  • Please read the Answer carefully. Add ADMIN2 = (, ) in ```constance config```. And then ```[config.ADMIN1, config.ADMIN2]``` in ```send_mail``` function – Satyajeet Mar 21 '16 at 07:29