Skip to content

Commit

Permalink
Make next URL and next label work via session
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanw committed Nov 28, 2024
1 parent f92df06 commit 110bf8d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions fragdenstaat_de/fds_donation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.core.mail import mail_managers
from django.core.validators import MinValueValidator
from django.utils.html import format_html
from django.utils.http import url_has_allowed_host_and_scheme
from django.utils.translation import gettext_lazy as _

from froide_payment.forms import StartPaymentMixin
Expand Down Expand Up @@ -81,6 +82,9 @@ class DonationSettingsForm(forms.Form):
initial_receipt = forms.BooleanField(required=False)
collapsed = forms.BooleanField(required=False)

next_url = forms.CharField(required=False)
next_label = forms.CharField(required=False)

def clean_amount_presets(self):
presets = self.cleaned_data["amount_presets"]
if presets == "-":
Expand Down Expand Up @@ -109,6 +113,14 @@ def clean_initial_receipt(self):
receipt = self.cleaned_data["initial_receipt"]
return int(receipt)

def clean_next_url(self):
next_url = self.cleaned_data["next_url"]
if url_has_allowed_host_and_scheme(
next_url, allowed_hosts=settings.ALLOWED_REDIRECT_HOSTS
):
return next_url
return ""

def make_donation_form(self, **kwargs):
d = {}
if self.is_valid():
Expand All @@ -131,6 +143,8 @@ class DonationFormFactory:
"initial_interval": 0,
"initial_receipt": "0",
"collapsed": False,
"next_url": "",
"next_label": "",
}
initials = {
"initial_amount": "amount",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.16 on 2024-11-28 15:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('fds_donation', '0045_donationprogressbarcmsplugin_purpose'),
]

operations = [
migrations.AddField(
model_name='donationformcmsplugin',
name='next_label',
field=models.CharField(blank=True, max_length=255),
),
]
3 changes: 3 additions & 0 deletions fragdenstaat_de/fds_donation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ class DonationFormCMSPlugin(CMSPlugin):

form_action = models.CharField(max_length=255, blank=True)
next_url = models.CharField(max_length=255, blank=True)
next_label = models.CharField(max_length=255, blank=True)

open_in_new_tab = models.BooleanField(default=False)

Expand Down Expand Up @@ -557,6 +558,8 @@ def make_form(self, **kwargs):
"collapsed": self.collapsed,
"gift_options": [gift.id for gift in self.gift_options.all()],
"default_gift": self.default_gift_id,
"next_url": self.next_url,
"next_label": self.next_label,
}
)
return form.make_donation_form(**kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ <h1>Vielen Dank für Ihre Spende!</h1>
{% include "fds_donation/includes/banktransfer.html" with payment=last_donation.payment order=last_donation.payment.order %}
</div>
{% endif %}
{% if extra_action_url %}
<div class="alert alert-info text-center">
<a class="btn btn-primary btn-lg"
target="_blank"
href="{{ extra_action_url }}">{{ extra_action_label }}</a>
</div>
{% endif %}
<p class="text-end">
<a href="{{ object.get_absolute_change_url }}"
class="btn btn-secondary mb-2">Spenderdaten aktualisieren</a>
Expand Down
5 changes: 5 additions & 0 deletions fragdenstaat_de/fds_donation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def get_form(self, form_class=None):
def form_valid(self, form):
order, related_obj = form.save()
method = form.cleaned_data["payment_method"]
if form.settings["next_url"]:
self.request.session["extra_action_url"] = form.settings["next_url"]
self.request.session["extra_action_label"] = form.settings["next_label"]
return redirect(order.get_absolute_payment_url(method))


Expand Down Expand Up @@ -133,6 +136,8 @@ def get_context_data(self, **kwargs):
"subscriptions": self.object.subscriptions.filter(canceled=None),
"donations": donations,
"last_donation": last_donation,
"extra_action_url": self.request.session.pop("extra_action_url"),
"extra_action_label": self.request.session.pop("extra_action_label"),
}
)
return ctx
Expand Down

0 comments on commit 110bf8d

Please sign in to comment.