def send_rsvp_notification(rsvp_pk): try: rsvp = RSVP.objects.select_related('person', 'event').get(pk=rsvp_pk) except RSVP.DoesNotExist: # RSVP does not exist any more?! return person_information = str(rsvp.person) recipients = [ organizer_config.person for organizer_config in rsvp.event.organizer_configs.filter( notifications_enabled=True) if organizer_config.person != rsvp.person ] attendee_bindings = { "EVENT_NAME": rsvp.event.name, "EVENT_SCHEDULE": rsvp.event.get_display_date(), "CONTACT_NAME": rsvp.event.contact_name, "CONTACT_EMAIL": rsvp.event.contact_email, "LOCATION_NAME": rsvp.event.location_name, "LOCATION_ADDRESS": rsvp.event.short_address, "EVENT_LINK": front_url("view_event", args=[rsvp.event.pk]) } send_mosaico_email( code='EVENT_RSVP_CONFIRMATION', subject=_("Confirmation de votre participation à l'événement"), from_email=settings.EMAIL_FROM, recipients=[rsvp.person], bindings=attendee_bindings) organizer_bindings = { "EVENT_NAME": rsvp.event.name, "PERSON_INFORMATION": person_information, "MANAGE_EVENT_LINK": front_url("manage_event", kwargs={"pk": rsvp.event.pk}) } send_mosaico_email( code='EVENT_RSVP_NOTIFICATION', subject=_("Un nouveau participant à l'un de vos événements"), from_email=settings.EMAIL_FROM, recipients=recipients, bindings=organizer_bindings)
def link(self, object): if object.slug: return format_html( '<a href="{0}">{0}</a>', front_url('view_calendar', kwargs={'slug': object.slug})) else: return '-'
def link(self, object): if object.pk: return format_html( '<a href="{0}">{0}</a>', front_url('view_event', kwargs={'pk': object.pk})) else: return '-'
def send_someone_joined_notification(membership_pk): try: membership = Membership.objects.select_related( 'person', 'supportgroup').get(pk=membership_pk) except Membership.DoesNotExist: return person_information = str(membership.person) managers_filter = (Q(is_referent=True) | Q(is_manager=True)) & Q(notifications_enabled=True) managing_membership = membership.supportgroup.memberships.filter( managers_filter).select_related('person').prefetch_related( 'person__emails') recipients = [membership.person for membership in managing_membership] bindings = { "GROUP_NAME": membership.supportgroup.name, "PERSON_INFORMATION": person_information, "MANAGE_GROUP_LINK": front_url("manage_group", kwargs={"pk": membership.supportgroup.pk}) } send_mosaico_email( code='GROUP_SOMEONE_JOINED_NOTIFICATION', subject=_("Un nouveau membre dans votre groupe d'action"), from_email=settings.EMAIL_FROM, recipients=recipients, bindings=bindings)
def link(self, object): if object.pk: return format_html( '<a href="{0}">{0}</a>', front_url('view_group', kwargs={'pk': object.pk})) else: return mark_safe('-')
def send_support_group_changed_notification(support_group_pk, changes): try: group = SupportGroup.objects.get(pk=support_group_pk, published=True) except SupportGroup.DoesNotExist: return change_descriptions = [ desc for label, desc in CHANGE_DESCRIPTION.items() if label in changes ] change_fragment = render_to_string(template_name='lib/list_fragment.html', context={'items': change_descriptions}) # TODO: find adequate way to set up domain names to use for these links bindings = { "GROUP_NAME": group.name, "GROUP_CHANGES": change_fragment, "GROUP_LINK": front_url("view_group", kwargs={'pk': support_group_pk}) } notifications_enabled = Q(notifications_enabled=True) & Q( person__group_notifications=True) recipients = [ membership.person for membership in group.memberships.filter( notifications_enabled).prefetch_related('person__emails') ] send_mosaico_email( code='GROUP_CHANGED', subject=_( "Les informations de votre groupe d'action ont été changées"), from_email=settings.EMAIL_FROM, recipients=recipients, bindings=bindings, )
def send_welcome_mail(person_pk): person = Person.objects.prefetch_related('emails').get(pk=person_pk) send_mosaico_email( code='WELCOME_MESSAGE', subject=_("Bienvenue sur la plateforme de la France insoumise"), from_email=settings.EMAIL_FROM, bindings={'PROFILE_LINK': front_url('change_profile')}, recipients=[person])
def send_event_creation_notification(organizer_config_pk): try: organizer_config = OrganizerConfig.objects.select_related( 'event', 'person').get(pk=organizer_config_pk) except OrganizerConfig.DoesNotExist: return event = organizer_config.event organizer = organizer_config.person bindings = { "EVENT_NAME": event.name, "EVENT_SCHEDULE": event.get_display_date(), "CONTACT_NAME": event.contact_name, "CONTACT_EMAIL": event.contact_email, "CONTACT_PHONE": event.contact_phone, "CONTACT_PHONE_VISIBILITY": _("caché") if event.contact_hide_phone else _("public"), "LOCATION_NAME": event.location_name, "LOCATION_ADDRESS": event.short_address, "EVENT_LINK": front_url("view_event", kwargs={'pk': event.pk}), "MANAGE_EVENT_LINK": front_url('manage_event', kwargs={'pk': event.pk}), } send_mosaico_email( code='EVENT_CREATION', subject=_("Les informations de votre nouvel événement"), from_email=settings.EMAIL_FROM, recipients=[organizer], bindings=bindings, )
def send_support_group_creation_notification(membership_pk): try: membership = Membership.objects.select_related( 'supportgroup', 'person').get(pk=membership_pk) except Membership.DoesNotExist: return group = membership.supportgroup referent = membership.person bindings = { "GROUP_NAME": group.name, "CONTACT_NAME": group.contact_name, "CONTACT_EMAIL": group.contact_email, "CONTACT_PHONE": group.contact_phone, "CONTACT_PHONE_VISIBILITY": _("caché") if group.contact_hide_phone else _("public"), "LOCATION_NAME": group.location_name, "LOCATION_ADDRESS": group.short_address, "GROUP_LINK": front_url("view_group", kwargs={'pk': group.pk}), "MANAGE_GROUP_LINK": front_url('manage_group', kwargs={'pk': group.pk}), } send_mosaico_email( code='GROUP_CREATION', subject=_("Les informations de votre nouveau groupe d'action"), from_email=settings.EMAIL_FROM, recipients=[referent], bindings=bindings, )
def send_mosaico_email(code, subject, from_email, recipients, bindings=None, connection=None, backend=None, fail_silently=False, preferences_link=True): """Send an email from a Mosaico template :param code: the code identifying the Mosaico template :param subject: the subject line of the email :param from_email: the address from which the email is to be sent :param recipients: a list of recipients to which the email will be send; alternatively, a single address :param bindings: a dictionary of replacements variables and their target values in the Mosaico template :param connection: an optional email server connection to use to send the emails :param backend: if no connection is given, an optional mail backend to use to send the emails :param fail_silently: whether any error should be raised, or just be ignored; by default it will raise :param gen_connection_params_function: a function that takes a recipient and generates connection params """ if isinstance(recipients, Person): recipients = [recipients] if bindings is None: bindings = {} if connection is None: connection = get_connection(backend, fail_silently) if preferences_link: bindings['PREFERENCES_LINK'] = front_url('message_preferences') link_bindings = {key: value for key, value in bindings.items() if is_front_url(value)} template = loader.get_template(f'mail_templates/{code}.html') for recipient in recipients: if link_bindings: connection_params = generate_token_params(recipient) for key, value in link_bindings.items(): bindings[key] = add_params_to_urls(value, connection_params) context = get_context_from_bindings(code, recipient, bindings) html_message = template.render(context=context) text_message = generate_plain_text(html_message) email = EmailMultiAlternatives( subject=subject, body=text_message, from_email=from_email, to=[recipient.email], connection=connection ) email.attach_alternative(html_message, "text/html") email.send(fail_silently=fail_silently)
def send_unsubscribe_email(person_pk): person = Person.objects.prefetch_related('emails').get(pk=person_pk) bindings = { "MANAGE_SUBSCRIPTIONS_LINK": front_url('message_preferences'), } send_mosaico_email( code='UNSUBSCRIBE_CONFIRMATION', subject=_( 'Vous avez été désabonné⋅e des emails de la France insoumise'), from_email=settings.EMAIL_FROM, recipients=[person], bindings=bindings)
def send_event_changed_notification(event_pk, changes): try: event = Event.objects.get(pk=event_pk) except Event.DoesNotExist: # event does not exist anymore ?! nothing to do return change_descriptions = [ desc for label, desc in CHANGE_DESCRIPTION.items() if label in changes ] change_fragment = render_to_string(template_name='lib/list_fragment.html', context={'items': change_descriptions}) notifications_enabled = Q(notifications_enabled=True) & Q( person__event_notifications=True) recipients = [ rsvp.person for rsvp in event.rsvps.filter( notifications_enabled).prefetch_related('person__emails') ] bindings = { "EVENT_NAME": event.name, "EVENT_CHANGES": change_fragment, "EVENT_LINK": front_url("view_event", kwargs={'pk': event_pk}), "EVENT_QUIT_LINK": front_url("quit_event", kwargs={'pk': event_pk}) } send_mosaico_email( code='EVENT_CHANGED', subject= _("Les informations d'un événement auquel vous assistez ont été changées" ), from_email=settings.EMAIL_FROM, recipients=recipients, bindings=bindings, )
def get_path(self, obj): return front_url('view_event', absolute=False, args=[obj.id])
def link(self, object): if object.pk: return format_html('<a href="{url}">{text}</a>', url=front_url('participate_poll', args=[object.pk]), text=_("Voir la consultation"))