Files
@ 53bbd1295ee2
Branch filter:
Location: C3L/C3L-Membership-Online-Form/c3l_membership/views.py
53bbd1295ee2
3.9 KiB
text/x-python
Show an error message to the user, if we fail fetching conversion rates for the cryptocurrencies
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 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
from .forms import MembershipForm
root_page = Blueprint("root", __name__, url_prefix="/<lang_code>")
xml_template = "<member><numm>{name}</numm><gebuertsdag>{birthday:%d.%m.%Y}</gebuertsdag><address>{address}</address><nick>{username}</nick><email>{email}</email><status>{status}</status><stemmrecht>{voting}</stemmrecht></member>"
@root_page.url_defaults
def add_lang_code(endpoint, values):
values.setdefault("lang_code", g.lang_code)
@root_page.url_value_preprocessor
def pull_lang_code(endpoint, values):
lang_code = values.pop("lang_code")
if lang_code != "favicon.ico":
g.lang_code = lang_code
@root_page.route("/", methods=("GET", "POST"))
def index():
form = MembershipForm()
choices = [
("cash", gettext("by cash")),
("wire transfer", gettext("by wire transfer")),
]
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():
xml_data = {}
if (
form.minor_member.data
or form.student.data
or form.membership.data == "supporting"
):
price = current_app.config["SUPPORTING_FEE"]
elif form.starving.data:
price = 1
else:
price = current_app.config["REGULAR_FEE"]
if form.starving.data:
status = "Starving"
elif form.minor_member.data or form.student.data:
status = "Student"
elif form.membership.data == "supporting":
status = "Supporter"
else:
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)
)
xml_data["username"] = form.username.data
xml_data["email"] = form.email.data
xml_data["address"] = " ".join(
(
form.street.data,
form.zip.data,
form.city.data,
form.state.data,
form.country.data,
)
)
xml_data["address"] = re.sub("\s+", " ", xml_data["address"])
xml_data["voting"] = 1 if form.membership.data == "regular" else 0
if form.payment.data in current_app.config["CRYPTOCURRENCIES"]:
try:
current_conversion_r = requests.get(
current_app.config["CONVERSION_URL"], timeout=30
)
current_conversion_r.raise_for_status()
except:
return render_template("index.html", form=form, crypto_error=True), 503
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
now = date.today()
year = now.year
xml_data["date"] = now
xml = xml_template.format(**xml_data)
html = render_template(
"member.html", form=form, price=price, year=year, xml=xml
)
return render_pdf(HTML(string=html))
return render_template("index.html", form=form, crypto_error=False)
|