import re
import subprocess
from datetime import date
from flask import Blueprint, current_app, render_template, request
from flask_weasyprint import HTML, render_pdf
from .forms import MembershipForm
root_page = Blueprint("root", __name__)
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.route("/", methods=("GET", "POST"))
def index():
form = MembershipForm()
choices = [("cash", "by cash"), ("wire transfer", "by wire transfer")]
for k, v in (
("DIGICASH_ENABLED", ("digicash", "by DigiCash")),
("BITCOIN_ENABLED", ("bitcoin", "by bitcoin")),
("ETHEREUM_ENABLED", ("ethereum", "by ethereum")),
("MONERO_ENABLED", ("monero", "by monero")),
("ZCASH_ENABLED", ("zcash", "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
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)