from django.urls import reverse from django.conf import settings from djadhere.utils import send_notification 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 send_notification(sujet, message, settings.ALLOCATIONS_EMAILS, cc=[benevole])