From 6f1bcc3db2c8ee807b68fa8a47a1a8cb7aabcc48 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Thu, 6 Nov 2025 00:48:00 +0000 Subject: Create a create_app helper to be used from wsgi.py --- halfnarp2.py | 54 +++++++++++++++++++++++++++++++----------------------- static/faq.html | 4 ++-- static/halfnarp.js | 2 +- wsgi.py | 6 +++--- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/halfnarp2.py b/halfnarp2.py index 5ac9405..24d099a 100755 --- a/halfnarp2.py +++ b/halfnarp2.py @@ -1,6 +1,6 @@ #!venv/bin/python -from flask import Flask, render_template, jsonify, request, abort, send_file, url_for +from flask import Flask, Blueprint, render_template, jsonify, request, abort, send_file, url_for from flask_sqlalchemy import SQLAlchemy from flask_cors import CORS from lxml import etree @@ -13,10 +13,9 @@ from datetime import datetime, time, timedelta from html_sanitizer import Sanitizer from hashlib import sha256 -app = Flask(__name__) +bp = Blueprint("main", __name__) db = SQLAlchemy() - class TalkPreference(db.Model): """A preference of halfnarp frontend. An array of strings""" @@ -26,17 +25,17 @@ class TalkPreference(db.Model): """ -@app.route("/") +@bp.route("/") def root(): return render_template("index.html") """ -@app.route("/-/talkpreferences", methods=["GET"]) +@bp.route("/-/talkpreferences", methods=["GET"]) def sessions(): return send_file("var/talks_local", mimetype="application/json") -@app.route("/-/talkpreferences/", methods=["GET"]) +@bp.route("/-/talkpreferences/", methods=["GET"]) def get_own_preferences(uid): pref = db.session.get(TalkPreference, uid) if pref == None: @@ -57,7 +56,7 @@ def get_own_preferences(uid): ) -@app.route("/-/talkpreferences/", methods=["POST"]) +@bp.route("/-/talkpreferences/", methods=["POST"]) def store_preferences(): try: content = request.json @@ -92,7 +91,7 @@ def store_preferences(): ) -@app.route("/-/talkpreferences/", methods=["POST", "PUT"]) +@bp.route("/-/talkpreferences/", methods=["POST", "PUT"]) def update_preferences(uid): pref = db.session.get(TalkPreference, uid) if pref == None: @@ -105,7 +104,7 @@ def update_preferences(uid): return jsonify({"uid": pref.uid, "hashed_uid": pref.public_uid}) -@app.route("/-/talkpreferences/public/", methods=["GET"]) +@bp.route("/-/talkpreferences/public/", methods=["GET"]) def get_preferences(public_uid): pref = ( db.session.query(TalkPreference) @@ -238,7 +237,7 @@ def export_prefs(config): print("[]]") -if __name__ == "__main__": +def parse_args(): parser = ArgumentParser(description="halfnarp2") parser.add_argument( "-i", @@ -257,20 +256,20 @@ if __name__ == "__main__": parser.add_argument( "-c", "--config", help="Config file location", default="./config.json" ) - args = parser.parse_args() + return parser.parse_args() - with open(args.config, mode="r", encoding="utf-8") as json_file: - config = json.load(json_file) - config["pretalx-api-url"] = ( - config["pretalx-url"] + "api/events/" + config["pretalx-conference"] - ) - app.config["SQLALCHEMY_DATABASE_URI"] = config.get( +def create_app(config_file="./config.json"): + app = Flask(__name__) + with open(config_file, mode="r", encoding="utf-8") as config_file: + app.config["halfnarp"] = json.load(config_file) + + app.config["SQLALCHEMY_DATABASE_URI"] = app.config["halfnarp"].get( "database-uri", "sqlite:///test.db" ) app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False - app.config["SERVER_NAME"] = config.get("server-name", "localhost") - app.config["SECRET_KEY"] = config.get("server-secret", "") + app.config["SERVER_NAME"] = app.config["halfnarp"].get("server-name", "localhost") + app.config["SECRET_KEY"] = app.config["halfnarp"].get("server-secret", "") if app.config["SECRET_KEY"] == "": print("You must set the server-secret in your config.json") @@ -281,15 +280,24 @@ if __name__ == "__main__": CORS() db.init_app(app) + app.register_blueprint(bp) + return app +if __name__ == "__main__": + args = parse_args() + app = create_app(args.config) + + app.config["halfnarp"]["pretalx-api-url"] = ( + app.config["halfnarp"]["pretalx-url"] + "api/events/" + app.config["halfnarp"]["pretalx-conference"] + ) with app.app_context(): db.create_all() if args.pretalx_import: - fetch_talks(config) + fetch_talks(app.config["halfnarp"]) elif args.fullnarp_export: - export_prefs(config) + export_prefs(app.config["halfnarp"]) else: app.run( - host=config.get("host", "127.0.0.1"), - port=int(config.get("port", "8080")), + host=app.config["halfnarp"].get("host", "127.0.0.1"), + port=int(app.config["halfnarp"].get("port", "8080")), ) diff --git a/static/faq.html b/static/faq.html index aefd03d..a90b4b5 100644 --- a/static/faq.html +++ b/static/faq.html @@ -7,7 +7,7 @@ -
The 38C3 halfnarp FAQ
+
The 39C3 halfnarp FAQ
Q: What is halfnarp?
@@ -18,7 +18,7 @@
Q: How does it work?
-

A: 38C3 Fahrplan is curated by six teams each responsible for one track. By default, lectures are sorted by these tracks.

+

A: 39C3 Fahrplan is curated by six teams each responsible for one track. By default, lectures are sorted by these tracks.

  • On a desktop browser, hovering over an event’s description reveals the full abstract. Clicking on an event adds or removes events to/from your favorites list – they turn green.
  • On mobile browsers, tapping an event once selects it and reveals the whole content. Tapping a selected event adds or removes events to/from your favorites list – they turn green.
  • diff --git a/static/halfnarp.js b/static/halfnarp.js index b7f4a45..5cd1ef3 100644 --- a/static/halfnarp.js +++ b/static/halfnarp.js @@ -93,7 +93,7 @@ function redraw_calendar(myuid, ids) { calendar += 'DTSTART:' + start.toISOString().replace(/-|;|:|\./g, '').replace(/...Z$/, 'Z') + '\r\n'; calendar += 'DURATION:PT' + item.duration + 'S\r\n'; calendar += 'LOCATION:' + item.room_name + '\r\n'; - calendar += 'URL:http://events.ccc.de/congress/2023/Fahrplan/events/' + item.event_id + '.html\r\n'; + calendar += 'URL:http://events.ccc.de/congress/2025/Fahrplan/events/' + item.event_id + '.html\r\n'; calendar += 'SUMMARY:' + item.title + '\r\n'; calendar += 'DESCRIPTION:' + item.abstract.replace(/\n|\r/g, ' ') + '\r\n'; // console.log( 'id:' + id + ' ' + all_events[id] ); diff --git a/wsgi.py b/wsgi.py index 2ad28d9..db217db 100644 --- a/wsgi.py +++ b/wsgi.py @@ -1,4 +1,4 @@ -from halfnarp2 import app +from halfnarp2 import create_app -if __name__ == "__main__": - app.run(debug=False) +app = create_app("config.json") +#app.run(debug=False) -- cgit v1.2.3