diff --git a/c3l_membership/views.py b/c3l_membership/views.py --- a/c3l_membership/views.py +++ b/c3l_membership/views.py @@ -2,6 +2,7 @@ import re import subprocess from datetime import date +import requests from flask import Blueprint, current_app, g, render_template, request from flask_babel import gettext from flask_weasyprint import HTML, render_pdf @@ -34,15 +35,18 @@ def index(): ("wire transfer", gettext("by wire transfer")), ] - for k, v in ( - ("DIGICASH_ENABLED", ("digicash", gettext("by DigiCash"))), - ("BITCOIN_ENABLED", ("bitcoin", gettext("by bitcoin"))), - ("ETHEREUM_ENABLED", ("ethereum", gettext("by ethereum"))), - ("MONERO_ENABLED", ("monero", gettext("by monero"))), - ("ZCASH_ENABLED", ("zcash", gettext("by zcash"))), - ): - if current_app.config[k]: - choices.append(v) + if current_app.config["DIGICASH_ENABLED"]: + choices.append(("digicash", gettext("by DigiCash"))) + + for cryptocurrency_label, options in current_app.config["CRYPTOCURRENCIES"].items(): + if options["ENABLED"]: + choices.append( + ( + cryptocurrency_label, + " ".join((gettext("by"), cryptocurrency_label.title())), + ), + ) + form.payment.choices = choices if form.validate_on_submit(): @@ -62,14 +66,15 @@ def index(): if form.starving.data: price = 1 - xml_data["status"] = "Starving" + status = "Starving" elif form.minor_member.data or form.student.data: - xml_data["status"] = "Student" + status = "Student" elif form.membership.data == "supporting": - xml_data["status"] = "Supporter" + status = "Supporter" else: - xml_data["status"] = "Regular" + status = "Regular" + xml_data["status"] = status xml_data["name"] = form.fullname.data xml_data["birthday"] = ( form.birthday.data if form.birthday.data is not None else date(9999, 12, 12) @@ -87,22 +92,21 @@ def index(): ) xml_data["address"] = re.sub("\s+", " ", xml_data["address"]) - if form.payment.data == "bitcoin": - price = subprocess.check_output( - [current_app.config["BITCOIN_CONVERSION_SCRIPT"], str(price)] - ).decode("utf-8") - elif form.payment.data == "ethereum": - price = subprocess.check_output( - [current_app.config["ETHEREUM_CONVERSION_SCRIPT"], str(price)] - ).decode("utf-8") - elif form.payment.data == "monero": - price = subprocess.check_output( - [current_app.config["MONERO_CONVERSION_SCRIPT"], str(price)] - ).decode("utf-8") - elif form.payment.data == "zcash": - price = subprocess.check_output( - [current_app.config["ZCASH_CONVERSION_SCRIPT"], str(price)] - ).decode("utf-8") + if form.payment.data in current_app.config["CRYPTOCURRENCIES"]: + + current_conversion_r = requests.get( + current_app.config["CONVERSION_URL"], timeout=30 + ) + try: + current_conversion_r.raise_for_status() + except: + return 500 + else: + current_conversion = current_conversion_r.json() + commodity = current_app.config["CRYPTOCURRENCIES"][form.payment.data][ + "COMMODITY" + ] + price = current_conversion[commodity][status.upper()] elif form.payment.data == "digicash": price = price * 100