Skip to content

Commit

Permalink
refactor: split routes.py in an 'endpoint' module
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Feb 21, 2024
1 parent 4c87674 commit f8c0be0
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 205 deletions.
8 changes: 5 additions & 3 deletions web/b3desk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,19 @@ def internal_error(error):

def setup_endpoints(app):
with app.app_context():
import b3desk.routes
import b3desk.endpoints.public
import b3desk.endpoints.routes

app.register_blueprint(b3desk.routes.bp)
app.register_blueprint(b3desk.endpoints.public.bp)
app.register_blueprint(b3desk.endpoints.routes.bp)


def setup_oidc(app):
from flask_pyoidc.provider_configuration import ClientMetadata
from flask_pyoidc.provider_configuration import ProviderConfiguration

with app.app_context():
logout_url = url_for("routes.logout", _external=True)
logout_url = url_for("public.logout", _external=True)

user_provider_configuration = ProviderConfiguration(
issuer=app.config["OIDC_ISSUER"],
Expand Down
Empty file.
144 changes: 144 additions & 0 deletions web/b3desk/endpoints/public.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import requests
from flask import Blueprint
from flask import current_app
from flask import redirect
from flask import render_template
from flask import url_for

from .. import auth
from .. import cache
from ..session import get_current_user
from ..session import has_user_session
from ..templates.content import FAQ_CONTENT
from .routes import meeting_mailto_params


bp = Blueprint("public", __name__)


@cache.cached(
timeout=current_app.config["STATS_CACHE_DURATION"], key_prefix="meetings_stats"
)
def get_meetings_stats():
# TODO: do this asynchroneously
# Currently, the page needs to wait another network request in get_meetings_stats
# before it can be rendered. This is mitigated by caching though.

if not current_app.config["STATS_URL"]:
return None

response = requests.get(current_app.config["STATS_URL"])
if response.status_code != 200:
return None

try:
stats_array = response.content.decode(encoding="utf-8").split("\n")
stats_array = [row.split(",") for row in stats_array]
participant_count = int(stats_array[current_app.config["STATS_INDEX"]][1])
running_count = int(stats_array[current_app.config["STATS_INDEX"]][2])
except Exception:
return None

result = {"participantCount": participant_count, "runningCount": running_count}
return result


@bp.route("/")
def index():
if has_user_session():
return redirect(url_for("public.welcome"))
else:
return redirect(url_for("public.home"))


@bp.route("/home")
def home():
if has_user_session():
return redirect(url_for("public.welcome"))

stats = get_meetings_stats()
return render_template(
"index.html",
stats=stats,
mail_meeting=current_app.config["MAIL_MEETING"],
max_participants=current_app.config["MAX_PARTICIPANTS"],
)


@bp.route("/welcome")
@auth.oidc_auth("default")
def welcome():
user = get_current_user()
stats = get_meetings_stats()

return render_template(
"welcome.html",
stats=stats,
max_participants=current_app.config["MAX_PARTICIPANTS"],
can_create_meetings=user.can_create_meetings,
max_meetings_per_user=current_app.config["MAX_MEETINGS_PER_USER"],
meeting_mailto_params=meeting_mailto_params,
mailto=current_app.config["MAILTO_LINKS"],
quick_meeting=current_app.config["QUICK_MEETING"],
file_sharing=current_app.config["FILE_SHARING"],
clipboard=current_app.config["CLIPBOARD"],
)


@bp.route("/mentions_legales")
def mentions_legales():
return render_template(
"footer/mentions_legales.html",
service_title=current_app.config["SERVICE_TITLE"],
service_tagline=current_app.config["SERVICE_TAGLINE"],
)


@bp.route("/cgu")
def cgu():
return render_template(
"footer/cgu.html",
service_title=current_app.config["SERVICE_TITLE"],
service_tagline=current_app.config["SERVICE_TAGLINE"],
)


@bp.route("/donnees_personnelles")
def donnees_personnelles():
return render_template(
"footer/donnees_personnelles.html",
service_title=current_app.config["SERVICE_TITLE"],
service_tagline=current_app.config["SERVICE_TAGLINE"],
)


@bp.route("/accessibilite")
def accessibilite():
return render_template(
"footer/accessibilite.html",
service_title=current_app.config["SERVICE_TITLE"],
service_tagline=current_app.config["SERVICE_TAGLINE"],
)


@bp.route("/documentation")
def documentation():
if current_app.config["DOCUMENTATION_LINK"]["is_external"]:
return redirect(current_app.config["DOCUMENTATION_LINK"]["url"])
return render_template(
"footer/documentation.html",
)


@bp.route("/logout")
@auth.oidc_logout
def logout():
return redirect(url_for("public.index"))


@bp.route("/faq")
def faq():
return render_template(
"faq.html",
contents=FAQ_CONTENT,
)
Loading

0 comments on commit f8c0be0

Please sign in to comment.