Skip to content

Commit

Permalink
Add prefill for donation amount, currency and editor
Browse files Browse the repository at this point in the history
  • Loading branch information
amCap1712 committed Sep 13, 2024
1 parent 1a731bc commit d55b3e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
34 changes: 31 additions & 3 deletions metabrainz/payments/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion metabrainz/templates/payments/donate.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h1 class="page-title">{{ _('Make a Donation') }}</h1>
<label for="input-amount" class="col-sm-offset-2 col-sm-3 control-label">{{ _('Amount') }}</label>
<div class="col-sm-4">
<div id="input-amount-fg">
{{ form.amount(id="input-amount", class="form-control", value="50.00") | safe }}
{{ form.amount(id="input-amount", class="form-control") | safe }}
<div id="currency-selector" class="btn-group" data-toggle="buttons">
{% for subfield in form.currency %}
<label class="btn btn-primary {{ 'active' if subfield.checked }}">
Expand Down

0 comments on commit d55b3e4

Please sign in to comment.