From 110bf8d35bb588bac1b537029b332c66d090684b Mon Sep 17 00:00:00 2001 From: Stefan Wehrmeyer Date: Thu, 28 Nov 2024 17:01:12 +0100 Subject: [PATCH] Make next URL and next label work via session --- fragdenstaat_de/fds_donation/forms.py | 14 ++++++++++++++ .../0046_donationformcmsplugin_next_label.py | 18 ++++++++++++++++++ fragdenstaat_de/fds_donation/models.py | 3 +++ .../templates/fds_donation/donor_detail.html | 7 +++++++ fragdenstaat_de/fds_donation/views.py | 5 +++++ 5 files changed, 47 insertions(+) create mode 100644 fragdenstaat_de/fds_donation/migrations/0046_donationformcmsplugin_next_label.py diff --git a/fragdenstaat_de/fds_donation/forms.py b/fragdenstaat_de/fds_donation/forms.py index 3bf0fb09d..9545a1f8a 100644 --- a/fragdenstaat_de/fds_donation/forms.py +++ b/fragdenstaat_de/fds_donation/forms.py @@ -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 @@ -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 == "-": @@ -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(): @@ -131,6 +143,8 @@ class DonationFormFactory: "initial_interval": 0, "initial_receipt": "0", "collapsed": False, + "next_url": "", + "next_label": "", } initials = { "initial_amount": "amount", diff --git a/fragdenstaat_de/fds_donation/migrations/0046_donationformcmsplugin_next_label.py b/fragdenstaat_de/fds_donation/migrations/0046_donationformcmsplugin_next_label.py new file mode 100644 index 000000000..34fa60904 --- /dev/null +++ b/fragdenstaat_de/fds_donation/migrations/0046_donationformcmsplugin_next_label.py @@ -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), + ), + ] diff --git a/fragdenstaat_de/fds_donation/models.py b/fragdenstaat_de/fds_donation/models.py index cd3e75ef5..ceb4d4d9c 100644 --- a/fragdenstaat_de/fds_donation/models.py +++ b/fragdenstaat_de/fds_donation/models.py @@ -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) @@ -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) diff --git a/fragdenstaat_de/fds_donation/templates/fds_donation/donor_detail.html b/fragdenstaat_de/fds_donation/templates/fds_donation/donor_detail.html index 6217ca878..323aaf042 100644 --- a/fragdenstaat_de/fds_donation/templates/fds_donation/donor_detail.html +++ b/fragdenstaat_de/fds_donation/templates/fds_donation/donor_detail.html @@ -9,6 +9,13 @@

Vielen Dank für Ihre Spende!

{% include "fds_donation/includes/banktransfer.html" with payment=last_donation.payment order=last_donation.payment.order %} {% endif %} + {% if extra_action_url %} +
+ {{ extra_action_label }} +
+ {% endif %}

Spenderdaten aktualisieren diff --git a/fragdenstaat_de/fds_donation/views.py b/fragdenstaat_de/fds_donation/views.py index 1cc2bda88..7d162b61f 100644 --- a/fragdenstaat_de/fds_donation/views.py +++ b/fragdenstaat_de/fds_donation/views.py @@ -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)) @@ -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