from django.core.mail.message import EmailMultiAlternatives from django.core.urlresolvers import reverse from django.conf import settings def mail_managers(subject, message, fail_silently=False, connection=None, html_message=None, **kwargs): """Send a message to the managers, as defined by the MANAGERS setting.""" if not settings.MANAGERS: return mail = EmailMultiAlternatives( '%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message, settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS], connection=connection, **kwargs ) if html_message: mail.attach_alternative(html_message, 'text/html') mail.send(fail_silently=fail_silently) def notify_allocation(request, new_alloc, old_alloc=None): fields = ['resource', 'service', 'route', 'start', 'end', 'notes'] benevole = '%s <%s>' % (request.user.username, request.user.email) message = 'Bénévole : ' + benevole message += '\n\nAllocation :' diff = False for field in fields: new_attr = getattr(new_alloc, field) if new_attr == '' or new_attr is None: new_attr = '-' if old_alloc: old_attr = getattr(old_alloc, field) if old_attr == '' or old_attr is None: old_attr = '-' if old_alloc and old_attr != new_attr: message += '\n-%12s: %s\n+%12s: %s' % (field, old_attr, field, new_attr) diff = True else: message += '\n %12s: %s' % (field, new_attr) url = 'https' if request.is_secure() else 'http' url += '://' + request.get_host() url += reverse('admin:services_ipresource_change', args=(new_alloc.resource.pk,)) message += '\n\nVoir : ' + url if old_alloc and diff: sujet = 'Modification d’une allocation' elif not old_alloc: sujet = 'Nouvelle allocation' else: sujet = None if sujet: sujet += ' ADT%d' % new_alloc.service.adhesion.pk mail_managers(sujet, message, cc=[benevole])