import re import os import logging import zipfile import random import string from io import BytesIO from flask import ( Flask, render_template, render_template_string, request, redirect, send_file, make_response, session, ) from flask_wtf import FlaskForm from flask_babel import Babel, lazy_gettext as _ from wtforms import ( StringField, SelectField, SubmitField, TextAreaField, BooleanField, SelectMultipleField, HiddenField, ) from wtforms.validators import ( DataRequired, Optional, Regexp, URL, Length, ) YOLOGEN_VERSION = "0.11" LANGUAGES = {"en": _("English"), "fr": _("French")} ############################################################################### # App initialization, misc configs ############################################################################### logger = logging.getLogger() app = Flask(__name__, static_url_path="/static", static_folder="static") if app.config.get("DEBUG"): app.config["TEMPLATES_AUTO_RELOAD"] = True app.config["LANGUAGES"] = LANGUAGES app.config["GENERATOR_VERSION"] = YOLOGEN_VERSION # This is the secret key used for session signing app.secret_key = "".join(random.choice(string.ascii_lowercase) for i in range(32)) def get_locale(): return ( session.get("lang") or request.accept_languages.best_match(LANGUAGES.keys()) or "en" ) babel = Babel(app, locale_selector=get_locale) @app.context_processor def jinja_globals(): d = { "locale": get_locale(), } if app.config.get("DEBUG"): d["tailwind_local"] = open("static/tailwind-local.css").read() return d app.jinja_env.globals["is_hidden_field"] = lambda field: isinstance(field, HiddenField) @app.route("/lang/") def set_lang(lang=None): assert lang in app.config["LANGUAGES"].keys() session["lang"] = lang return make_response(redirect(request.referrer or "/")) ############################################################################### # Forms ############################################################################### class GeneralInfos(FlaskForm): app_id = StringField( _("Application identifier (id)"), description=_("Small caps and without spaces"), validators=[DataRequired(), Regexp("[a-z_1-9]+.*(?