Example #1
0
def new_exchange_email(request, exchange_id):
    try:
        exchange = Exchange.objects.select_related().get(pk=exchange_id)
    except Exchange.DoesNotExist:
        raise Http404

    project = exchange.get_project()

    if request.method == "POST":
        form = crm_forms.EmailForm(request.POST, business=exchange.business, project=project)
        if form.is_valid():
            memo = form.cleaned_data["memo"]
            recipient = form.cleaned_data["to"]
            context = {"exchange": exchange, "memo": memo, "recipient": recipient}
            recipients = [recipient]
            attachment = generate_exchange_pdf(exchange)
            if project:
                subject = project.name + " " + exchange.type.label
            elif exchange.business:
                subject = exchange.business.name + " " + exchange.type.label
            else:
                subject = exchange.type.label
            subject += " %s" % exchange.date

            if not send_exchange_email(
                exchange_type=exchange.type,
                recipients=recipients,
                subject=subject,
                context=context,
                attachment=attachment,
            ):
                request.notifications.add("Failed to send email")
                return HttpResponseRedirect(reverse("view_business", kwargs={"business_id": exchange.business.id}))

            exchange.delivered = True
            exchange.save()

            return HttpResponseRedirect(reverse("view_business", kwargs={"business_id": exchange.business.id}))
    else:
        form = crm_forms.EmailForm(business=exchange.business, project=project)

    context = {"exchange": exchange, "form": form}
    return context
Example #2
0
def view_exchange_as_pdf(request, exchange_id):
    exchange = get_object_or_404(ledger.Exchange, id=exchange_id, editor=request.user)
    pdf = generate_exchange_pdf(exchange)

    return HttpResponse(pdf, content_type="application/pdf")