0
votes

I have an filemanager application and I want some specific admin users to be notified when "baseusers" create directories. Mails are sent to users defined in admin pages. The optimal solution would be to have a checkbox in my profile pages for getting notifications but I would like to get this to work first. Here is my simple solution:

from django.core.mail import send_mail, BadHeaderError
from basedraft.sendmail.views import send_email
from django.core.signals import request_finished

@permission_required('fileman.can_fm_add')
def createDir(request, path=None):
    if path is None:
        return HttpResponse(_(u"Path does not set."))
    try:
        path = toString(path)
        os.mkdir(path)

    except Exception, msg:
        return raise_error(request, [str(msg)])
    createHistory(request.user, "createdir", toString(path))    # Added this to history, added createdir to models

    # Send mail to recipients when a directory is created
    send_email(toString(path))

    return HttpResponseRedirect('/fm/list/%s' % path)

Problem here is that the site hangs until the mail is sent. I tried using signals like so:


from django.core.mail import send_mail, BadHeaderError
from basedraft.sendmail.views import send_email
from django.core.signals import request_finished

@permission_required('fileman.can_fm_add')
def createDir(request, path=None):
    if path is None:
        return HttpResponse(_(u"Path does not set."))
    try:
        path = toString(path)
        os.mkdir(path)

    except Exception, msg:
        return raise_error(request, [str(msg)])
    createHistory(request.user, "createdir", toString(path))    # Added this to history, added createdir to models

    # Send mail to recipients when a directory is created
    #send_email(toString(path))
    request_finished.connect(mail_callback)
    return HttpResponseRedirect('/fm/list/%s' % path)


def mail_callback(sender, **kwargs):
    #path = setPath
    send_email('test')

But this just started sending alot of emails for every finished request. Any ideas how I should do this, if there is a way so that the site won't be hanging but the sending of email is done in the background?

1

1 Answers

2
votes

To send and process the mails in the background you should use a mail queue.

Several options are discussed in this thread: Advice on Python/Django and message queues

For your use case django-mailer seems like a good choice.