From 1a731bcb8624525294113d99095a00c2b45e6e9e Mon Sep 17 00:00:00 2001 From: Kartik Ohri Date: Wed, 11 Sep 2024 19:45:20 +0530 Subject: [PATCH 01/11] Prefill donation form if user is logged in --- metabrainz/payments/views.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/metabrainz/payments/views.py b/metabrainz/payments/views.py index bdc9f15c..cf3000a7 100644 --- a/metabrainz/payments/views.py +++ b/metabrainz/payments/views.py @@ -1,6 +1,7 @@ from __future__ import division from flask import Blueprint, request, render_template, url_for, redirect, current_app, jsonify from flask_babel import gettext +from flask_login import current_user from werkzeug.datastructures import MultiDict from metabrainz.payments import SUPPORTED_CURRENCIES @@ -20,7 +21,10 @@ @payments_bp.route('/donate') def donate(): """Regular donation page.""" - return render_template('payments/donate.html', form=DonationForm()) + form = DonationForm() + if current_user is not None and not current_user.is_anonymous: + form.editor.data = current_user.musicbrainz_id + return render_template('payments/donate.html', form=form) @payments_bp.route('/payment/') From d55b3e478d7ffae8e685253f8d8f501371deae16 Mon Sep 17 00:00:00 2001 From: Kartik Ohri Date: Sat, 14 Sep 2024 01:14:26 +0530 Subject: [PATCH 02/11] Add prefill for donation amount, currency and editor --- metabrainz/payments/views.py | 34 +++++++++++++++++++++-- metabrainz/templates/payments/donate.html | 2 +- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/metabrainz/payments/views.py b/metabrainz/payments/views.py index cf3000a7..b8105807 100644 --- a/metabrainz/payments/views.py +++ b/metabrainz/payments/views.py @@ -1,10 +1,13 @@ from __future__ import division + +from decimal import Decimal, InvalidOperation + from flask import Blueprint, request, render_template, url_for, redirect, current_app, jsonify from flask_babel import gettext from flask_login import current_user from werkzeug.datastructures import MultiDict -from metabrainz.payments import SUPPORTED_CURRENCIES +from metabrainz.payments import SUPPORTED_CURRENCIES, Currency from metabrainz.model.payment import Payment from metabrainz.payments.forms import DonationForm, PaymentForm from metabrainz import flash @@ -22,8 +25,33 @@ def donate(): """Regular donation page.""" form = DonationForm() - if current_user is not None and not current_user.is_anonymous: - form.editor.data = current_user.musicbrainz_id + + if editor := request.args.get('editor'): + form.editor.data = editor + else: + if current_user is not None and not current_user.is_anonymous: + form.editor.data = current_user.musicbrainz_id + + amount = None + if _amount := request.args.get('amount'): + try: + value = Decimal(_amount) + if value >= 0: + amount = value + except (ValueError, TypeError, InvalidOperation): + pass + + if amount is None: + amount = Decimal(50) + form.amount.data = amount + + _currency = request.args.get('currency', 'usd') + if _currency.lower() == 'eur': + currency = 'eur' + else: + currency = 'usd' + form.currency.data = currency + return render_template('payments/donate.html', form=form) diff --git a/metabrainz/templates/payments/donate.html b/metabrainz/templates/payments/donate.html index a15675d8..feae52b8 100644 --- a/metabrainz/templates/payments/donate.html +++ b/metabrainz/templates/payments/donate.html @@ -39,7 +39,7 @@

{{ _('Make a Donation') }}

- {{ form.amount(id="input-amount", class="form-control", value="50.00") | safe }} + {{ form.amount(id="input-amount", class="form-control") | safe }}
{% for subfield in form.currency %}