add helper

develop
gwen 2 years ago
parent cf28d5cdc5
commit 0024ee3231

@ -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,

@ -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']
"""

@ -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/<username>")
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

@ -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/<username>")
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"""

Loading…
Cancel
Save