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="/") xml_template = "{name}{birthday:%d.%m.%Y}
{address}
{username}{email}{status}{voting}
" @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"] else: price = current_app.config["REGULAR_FEE"] if form.membership.data == "supporting": xml_data["voting"] = 0 elif form.membership.data == "regular": xml_data["voting"] = 1 if form.starving.data: price = 1 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"]) 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 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)