예제 #1
0
파일: models.py 프로젝트: jacqui/squarelet
 def send(self):
     send_mail(
         subject=_(f"Invitation to join {self.organization.name}"),
         template="organizations/email/invitation.html",
         to=[self.email],
         extra_context={"invitation": self},
     )
예제 #2
0
파일: tasks.py 프로젝트: jacqui/squarelet
def handle_invoice_failed(invoice_data):
    """Handle receiving a invoice.payment_failed event from the Stripe webhook"""
    try:
        organization = Organization.objects.get(
            customer_id=invoice_data["customer"])
    except Organization.DoesNotExist:
        if invoice_data["lines"]["data"][0]["plan"]["id"] == "donate":
            # donations are handled through muckrock - do not log an error
            return
        logger.error(
            "Invoice failed (%s) for customer (%s) with no matching organization",
            invoice_data["id"],
            invoice_data["customer"],
        )
        return

    organization.payment_failed = True
    organization.save()

    logger.info("Payment failed: %s", invoice_data)

    attempt = invoice_data["attempt_count"]
    if attempt == 4:
        subject = _("Your subscription has been cancelled")
        organization.subscription_cancelled()
    else:
        subject = _("Your payment has failed")

    send_mail(
        subject=subject,
        template="organizations/email/payment_failed.html",
        organization=organization,
        organization_to=ORG_TO_ADMINS,
        extra_context={"attempt": "final" if attempt == 4 else attempt},
    )
예제 #3
0
 def post(self, request, *args, **kwargs):
     self.organization = self.get_object()
     if not self.request.user.is_authenticated:
         return redirect(self.organization)
     is_member = self.organization.has_member(self.request.user)
     if request.POST.get("action") == "join" and not is_member:
         self.organization.invitations.create(email=request.user.email,
                                              user=request.user,
                                              request=True)
         messages.success(request,
                          _("Request to join the organization sent!"))
         send_mail(
             subject=_(
                 f"{request.user} has requested to join {self.organization}"
             ),
             template="organizations/email/join_request.html",
             organization=self.organization,
             organization_to=ORG_TO_ADMINS,
             extra_context={"joiner": request.user},
         )
     elif request.POST.get("action") == "leave" and is_member:
         self.request.user.memberships.filter(
             organization=self.organization).delete()
         messages.success(request, _("You have left the organization"))
         mixpanel_event(
             request,
             "Left Organization",
             {
                 "Name": self.organization.name,
                 "UUID": str(self.organization.uuid)
             },
         )
     return redirect(self.organization)
예제 #4
0
def email_changed(request, user, from_email_address, to_email_address,
                  **kwargs):
    """The user has changed their primary email"""
    # update the stripe customer for all accounts
    # Leave out presspass for now, as they do not have a stripe account yet
    accounts = [StripeAccounts.muckrock]
    for account in accounts:
        customer = user.individual_organization.customer(
            account).stripe_customer
        customer.email = to_email_address.email
        customer.save()

    # clear the email failed flag
    with transaction.atomic():
        user.email_failed = False
        user.save()
        # send client sites a cache invalidation to update this user's info
        transaction.on_commit(
            lambda: send_cache_invalidations("user", user.uuid))

    # send the user a notification
    send_mail(
        subject=_("Changed email address"),
        template="users/email/email_change.html",
        to=[from_email_address.email],
        source=user.source,
        extra_context={
            "from_email_address": from_email_address.email,
            "to_email_address": to_email_address.email,
        },
    )
예제 #5
0
 def send_receipt(self):
     """Send receipt"""
     send_mail(
         subject=_("Receipt"),
         template="organizations/email/receipt.html",
         organization=self.organization,
         organization_to=ORG_TO_RECEIPTS,
         extra_context={
             "charge": self,
             "individual_subscription": self.description == "Professional",
             "group_subscription":
             self.description.startswith("Organization"),
         },
     )
예제 #6
0
    def send(self, source=None):

        # default source to "muckrock" in case this is called without a value
        if source is None:
            source = "muckrock"

        if self.request:
            send_mail(
                subject=_(f"{self.user} has requested to join {self.organization}"),
                template="organizations/email/join_request.html",
                organization=self.organization,
                organization_to=ORG_TO_ADMINS,
                source=source,
                extra_context={"joiner": self.user},
            )
        else:
            send_mail(
                subject=_(f"Invitation to join {self.organization.name}"),
                template="organizations/email/invitation.html",
                to=[self.email],
                source=source,
                extra_context={"invitation": self},
            )
예제 #7
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        headers = self.get_success_headers(serializer.data)

        setup_user_email(request, user, [])
        if not user.is_agency:
            email_address = EmailAddress.objects.get_primary(user)
            key = EmailConfirmationHMAC(email_address).key
            activate_url = reverse("account_confirm_email", args=[key])
            send_mail(
                subject=_("Welcome to MuckRock"),
                template="account/email/email_confirmation_signup_message.html",
                user=user,
                extra_context={
                    "activate_url": activate_url,
                    "minireg": True
                },
            )

        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
예제 #8
0
def email_changed(request, user, from_email_address, to_email_address,
                  **kwargs):
    """The user has changed their primary email"""
    # update their stripe customer
    customer = user.individual_organization.customer
    customer.email = to_email_address.email
    customer.save()
    # clear the email failed flag
    with transaction.atomic():
        user.email_failed = False
        user.save()
        # send client sites a cache invalidation to update this user's info
        transaction.on_commit(
            lambda: send_cache_invalidations("user", user.uuid))
    # send the user a notification
    send_mail(
        subject=_("Changed email address"),
        template="users/email/email_change.html",
        to=[from_email_address.email],
        extra_context={
            "from_email_address": from_email_address.email,
            "to_email_address": to_email_address.email,
        },
    )