Files
@ 3e495ae230a2
Branch filter:
Location: C3L/C3L-Membership-Online-Form/c3l_membership/views.py
3e495ae230a2
4.0 KiB
text/x-python
Mark fields that are not required as explicitly optional
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 | import re
import subprocess
from datetime import date
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")),
]
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)
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"]
xml_data["voting"] = 0
elif form.membership.data == "regular":
price = current_app.config["REGULAR_FEE"]
xml_data["voting"] = 1
if form.starving.data:
price = 1
if form.starving.data:
xml_data["status"] = "Starving"
elif form.minor_member.data or form.student.data:
xml_data["status"] = "Student"
elif form.membership.data == "supporting":
xml_data["status"] = "Supporter"
else:
xml_data["status"] = "Regular"
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"])
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")
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)
|