def campaign_edit(request, key): """ Edit a campaign, basically delivers the angular app that changes the values via the REST backend. """ campaign = get_campaign_or_404(request, key) rest_url = request.build_absolute_uri("/yeah/rest/campaigns/{}".format(campaign.key)) methods = get_payment_methods() currencies = [{"id": x[0], "display_name": x[1]} for x in Campaign.CURRENCIES] campaign_ser = CampaignSerializer(campaign) initial_data = JSONRenderer().render(campaign_ser.data, accepted_media_type="application/json; indent=4") perk_ser = PerkSerializer(campaign.perks.all(), many=True) initial_perks = JSONRenderer().render(perk_ser.data, accepted_media_type="application/json; indent=4") context = { "rest_url": rest_url, "campaign": campaign, "methods": methods, "initial_data": initial_data, "initial_perks": initial_perks, "currencies": currencies, } return render(request, "campaigns/campaign_edit.html", context)
def campaign_edit(request, key): """ Edit a campaign, basically delivers the angular app that changes the values via the REST backend. """ campaign = get_campaign_or_404(request, key) methods = get_payment_methods() currencies = [{'id': x[0], 'display_name': x[1]} for x in Campaign.CURRENCIES] campaign_ser = CampaignSerializer(campaign) initial_data = JSONRenderer().render(campaign_ser.data, accepted_media_type='application/json; indent=4') perk_ser = PerkSerializer(campaign.perks.all(), many=True) initial_perks = JSONRenderer().render(perk_ser.data, accepted_media_type='application/json; indent=4') context = { 'campaign': campaign, 'methods': methods, 'initial_data': initial_data, 'initial_perks': initial_perks, 'currencies': currencies, } return render(request, 'campaigns/campaign_edit.html', context)
def campaign_edit(request, key): """ Edit a campaign, basically delivers the angular app that changes the values via the REST backend. """ campaign = get_campaign_or_404(request, key) rest_url = request.build_absolute_uri('/yeah/rest/campaigns/{}'.format(campaign.key)) methods = get_payment_methods() currencies = [{'id': x[0], 'display_name': x[1]} for x in Campaign.CURRENCIES] campaign_ser = CampaignSerializer(campaign) initial_data = JSONRenderer().render(campaign_ser.data, accepted_media_type='application/json; indent=4') perk_ser = PerkSerializer(campaign.perks.all(), many=True) initial_perks = JSONRenderer().render(perk_ser.data, accepted_media_type='application/json; indent=4') context = { 'rest_url': rest_url, 'campaign': campaign, 'methods': methods, 'initial_data': initial_data, 'initial_perks': initial_perks, 'currencies': currencies, } return render(request, 'campaigns/campaign_edit.html', context)
def campaign_details(request, key): """ View that shows a single activity. """ logger.debug("campaign details") campaign = get_campaign_or_404(request, key) template_name = 'campaigns/campaign_details.html' if request.method == 'GET': if request.GET.get('embedded', None) is not None: template_name = 'campaigns/campaign_details_embedded.html' methods = get_payment_methods() supporters = Transaction.objects.filter(campaign=campaign, show_name=True) context = { 'campaign': campaign, 'methods': methods, 'supporters': supporters } try: if settings.YLDT_PLEDGE_BUTTON_TEXT: context['pledge_button_text'] = settings.YLDT_PLEDGE_BUTTON_TEXT except AttributeError: pass return render(request, template_name, context)
def list_payment_methods(request, key): campaign = get_object_or_404(Campaign, key=key) methods = get_payment_methods() ser_methods = PaymentMethodSerializer(methods, many=True, context={ 'request': request, 'key': key }) return Response(ser_methods.data)
def select_payment(request, key): """ The user has clicked the "Pledge now" button. Let's show him the list of available Payment methods """ campaign = get_campaign_or_404(request, key) perk_id = request.GET.get('perk', None) pledge_value = Decimal('0.0') perk = None if perk_id != None: perk = get_object_or_404(Perk, pk=int(perk_id)) pledge_value = perk.amount methods = get_payment_methods() context = { 'campaign': campaign, 'selected_perk': perk, 'methods': methods, 'pledge_value': pledge_value, } if request.method == 'POST': form = forms.SelectPaymentForm(campaign, perk, request.POST) if form.is_valid(): amount = form.cleaned_data.get('amount') payment_method_name= form.cleaned_data.get('payment_method') method = get_method_by_name(payment_method_name) email = form.cleaned_data.get('email1') name = form.cleaned_data.get('name') show_name = not form.cleaned_data['hide_name'] # Create a new payment transaction with a random ID. transaction_id = str(uuid.uuid4()) perk_id = None if perk != None: perk_id = perk.id PledgePaymentCommand(transaction_id, campaign.key, amount, email, perk_id, name, show_name) UnverifyPaymentCommand(transaction_id, payment_method_name) # Delegate the payment transaction to the pay() method of the selected # payment method. The method will then redirect the user to the page it needs # to complete the payment. return method.pay(request, campaign, transaction_id) else: return render(request, 'campaigns/select_payment.html', dict(context, form=form)) else: return render(request, 'campaigns/select_payment.html', context)
def test_select_payment_with_right_amount(client, campaign): """ Test if we can select a payment and a perk """ method = get_payment_methods()[0] perk = campaign.perks.last() resp = client.post( reverse('select_payment', args=[campaign.key]) + '?perk={}'.format(perk.id), data={ 'payment_method': method.name, 'amount': str(perk.amount), 'email1': '*****@*****.**', 'email2': '*****@*****.**', }) # should redirect to a payment page. assert resp.status_code == 200
def test_select_payment_with_right_amount(client, campaign): """ Test if we can select a payment and a perk """ method = get_payment_methods()[0] perk = campaign.perks.last() resp = client.post(reverse('select_payment', args=[campaign.key]) + '?perk={}'.format(perk.id), data={ 'payment_method': method.name, 'amount': str(perk.amount), 'email1': '*****@*****.**', 'email2': '*****@*****.**', }) # should redirect to a payment page. assert resp.status_code == 200
def select_payment(request, key): """ The user has clicked the "Pledge now" button. Let's show him the list of available Payment methods """ campaign = get_campaign_or_404(request, key) perk_id = request.GET.get("perk", None) pledge_value = Decimal("0.0") perk = None if perk_id != None: perk = get_object_or_404(Perk, pk=int(perk_id)) pledge_value = perk.amount methods = get_payment_methods() context = {"campaign": campaign, "selected_perk": perk, "methods": methods, "pledge_value": pledge_value} if request.method == "POST": form = forms.SelectPaymentForm(campaign, perk, request.POST) if form.is_valid(): amount = form.cleaned_data.get("amount") payment_method_name = form.cleaned_data.get("payment_method") method = get_method_by_name(payment_method_name) email = form.cleaned_data.get("email1") name = form.cleaned_data.get("name") show_name = not form.cleaned_data["hide_name"] # Create a new payment transaction with a random ID. transaction_id = str(uuid.uuid4()) perk_id = None if perk != None: perk_id = perk.id BeginPayment(transaction_id, campaign.key, amount, email, perk_id, name, show_name, payment_method_name) # Delegate the payment transaction to the pay() method of the selected # payment method. The method will then redirect the user to the page it needs # to complete the payment. return method.pay(request, campaign, transaction_id) else: return render(request, "campaigns/select_payment.html", dict(context, form=form)) else: return render(request, "campaigns/select_payment.html", context)
def test_select_payment_with_low_amount(client, campaign): """ Test if we can select a payment and a perk """ method = get_payment_methods()[0] perk = campaign.perks.last() resp = client.post( reverse('select_payment', args=[campaign.key]) + '?perk={}'.format(perk.id), data={ 'payment_method': method, 'amount': str(perk.amount / Decimal('2.0')), 'email1': '*****@*****.**', 'email2': '*****@*****.**', }) assert resp.status_code == 200 dom = html.fromstring(resp.content) # there should be errors assert len(dom.cssselect('.errorlist')) >= 1
def test_select_payment_with_low_amount(client, campaign): """ Test if we can select a payment and a perk """ method = get_payment_methods()[0] perk = campaign.perks.last() resp = client.post(reverse('select_payment', args=[campaign.key]) + '?perk={}'.format(perk.id), data={ 'payment_method': method, 'amount': str(perk.amount / Decimal('2.0')), 'email1': '*****@*****.**', 'email2': '*****@*****.**', }) assert resp.status_code == 200 dom = html.fromstring(resp.content) # there should be errors assert len(dom.cssselect('.errorlist')) >= 1
def campaign_details(request, key): """ View that shows a single activity. """ logger.debug("campaign details") campaign = get_campaign_or_404(request, key) template_name = "campaigns/campaign_details.html" if request.method == "GET": if request.GET.get("embedded", None) is not None: template_name = "campaigns/campaign_details_embedded.html" methods = get_payment_methods() supporters = Transaction.objects.filter(campaign=campaign, show_name=True) context = {"campaign": campaign, "methods": methods, "supporters": supporters} try: if settings.YLDT_PLEDGE_BUTTON_TEXT: context["pledge_button_text"] = settings.YLDT_PLEDGE_BUTTON_TEXT except AttributeError: pass return render(request, template_name, context)
def campaign_details(request, key): """ View that shows a single campaign. """ logger.debug("campaign details") campaign = get_campaign_or_404(request, key) template_name = 'campaigns/campaign_details.html' if request.method == 'GET': if request.GET.get('embedded', None) is not None: template_name = 'campaigns/campaign_details_embedded.html' methods = get_payment_methods() supporters = Transaction.objects.filter(campaign=campaign, show_name=True) context = {'campaign': campaign, 'methods': methods, 'supporters': supporters} try: if settings.YLDT_PLEDGE_BUTTON_TEXT: context['pledge_button_text'] = settings.YLDT_PLEDGE_BUTTON_TEXT except AttributeError: pass return render(request, template_name, context)
def get_payment_methods(self, obj): methods = get_payment_methods() context = dict(self.context, key=obj.key) ser = PaymentMethodSerializer(methods, many=True, context=context) return ser.data
def list_payment_methods(request, key): campaign = get_object_or_404(Campaign, key=key) methods = get_payment_methods() ser_methods = PaymentMethodSerializer(methods, many=True, context={"request": request, "key": key}) return Response(ser_methods.data)