Changeset - 53bbd1295ee2
[Not reviewed]
default
0 2 0
Dennis Fink - 3 years ago 2022-01-19 20:50:54
dennis.fink@c3l.lu
Show an error message to the user, if we fail fetching conversion rates for the cryptocurrencies
2 files changed with 7 insertions and 5 deletions:
0 comments (0 inline, 0 general)
c3l_membership/templates/index.html
Show inline comments
 
@@ -2,96 +2,99 @@
 
<html lang="en">
 
  <head>
 
    <meta charset="utf-8" />
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 
    <title>{% trans %}C3L Membership Application{% endtrans %}</title>
 

	
 
    <link rel="stylesheet" href="{{ url_for('static', filename='pure-min.css') }}" />
 
    <link rel="stylesheet" href="{{ url_for('static', filename='grids-responsive-min.css') }}" />
 
    <link rel="stylesheet" href="{{ url_for('static', filename='flag-icons.css') }}" />
 
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
 
  </head>
 
  <body>
 
    <div class="pure-g">
 
      <div class="pure-u-md-1-3"></div>
 
      <div class="pure-u-1-1 pure-u-md-1-3">
 
        <div class="pure-menu pure-menu-horizontal">
 
          <ul class="pure-menu-list">
 
            <li class="pure-menu-item">
 
              <a href="{{ url_for("root.index", lang_code="en") }}" class="pure-menu-link"><span class="fi fi-gb"></span> English</a>
 
            </li>
 
            <li class="pure-menu-item">
 
              <a href="{{ url_for("root.index", lang_code="lb") }}" class="pure-menu-link"><span class="fi fi-lu"></span> Lëtzebuergesch</a>
 
            </li>
 
            <li class="pure-menu-item">
 
              <a href="{{ url_for("root.index", lang_code="de") }}" class="pure-menu-link"><span class="fi fi-de"></span> Deutsch</a>
 
            </li>
 
            <li class="pure-menu-item">
 
              <a href="{{ url_for("root.index", lang_code="fr") }}" class="pure-menu-link"><span class="fi fi-fr"></span> Français</a>
 
            </li>
 
          </ul>
 
        </div>
 
      </div>
 
      <div class="pure-u-md-1-3"></div>
 
    </div>
 
    <div class="pure-g">
 
      <div class="pure-u-md-1-3"></div>
 
      <div class="pure-u-1-1 pure-u-md-1-3">
 
        <img src="{{ url_for('static', filename='images/logo.png') }}" class="pure-img" />
 
        <h1>{% trans %}Membership Application{% endtrans %}</h1>
 
        <h2>{% trans %}How to use this form{% endtrans %}</h2>
 
        <p>{% trans %}Fill out this form and click on "Become a member". Afterwards you will be presented with a PDF, which you have to send to info@c3l.lu or bring it to one of our next events. Print it or save it to your local hardware, because we don't save a copy on our servers for data protection reasons!{% endtrans %}</p>
 
        {% if form.errors %}
 
          {% for fieldname, errors in form.errors.items() %}
 
            {% for error in errors %}
 
              <p class="form-error">{{ fieldname }} - {{ error }}</p>
 
            {% endfor %}
 
          {% endfor %}
 
        {% endif %}
 
        {% if crypto_error %}
 
          <p class="form-error">{% trans %}Couldn't fetch conversion rate for cryptocurrencies. Please try again later or use another payment option!{% endtrans %}</p>
 
        {% endif %}
 
        <form class="pure-form pure-form-stacked" method="POST" action="{{ url_for('root.index', lang_code=g.lang_code) }}">
 
          <fieldset>
 
            {{ form.hidden_tag() }}
 
            <legend>{% trans %}Required information{% endtrans %}</legend>
 
            <div class="pure-g">
 
              <div class="pure-u-1-1">
 
                <b>{{ form.username.label }}</b>
 
                {{ form.username(required=True, class="pure-u-1-1 field-error" if form.username.errors else "pure-u-1-1") }}
 
              </div>
 
            </div>
 
            <div class="pure-g">
 
              <div class="pure-u-1-1">
 
                <b>{{ form.email.label }}</b>
 
                {{ form.email(required=True, class="pure-u-1-1 field-error" if form.email.errors else "pure-u-1-1") }}
 
              </div>
 
            </div>
 
            <div class="pure-g">
 
              <div class="pure-u-1-1">
 
                <b>{{ form.fullname.label }}</b>
 
                {{ form.fullname(required=True, class="pure-u-1-1 field-error" if form.fullname.errors else "pure-u-1-1") }}
 
              </div>
 
            </div>
 
            <div class="pure-g">
 
              <div class="pure-u-1-1">
 
                <b>{{ form.membership.label }}</b>
 
                {% for option in form.membership %}
 
                  <label for="{{ option.id }}" class="pure-radio pure-u-1-1">
 
                    {{ option() }} {{ option.label.text }}
 
                  </label>
 
                {% endfor %}
 
                <div class="pure-g">
 
                  <div class="pure-u-1-1">
 
                    <label for="{{ form.student.id }}" class="pure-checkbox">
 
                      {{ form.student }} {{ form.student.label.text|safe}}
 
                    </label>
 
                  </div>
 
                </div>
 
                <div class="pure-g">
 
                  <div class="pure-u-1-1">
 
                    <label for="{{ form.starving.id }}" class="pure-checkbox">
 
                      {{ form.starving }} {{ form.starving.label.text|safe}}
 
                    </label>
 
                  </div>
 
                </div>
 
                <h3>{% trans %}What's difference between the different membership options?{% endtrans %}</h3>
 
                <p>{% trans %}All the options include to following benefits:{% endtrans %}</p>
 
                <ul>
 
                  <li>{% trans %}Access to the <a href="https://wiki.c3l.lu/doku.php?id=organization:membership#benefits">services</a> run by us{% endtrans %}</li>
c3l_membership/views.py
Show inline comments
 
@@ -13,111 +13,110 @@ 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.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
 
            )
 
            try:
 
                current_conversion_r.raise_for_status()
 
            except:
 
                return 500
 
                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)
 

	
 
    return render_template("index.html", form=form, crypto_error=False)
0 comments (0 inline, 0 general)