Files @ 88aebaa55be4
Branch filter:

Location: C3L/C3L-Membership-Online-Form/c3l_membership/views.py - annotation

Dennis Fink
Merge crypto
587a9a50643f
6f06fc328a13
6f06fc328a13
6f06fc328a13
ade19bdcb84a
4005eddcc559
805cb46ada9a
6f06fc328a13
6f06fc328a13
6f06fc328a13
6f06fc328a13
4005eddcc559
6f06fc328a13
587a9a50643f
587a9a50643f
6f06fc328a13
4005eddcc559
4005eddcc559
4005eddcc559
4005eddcc559
4005eddcc559
4005eddcc559
4005eddcc559
8e2c954d832f
8e2c954d832f
8e2c954d832f
4005eddcc559
4005eddcc559
6f06fc328a13
6f06fc328a13
6f06fc328a13
7cf7d1e162a4
805cb46ada9a
805cb46ada9a
805cb46ada9a
805cb46ada9a
7cf7d1e162a4
10cfc4437178
10cfc4437178
10cfc4437178
10cfc4437178
10cfc4437178
10cfc4437178
10cfc4437178
10cfc4437178
ade19bdcb84a
10cfc4437178
10cfc4437178
10cfc4437178
7cf7d1e162a4
7cf7d1e162a4
6f06fc328a13
6f06fc328a13
587a9a50643f
587a9a50643f
52127f437376
52127f437376
52127f437376
52127f437376
52127f437376
7cf7d1e162a4
587a9a50643f
52127f437376
7cf7d1e162a4
587a9a50643f
6f06fc328a13
52127f437376
6f06fc328a13
ade19bdcb84a
587a9a50643f
ade19bdcb84a
587a9a50643f
ade19bdcb84a
587a9a50643f
ade19bdcb84a
587a9a50643f
ade19bdcb84a
587a9a50643f
3e495ae230a2
3e495ae230a2
3e495ae230a2
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
10cfc4437178
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
ade19bdcb84a
6f06fc328a13
6f06fc328a13
6f06fc328a13
587a9a50643f
587a9a50643f
587a9a50643f
6f06fc328a13
587a9a50643f
587a9a50643f
587a9a50643f
587a9a50643f
6f06fc328a13
6f06fc328a13
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"]
            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
            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)