Esempio n. 1
0
def crowdfund_request(request, idx, **kwargs):
    """Crowdfund a request"""
    # pylint: disable=unused-argument
    # select for update locks this request in order to prevent a race condition
    # allowing multiple crowdfunds to be created for it
    foia = get_object_or_404(
        FOIARequest.objects.select_for_update().select_related(
            "agency__jurisdiction", "composer"
        ),
        pk=idx,
    )
    # check for unauthorized access
    if not foia.has_perm(request.user, "crowdfund"):
        messages.error(request, "You may not crowdfund this request.")
        return redirect(foia)
    if request.method == "POST":
        # save crowdfund object
        form = CrowdfundForm(request.POST)
        if form.is_valid():
            crowdfund = form.save()
            foia.crowdfund = crowdfund
            foia.save(comment="added a crowdfund")
            messages.success(request, "Your crowdfund has started, spread the word!")
            new_action(
                request.user, "began crowdfunding", action_object=crowdfund, target=foia
            )
            crowdfund.send_intro_email(request.user)
            mixpanel_event(
                request,
                "Launch Request Crowdfund",
                foia.mixpanel_data(
                    {
                        "Name": crowdfund.name,
                        "Payment Capped": crowdfund.payment_capped,
                        "Payment Required": float(crowdfund.payment_required),
                        "Date Due": crowdfund.date_due.isoformat(),
                    }
                ),
            )
            return redirect(foia)

    elif request.method == "GET":
        # create crowdfund form
        default_crowdfund_duration = 30
        date_due = timezone.now() + timedelta(default_crowdfund_duration)
        initial = {
            "name": "Crowdfund Request: %s" % str(foia),
            "description": "Help cover the request fees needed to free these docs!",
            "payment_required": foia.get_stripe_amount(),
            "date_due": date_due,
            "foia": foia,
        }
        form = CrowdfundForm(initial=initial)
        mixpanel_event(request, "Start Request Crowdfund", foia.mixpanel_data())

    return render(request, "forms/foia/crowdfund.html", {"form": form})
Esempio n. 2
0
def crowdfund_request(request, idx, **kwargs):
    """Crowdfund a request"""
    # pylint: disable=unused-argument
    # select for update locks this request in order to prevent a race condition
    # allowing multiple crowdfunds to be created for it
    foia = FOIARequest.objects.select_for_update().get(pk=idx)
    # check for unauthorized access
    if not foia.has_perm(request.user, 'crowdfund'):
        messages.error(request, 'You may not crowdfund this request.')
        return redirect(foia)
    if request.method == 'POST':
        # save crowdfund object
        form = CrowdfundForm(request.POST)
        if form.is_valid():
            crowdfund = form.save()
            foia.crowdfund = crowdfund
            foia.save(comment='added a crowdfund')
            messages.success(
                request, 'Your crowdfund has started, spread the word!'
            )
            new_action(
                request.user,
                'began crowdfunding',
                action_object=crowdfund,
                target=foia
            )
            crowdfund.send_intro_email(request.user)
            return redirect(foia)

    elif request.method == 'GET':
        # create crowdfund form
        default_crowdfund_duration = 30
        date_due = timezone.now() + timedelta(default_crowdfund_duration)
        initial = {
            'name':
                u'Crowdfund Request: %s' % unicode(foia),
            'description':
                'Help cover the request fees needed to free these docs!',
            'payment_required':
                foia.get_stripe_amount(),
            'date_due':
                date_due,
            'foia':
                foia
        }
        form = CrowdfundForm(initial=initial)

    return render(
        request,
        'forms/foia/crowdfund.html',
        {'form': form},
    )
Esempio n. 3
0
def crowdfund_request(request, idx, **kwargs):
    """Crowdfund a request"""
    # pylint: disable=unused-argument
    foia = FOIARequest.objects.get(pk=idx)
    owner_or_staff = request.user == foia.user or request.user.is_staff
    # check for unauthorized access
    if not owner_or_staff:
        messages.error(request, 'You may only crowdfund your own requests.')
        return redirect(foia)
    if foia.has_crowdfund():
        messages.error(request, 'You may only run one crowdfund per request.')
        return redirect(foia)
    if foia.status != 'payment':
        messages.error(request,
                       'You may only crowfund when payment is required.')
        return redirect(foia)
    if request.method == 'POST':
        # save crowdfund object
        form = CrowdfundForm(request.POST)
        if form.is_valid():
            crowdfund = form.save()
            foia.crowdfund = crowdfund
            foia.save(comment='added a crowdfund')
            messages.success(request,
                             'Your crowdfund has started, spread the word!')
            new_action(request.user,
                       'began crowdfunding',
                       action_object=crowdfund,
                       target=foia)
            return redirect(foia)

    elif request.method == 'GET':
        # create crowdfund form
        default_crowdfund_duration = 30
        date_due = datetime.now() + timedelta(default_crowdfund_duration)
        initial = {
            'name': u'Crowdfund Request: %s' % unicode(foia),
            'description':
            'Help cover the request fees needed to free these docs!',
            'payment_required': foia.get_stripe_amount(),
            'date_due': date_due,
            'foia': foia
        }
        form = CrowdfundForm(initial=initial)

    return render_to_response('forms/foia/crowdfund.html', {'form': form},
                              context_instance=RequestContext(request))