Changeset - 9b37bb918b8a
[Not reviewed]
default
0 2 0
Dennis Fink - 3 years ago 2022-01-19 23:35:16
dennis.fink@c3l.lu
Reformat
2 files changed with 8 insertions and 6 deletions:
0 comments (0 inline, 0 general)
c3l_membership/__init__.py
Show inline comments
 
import base64
 
import json
 
import os.path
 
import secrets
 
import subprocess
 

	
 
from flask import Flask, g, redirect, request, url_for
 
from flask.cli import AppGroup
 
from flask_babel import Babel
 
from flask_qrcode import QRcode
 

	
 
qrcode = QRcode()
 
babel = Babel()
 
babel_cli = AppGroup("babel")
 

	
 

	
 
def create_app():
 

	
 
    app = Flask(__name__)
 

	
 
    if app.debug:
 
        config_file = os.path.abspath("config.json")
 
    else:
 
        config_file = os.path.abspath("/etc/membership.json")
 
    config_file = (
 
        os.path.abspath("config.json")
 
        if app.debug
 
        else os.path.abspath("/etc/membership.json")
 
    )
 

	
 
    try:
 
        app.config.from_file(config_file, load=json.load)
 
    except FileNotFoundError:
 
        pass
 

	
 
    qrcode.init_app(app)
 
    babel.init_app(app)
 

	
 
    _default_secret_key = base64.b64encode(secrets.token_bytes()).decode("utf-8")
 
    app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", _default_secret_key)
 
    app.config["LANGUAGES"] = ["en", "de", "fr", "lb"]
 

	
 
    app.config.setdefault("SUPPORTING_FEE", 40)
 
    app.config.setdefault("REGULAR_FEE", 120)
 

	
 
    app.config.setdefault("DIGICASH_ENABLED", True)
 
    app.config.setdefault("CRYPTOCURRENCIES", dict())
 

	
 
    @babel.localeselector
 
    def get_locale():
 
        if not g.get("lang_code", None):
 
            g.lang_code = request.accept_languages.best_match(app.config["LANGUAGES"])
 
        return g.lang_code
 

	
 
    from .views import root_page
 

	
 
    app.register_blueprint(root_page)
 

	
 
    @app.route("/")
 
    def home():
 
        g.lang_code = request.accept_languages.best_match(app.config["LANGUAGES"])
 
        return redirect(url_for("root.index"))
 

	
 
    app.cli.add_command(babel_cli)
 

	
 
    return app
 

	
 

	
 
@babel_cli.command("extract")
 
def babel_extract():
 
    subprocess.run(
 
        [
 
            "pybabel",
 
            "extract",
 
            "-F",
 
            "babel.cfg",
 
            "-k",
c3l_membership/forms.py
Show inline comments
 
from datetime import date
 

	
 
from flask_babel import lazy_gettext
 
from flask_wtf import FlaskForm
 
from wtforms import (
 
    BooleanField,
 
    DateField,
 
    RadioField,
 
    StringField,
 
    SubmitField,
 
    ValidationError,
 
)
 
from wtforms.validators import Email, InputRequired, Length, Optional
 

	
 

	
 
class NotEqualTo:
 
    """
 
    Compares the values of two fields.
 
    :param fieldname:
 
        The name of the other field to compare to.
 
    :param message:
 
        Error message to raise in case of a validation error. Can be
 
        interpolated with `%(other_label)s` and `%(other_name)s` to provide a
 
        more helpful error.
 
    """
 

	
 
    def __init__(self, fieldname, message=None):
 
        self.fieldname = fieldname
 
        self.message = message
 

	
 
    def __call__(self, form, field):
 
        try:
 
            other = form[self.fieldname]
 
        except KeyError as exc:
 
            raise ValidationError(
 
                field.gettext("Invalid field name '%s'.") % self.fieldname
 
            ) from exc
 
        if field.data != other.data:
 
            return
 

	
 
        d = {
 
            "other_label": hasattr(other, "label")
 
            and other.label.text
 
            or self.fieldname,
 
            "other_name": self.fieldname,
 
        }
 
        message = self.message
 
        if message is None:
 
            message = field.gettext("Field must not be equal to %(other_name)s.")
 

	
 
        raise ValidationError(message % d)
 

	
 

	
 
class MembershipForm(FlaskForm):
 

	
 
    username = StringField(
 
        lazy_gettext("Username"),
 
        validators=[
 
            InputRequired(lazy_gettext("This field is required!")),
 
            Length(max=255),
 
        ],
 
    )
 

	
 
    email = StringField(
 
        lazy_gettext("E-Mail"),
 
        validators=[InputRequired(lazy_gettext("This field is required!")), Email()],
 
    )
 

	
 
    fullname = StringField(
 
        lazy_gettext("Full Name"),
 
        validators=[
 
            InputRequired(lazy_gettext("This field is required!")),
 
            Length(max=65536),
 
        ],
 
    )
 

	
 
    membership = RadioField(
 
        lazy_gettext("Membership Plan"),
 
        validators=[InputRequired(lazy_gettext("Please select one of the options!"))],
 
        choices=[
 
            (
 
                "regular",
 
                lazy_gettext(
 
                    "Regular membership - Membership with voting rights on the general assembly."
 
                ),
 
            ),
 
            (
 
                "supporting",
 
                lazy_gettext(
 
                    "Supporting membership - Membership without voting rights on the general assembly."
 
                ),
 
            ),
 
        ],
 
    )
 

	
 
    student = BooleanField(
 
        lazy_gettext(
 
            "I am a student and would like to have the reduced membership fees."
 
        ),
 
        validators=[
 
            Optional(),
 
            NotEqualTo(
 
                "starving",
 
                lazy_gettext(
 
                    "Student and Starving Hacker are mutually exclusive! Please select only one of them."
 
                ),
 
            ),
 
        ],
 
    )
 

	
 
    starving = BooleanField(
 
        lazy_gettext(
 
            "I am a starving hacker and cannot afford the membership! (Please get in touch with us at info@c3l.lu before filling out this membership form)"
 
        )
 
    )
 

	
 
    payment = RadioField(
 
        lazy_gettext("Payment Options"),
 
        validators=[InputRequired(lazy_gettext("Please select one of the options!"))],
 
    )
 

	
 
    birthday = DateField(lazy_gettext("Birthday"), validators=[Optional()])
 

	
 
    street = StringField(
 
        lazy_gettext("Nr., Street"),
 
        validators=[Optional(), Length(max=4000)],
 
    )
 

	
 
    zip = StringField(
 
        lazy_gettext("Postal Code"),
 
        validators=[Optional(), Length(max=30)],
 
    )
 

	
 
    city = StringField(
 
        lazy_gettext("City/Town"),
 
        validators=[Optional(), Length(max=500)],
 
    )
 

	
 
    state = StringField(
 
        lazy_gettext("State/County/Province"),
 
        validators=[Optional(), Length(max=500)],
 
    )
 

	
 
    country = StringField(
 
        lazy_gettext("Country"),
 
        validators=[Optional(), Length(max=500)],
 
    )
 

	
 
    terms = BooleanField(
 
        lazy_gettext(
 
            'By submitting this membership application, you agree to have read and understood the <a href="http://statutes.c3l.lu">statutes of the Chaos Computer Club Lëtzebuerg A.S.B.L.</a>.'
 
        ),
 
        validators=[InputRequired()],
 
    )
 

	
 
    minor_member = BooleanField(
 
        lazy_gettext(
 
            "I am under 18 years of age and have the approval of my legal representative."
 
        )
 
    )
 
    submit = SubmitField(lazy_gettext("Become a member"))
0 comments (0 inline, 0 general)