def send_contract_confirmation_email(self, payment_id): try: payment = Payment.objects.get(id=payment_id) except Payment.DoesNotExist: return None person = payment.person if "contract_path" not in payment.meta: raise RuntimeError("Contrat non généré pour le paiement {}".format( repr(payment))) full_name = f'{payment.meta["first_name"]} {payment.meta["last_name"]}' try: with open(Path(settings.MEDIA_ROOT) / payment.meta["contract_path"], mode="rb") as contract: send_mosaico_email( code="CONTRACT_CONFIRMATION", subject="Votre contrat de prêt", from_email=settings.EMAIL_FROM, bindings={ "CHER_PRETEUR": SUBSTITUTIONS["cher_preteur"][payment.meta["gender"]] }, recipients=[person], attachments=[{ "filename": f"contrat_pret_{slugify(full_name, only_ascii=True)}.pdf", "content": contract.read(), "mimetype": "application/pdf", }], ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_confirmation_change_email(self, new_email, user_pk, **kwargs): try: Person.objects.get(pk=user_pk) except Person.DoesNotExist: return subscription_token = add_email_confirmation_token_generator.make_token( new_email=new_email, user=user_pk ) query_args = { "new_email": new_email, "user": user_pk, "token": subscription_token, **kwargs, } confirm_change_mail_url = front_url( "confirm_change_mail", query=query_args, auto_login=False ) try: send_mosaico_email( code="CHANGE_MAIL_CONFIRMATION", subject="confirmer votre changement d'adresse", from_email=settings.EMAIL_FROM, recipients=[new_email], bindings={"CONFIRMATION_URL": confirm_change_mail_url}, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_external_join_confirmation(self, group_pk, email, **kwargs): try: group = SupportGroup.objects.get(pk=group_pk) except SupportGroup.DoesNotExist: return subscription_token = subscription_confirmation_token_generator.make_token( email=email, **kwargs ) confirm_subscription_url = front_url( "external_join_group", args=[group_pk], auto_login=False ) query_args = {"email": email, **kwargs, "token": subscription_token} confirm_subscription_url += "?" + urlencode(query_args) bindings = {"GROUP_NAME": group.name, "JOIN_LINK": confirm_subscription_url} try: send_mosaico_email( code="GROUP_EXTERNAL_JOIN_OPTIN", subject=_(f"Confirmez que vous souhaitez rejoindre « {group.name} »"), from_email=settings.EMAIL_FROM, recipients=[email], bindings=bindings, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_check_information(check_id, force=False): try: check = CheckPayment.objects.get(id=check_id) except CheckPayment.DoesNotExist: return mail_sent = check.meta.get("information_email_sent") if mail_sent and not force: return check_mode = PAYMENT_MODES[check.mode] if check_mode.warnings: warning_list = [ f"<li>{conditional_escape(warning)}</li>" for warning in check_mode.warnings ] warnings = mark_safe( f"<p>Prenez bien garde aux points suivants :</p><ul>{''.join(warning_list)}</ul>" ) else: warnings = "" bindings = { "TITLE": check_mode.title, "ORDER": check_mode.order, "AMOUNT": check.get_price_display(), "PAYMENT_ID": str(check.id), "ADDRESS": mark_safe("<br>".join( conditional_escape(part) for part in check_mode.address)), "ADDITIONAL_INFORMATION": check_mode.additional_information, "WARNINGS": warnings, } send_mosaico_email( code="CHECK_INFORMATION", subject=check_mode.title, from_email=settings.EMAIL_FROM, recipients=[check.person or check.email], bindings=bindings, ) check.meta["information_email_sent"] = True check.save()
def send_welcome_mail(self, person_pk): person = Person.objects.prefetch_related("emails").get(pk=person_pk) try: 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("personal_information")}, recipients=[person], ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_unsubscribe_email(self, person_pk): person = Person.objects.prefetch_related("emails").get(pk=person_pk) bindings = {"MANAGE_SUBSCRIPTIONS_LINK": front_url("contact")} try: 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, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_abuse_report_message(self, inviter_id): if not inviter_id: return try: inviter = Person.objects.get(pk=inviter_id) except Person.DoesNotExist: return try: send_mosaico_email( code="GROUP_INVITATION_ABUSE_MESSAGE", subject="Signalement pour invitation sans consentement", from_email=settings.EMAIL_FROM, recipients=[inviter], ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_login_email(self, email, short_code, expiry_time): utc_expiry_time = timezone.make_aware( timezone.datetime.utcfromtimestamp(expiry_time), timezone.utc) local_expiry_time = timezone.localtime(utc_expiry_time) try: send_mosaico_email( code="LOGIN_MESSAGE", subject="Connexion à agir.lafranceinsoumise.fr", from_email=settings.EMAIL_FROM, bindings={ "CODE": interleave_spaces(short_code), "EXPIRY_TIME": local_expiry_time.strftime("%H:%M"), }, recipients=[email], ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_confirmation_merge_account(self, user_pk_requester, user_pk_merge, **kwargs): """Envoie une demande de fusion de conte. La premiere clée primaire de person est celle du profil qui va rester, la deuxième est celle du profil qui sera suprimé une fois les données du compte fusionées. Si l'un des deux utilisateur n'existe pas ou que l'utilisqteur est le même, l'opération est annulé. """ try: requester_email = Person.objects.get(pk=user_pk_requester).email merge_email = Person.objects.get(pk=user_pk_merge).email except Person.DoesNotExist: return if user_pk_requester == user_pk_merge: return subscription_token = merge_account_token_generator.make_token( pk_requester=str(user_pk_requester), pk_merge=str(user_pk_merge) ) query_args = { "pk_requester": user_pk_requester, "pk_merge": user_pk_merge, "token": subscription_token, **kwargs, } confirm_merge_account = front_url( "confirm_merge_account", query=query_args, auto_login=False ) try: send_mosaico_email( code="MERGE_ACCOUNT_CONFIRMATION", subject="Veuillez confirmer la fusion de compte", from_email=settings.EMAIL_FROM, recipients=[merge_email], bindings={ "CONFIRMATION_URL": confirm_merge_account, "REQUESTER_EMAIL": requester_email, }, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_person_form_confirmation(self, submission_pk): try: submission = PersonFormSubmission.objects.get(pk=submission_pk) except: return person = submission.person form = submission.form bindings = {"CONFIRMATION_NOTE": mark_safe(form.confirmation_note)} try: send_mosaico_email( code="FORM_CONFIRMATION", subject=_("Confirmation"), from_email=settings.EMAIL_FROM, recipients=[person], bindings=bindings, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_support_group_changed_notification(self, 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} ) 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") ] try: 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, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_someone_joined_notification(self, 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} ), } try: 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, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_confirmation_email(self, email, **kwargs): if PersonEmail.objects.filter(address__iexact=email).exists(): p = Person.objects.get_by_natural_key(email) try: send_mosaico_email( code="ALREADY_SUBSCRIBED_MESSAGE", subject=_("Vous êtes déjà inscrit !"), from_email=settings.EMAIL_FROM, bindings={ "PANEL_LINK": front_url("dashboard", auto_login=True), "AGO": pretty_time_since(p.created), }, recipients=[p], ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc) return subscription_token = subscription_confirmation_token_generator.make_token( email=email, **kwargs ) confirm_subscription_url = front_url("subscription_confirm", auto_login=False) query_args = {"email": email, **kwargs, "token": subscription_token} confirm_subscription_url += "?" + urlencode(query_args) try: send_mosaico_email( code="SUBSCRIPTION_CONFIRMATION_MESSAGE", subject=_("Plus qu'un clic pour vous inscrire"), from_email=settings.EMAIL_FROM, recipients=[email], bindings={"CONFIRMATION_URL": confirm_subscription_url}, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_person_form_notification(self, submission_pk): try: submission = PersonFormSubmission.objects.get(pk=submission_pk) except: return form = submission.form if form.send_answers_to is None: return person = submission.person pretty_submission = get_formatted_submission(submission) bindings = { "ANSWER_EMAIL": person.email, "FORM_NAME": form.title, "INFORMATIONS": mark_safe( render_to_string( "people/includes/personform_submission_data.html", {"submission_data": pretty_submission}, ) ), } try: send_mosaico_email( code="FORM_NOTIFICATION", subject=_("Formulaire : " + form.title), from_email=settings.EMAIL_FROM, reply_to=[person.email], recipients=[form.send_answers_to], bindings=bindings, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def send_support_group_creation_notification(self, 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", auto_login=False, kwargs={"pk": group.pk} ), "MANAGE_GROUP_LINK": front_url("manage_group", kwargs={"pk": group.pk}), } try: send_mosaico_email( code="GROUP_CREATION", subject=_("Les informations de votre nouveau groupe d'action"), from_email=settings.EMAIL_FROM, recipients=[referent], bindings=bindings, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)
def invite_to_group(self, group_id, invited_email, inviter_id): try: group = SupportGroup.objects.get(pk=group_id) except SupportGroup.DoesNotExist: return try: person = Person.objects.get_by_natural_key(invited_email) except Person.DoesNotExist: person = None group_name = group.name report_params = {"group_id": group_id, "inviter_id": inviter_id} report_params["token"] = abusive_invitation_report_token_generator.make_token( **report_params ) report_url = front_url("report_invitation_abuse", query=report_params) if person: invitation_token = invitation_confirmation_token_generator.make_token( person_id=person.pk, group_id=group_id ) join_url = front_url( "invitation_confirmation", query={ "person_id": person.id, "group_id": group_id, "token": invitation_token, }, ) try: send_mosaico_email( code="GROUP_INVITATION_MESSAGE", subject="Vous avez été invité à rejoindre un groupe de la FI", from_email=settings.EMAIL_FROM, recipients=[person], bindings={ "GROUP_NAME": group_name, "CONFIRMATION_URL": join_url, "REPORT_URL": report_url, }, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc) else: invitation_token = subscription_confirmation_token_generator.make_token( email=invited_email, group_id=group_id ) join_url = front_url( "invitation_with_subscription_confirmation", query={ "email": invited_email, "group_id": group_id, "token": invitation_token, }, ) try: send_mosaico_email( code="GROUP_INVITATION_WITH_SUBSCRIPTION_MESSAGE", subject="Vous avez été invité à rejoindre la France insoumise", from_email=settings.EMAIL_FROM, recipients=[invited_email], bindings={ "GROUP_NAME": group_name, "CONFIRMATION_URL": join_url, "REPORT_URL": report_url, }, ) except (smtplib.SMTPException, socket.error) as exc: self.retry(countdown=60, exc=exc)