From 0024ee323195444cd5ffefebfc1c0e0e5e9df4a3 Mon Sep 17 00:00:00 2001 From: gwen Date: Thu, 14 Sep 2023 22:05:17 +0200 Subject: [PATCH] add helper --- app/app.py | 1 - app/config.py | 14 ++++++++------ app/helper.py | 24 ++++++++++++++++++++++++ app/routes.py | 25 +------------------------ 4 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 app/helper.py diff --git a/app/app.py b/app/app.py index a0f8b50..04b3add 100644 --- a/app/app.py +++ b/app/app.py @@ -11,7 +11,6 @@ APPPATH = os.path.dirname(os.path.abspath(__file__)) templates = os.path.join(APPPATH, "templates") static = os.path.join(APPPATH, "static") - app = Flask( "Princely-Acts", template_folder=templates, diff --git a/app/config.py b/app/config.py index b661201..9ca0549 100644 --- a/app/config.py +++ b/app/config.py @@ -3,18 +3,20 @@ from pathlib import Path # path configuration -# let's guess that :file:`config.py` is located here : :file:`{rootpath}/webapp/` +# let's guess that :file:`config.py` is located here : :file:`{rootpath}/app/` _here = Path(__file__).resolve().parent - rootpath = _here.parent "root project directory" from yaml import safe_load -# loads database credentials +# loads database credentials in the globals config module local_params_file = rootpath / "params.yaml" with open(local_params_file, 'r') as file_handle: params_content = safe_load(file_handle) - dbadmin = params_content['dbadmin'] - dbpassword = params_content['dbpassword'] - server_ip = params_content['server_ip'] + globals().update(params_content) +""" +# dbadmin = params_content['dbadmin'] +# dbpassword = params_content['dbpassword'] +# server_ip = params_content['server_ip'] +""" diff --git a/app/helper.py b/app/helper.py new file mode 100644 index 0000000..dfb37e8 --- /dev/null +++ b/app/helper.py @@ -0,0 +1,24 @@ +"helper functions" + +from flask import abort + +def find_one_or_404(collection, **kwargs): + """Find a single document or raise a 404. + + This is like :meth:`~pymongo.collection.Collection.find_one`, but + rather than returning ``None``, cause a 404 Not Found HTTP status + on the request. + + .. code-block:: python + + @app.route("/user/") + def user_profile(username): + userfound = find_one_or_404(usercollection, {"_id": username}) + return render_template("user.html", + user=userfound) + """ + found = collection.find_one(**kwargs) + if found is None: + abort(404) + return found + diff --git a/app/routes.py b/app/routes.py index 8a8fb51..9460cc7 100644 --- a/app/routes.py +++ b/app/routes.py @@ -5,6 +5,7 @@ from flask import Blueprint, abort, render_template, request, send_from_director from pymongo import MongoClient from .config import dbadmin, dbpassword, server_ip +from .helper import find_one_or_404 main = Blueprint("main", __name__, url_prefix="/") @@ -17,30 +18,6 @@ actesdb = dbclient["actesdb"] housecol = actesdb["house"] -# (unproudly borrowed from the flask-pymongo's helpers :) -from flask import abort -def find_one_or_404(self, *args, **kwargs): - """Find a single document or raise a 404. - - This is like :meth:`~pymongo.collection.Collection.find_one`, but - rather than returning ``None``, cause a 404 Not Found HTTP status - on the request. - - .. code-block:: python - - @app.route("/user/") - def user_profile(username): - user = mongo.db.users.find_one_or_404({"_id": username}) - return render_template("user.html", - user=user) - - """ - found = self.find_one(*args, **kwargs) - if found is None: - abort(404) - return found - - @main.route("/") def home(): """home route"""