From cf5cf93763c9fafbfc4a5c488035e03101f6d5ac Mon Sep 17 00:00:00 2001 From: gwen Date: Tue, 12 Sep 2023 15:01:32 +0200 Subject: [PATCH 1/7] index page --- app/app.py | 8 +++--- app/routes.py | 56 +++++++++++++++++++++++++++++++++++++++++ app/templates/home.html | 2 +- 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 app/routes.py diff --git a/app/app.py b/app/app.py index 26f198f..4d6f3b9 100644 --- a/app/app.py +++ b/app/app.py @@ -5,6 +5,8 @@ from flask import Flask from flask import render_template import werkzeug +from .routes import main + APPPATH = os.path.dirname(os.path.abspath(__file__)) templates = os.path.join(APPPATH, "templates") static = os.path.join(APPPATH, "static") @@ -27,9 +29,5 @@ def page_not_found(e: werkzeug.exceptions.HTTPException) -> t.Tuple[t.Text, int] def internal_server_error(e: werkzeug.exceptions.HTTPException) -> t.Tuple[t.Text, int]: return render_template("500.html", title="Erreur interne du serveur"), 500 +app.register_blueprint(main) -### DEBUG -@app.route('/') -@app.route('/index') -def hello_world(): - return "

Hello, World!

" diff --git a/app/routes.py b/app/routes.py new file mode 100644 index 0000000..1d276eb --- /dev/null +++ b/app/routes.py @@ -0,0 +1,56 @@ +import typing as t + +from flask import Blueprint, abort, render_template, request, send_from_directory + +main = Blueprint("main", __name__, url_prefix="/") + +@main.route("/") +def home(): + """home route""" + return render_template("home.html") + + +@main.route("/about/") +def about(): + """home route""" + return render_template("about.html") + + +@main.route("/actes/") +def corpora_all(): + """copora all route""" + return "hello" +# info = [(t.date, t.filename, t.analysis, t.prod_place_acte, +# t.diplo_type_acte, t.state_doc) for t in Acte.select()] +# prince_acte = [] +# return render_template("corpora_all.html") + + +@main.route("/actes/") # dont put a slash at the end +def actes(house): + """actes route""" + return "hello" +# house_q = [t.id_house for t in House.select().where( +# House.house_label == house)] +# prince_q = [(t.name_indiv, t.id_indiv) for t in Individual.select().where( +# (Individual.role_indiv == "prince") +# &(Individual.house_indiv == house_q[0]))] +# return render_template("corpus.html", house=house, princes=prince_q) + + +@main.route("/contact") +def contact() -> t.Text: + """Displays the Contact page""" + return render_template("contact.html", title="Contact") + + +@main.route("/termsofservice") +def terms() -> t.Text: + """Displaysthe T&C page.""" + return render_template("terms.html", title="Mentions légales") + + +@main.route("/privacy") +def privacy() -> t.Text: + """Displays the privacy policy page.""" + return render_template("privacy.html", title="Politique de confidentialité") diff --git a/app/templates/home.html b/app/templates/home.html index bcf38e0..83a745f 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -58,4 +58,4 @@ -{% endblock %} \ No newline at end of file +{% endblock %} From 1e82636d9f4f99cdb37d263a0339f79489b1d08f Mon Sep 17 00:00:00 2001 From: gwen Date: Tue, 12 Sep 2023 16:45:56 +0200 Subject: [PATCH 2/7] begin sluggify the routes --- app/routes.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/app/routes.py b/app/routes.py index 1d276eb..9fcd83b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -19,25 +19,39 @@ def about(): @main.route("/actes/") def corpora_all(): """copora all route""" - return "hello" -# info = [(t.date, t.filename, t.analysis, t.prod_place_acte, -# t.diplo_type_acte, t.state_doc) for t in Acte.select()] -# prince_acte = [] -# return render_template("corpora_all.html") + # FIXME rendre ce template dynamique, if faut récuperer les maisons depuis la base + # FIXME récupérer les maisons depuis la base + # house_label = ["Bourbon", "Berry", "Anjou"] + return render_template("corpora_all.html") @main.route("/actes/") # dont put a slash at the end def actes(house): """actes route""" - return "hello" -# house_q = [t.id_house for t in House.select().where( -# House.house_label == house)] -# prince_q = [(t.name_indiv, t.id_indiv) for t in Individual.select().where( -# (Individual.role_indiv == "prince") -# &(Individual.house_indiv == house_q[0]))] -# return render_template("corpus.html", house=house, princes=prince_q) + #house = "Berry" + # FIXME faire des princes correspondant à la maison (house) sélectionnée + # FIXME : supprimer ces ids + # Corpus > Bourbon + princes = [("Charles Ier de Bourbon", 36), ("Agnès de Bourgogne", 38), + ("Marie de Berry", 39)] + return render_template("corpus.html", house=house, princes=princes) +# FIXME SLUGGIFY the house +# FIXME supprimer cet id de prince et remplacer par le slug du prince +@main.route("/actes//") # don't put a slash at the end +def prince_corpus(house=None, prince=None): + """copora prince, **timeline view**""" + return "FIXME" + + +# FIXME : slugigy this route (remove acte_id) (prince and acte) +# FIXME: isn't it /acte/house/prince/slug instead of /acte/prince/slug ? +@main.route("/acte//") # don't put a slash at the end +def acte(house=None, prince=None, acte_id=None): + """specific prince's acte""" + return "FIXME" + @main.route("/contact") def contact() -> t.Text: """Displays the Contact page""" From f40127b2648e619c6ce575700b696231954862b3 Mon Sep 17 00:00:00 2001 From: gwen Date: Thu, 14 Sep 2023 17:55:05 +0200 Subject: [PATCH 3/7] database query is working --- app/app.py | 6 ++--- app/routes.py | 49 +++++++++++++++++++++++++++++----- app/templates/corpora_all.html | 12 ++++----- requirements.txt | 14 ++++------ 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/app/app.py b/app/app.py index 4d6f3b9..a0f8b50 100644 --- a/app/app.py +++ b/app/app.py @@ -3,7 +3,7 @@ import os from flask import Flask from flask import render_template -import werkzeug +#import werkzeug from .routes import main @@ -21,12 +21,12 @@ app = Flask( @app.errorhandler(404) -def page_not_found(e: werkzeug.exceptions.HTTPException) -> t.Tuple[t.Text, int]: +def page_not_found(e): return render_template("404.html", title="Page non trouvée"), 404 @app.errorhandler(500) -def internal_server_error(e: werkzeug.exceptions.HTTPException) -> t.Tuple[t.Text, int]: +def internal_server_error(e): return render_template("500.html", title="Erreur interne du serveur"), 500 app.register_blueprint(main) diff --git a/app/routes.py b/app/routes.py index 9fcd83b..f4d5dc5 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,6 +4,41 @@ from flask import Blueprint, abort, render_template, request, send_from_director main = Blueprint("main", __name__, url_prefix="/") +from pymongo import MongoClient + +import urllib.parse + +username = urllib.parse.quote_plus('dbadmin') +password = urllib.parse.quote_plus('BrOp48la%Mrops') +myclient = MongoClient('mongodb://%s:%s@149.202.41.75:27017' % (username, password)) +mydb = myclient["actesdb"] +dbhouse = mydb["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""" @@ -12,17 +47,19 @@ def home(): @main.route("/about/") def about(): - """home route""" + """about route""" return render_template("about.html") @main.route("/actes/") def corpora_all(): - """copora all route""" - # FIXME rendre ce template dynamique, if faut récuperer les maisons depuis la base - # FIXME récupérer les maisons depuis la base - # house_label = ["Bourbon", "Berry", "Anjou"] - return render_template("corpora_all.html") + """copora all + + lists houses + sample_house_names = ["Bourbon", "Berry", "Anjou", ...] + """ + houses = list(dbhouse.find()) + return render_template("corpora_all.html", houses=houses) @main.route("/actes/") # dont put a slash at the end diff --git a/app/templates/corpora_all.html b/app/templates/corpora_all.html index f932c94..8e4208c 100644 --- a/app/templates/corpora_all.html +++ b/app/templates/corpora_all.html @@ -4,9 +4,9 @@

Corpus

Corpus disponibles :

- -{% endblock %} \ No newline at end of file + {% for house in houses %} +
  • Actes des ducs et duchesses {{ house.particle }}
  • + + {% endfor %} + +{% endblock %} diff --git a/requirements.txt b/requirements.txt index 022a5bd..cc364e7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,25 +3,21 @@ click==8.1.3 dnspython==2.4.2 email-validator==2.0.0.post2 -Flask==2.2.2 -flask-mongoengine==1.0.0 +Flask==2.3.3 Flask-WTF==1.1.1 idna==3.4 gunicorn==21.2.0 importlib-metadata==5.0.0 itsdangerous==2.1.2 Jinja2==3.1.2 -# FIXME remove lxml later -# lxml==4.9.1 -MarkupSafe==2.1.1 +MarkupSafe==2.1.3 packaging==23.1 -mongoengine==0.27.0 -# FIXME remove pewee later -# peewee==3.15.3 +#mongoengine==0.27.0 pymongo==4.5.0 #soupsieve==2.3.2.post1 #tqdm==4.64.1 typing==3.7.4.3 -Werkzeug==2.2.2 +Werkzeug==2.3.7 +Flask-WTF==1.1.1 WTForms==3.0.1 zipp==3.9.0 From 1f577c04bdd9a222fdf77731f3a87a10affdc637 Mon Sep 17 00:00:00 2001 From: gwen Date: Thu, 14 Sep 2023 18:03:46 +0200 Subject: [PATCH 4/7] add config.py --- .gitignore | 2 +- app/config.py | 19 +++++++++++++++++++ app/routes.py | 12 ++++++------ requirements.txt | 2 ++ 4 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 app/config.py diff --git a/.gitignore b/.gitignore index 29eeee3..e6b4e53 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .venv/* - +params.yaml diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..5775352 --- /dev/null +++ b/app/config.py @@ -0,0 +1,19 @@ +"""Application's configuration file""" + +from pathlib import Path + +# path configuration +# let's guess that :file:`config.py` is located here : :file:`{rootpath}/webapp/` +_here = Path(__file__).resolve().parent + +rootpath = _here.parent +"root project directory" + +from yaml import safe_load + +# loads database credentials +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'] diff --git a/app/routes.py b/app/routes.py index f4d5dc5..7eaa67c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,15 +1,15 @@ import typing as t +import urllib.parse from flask import Blueprint, abort, render_template, request, send_from_directory - -main = Blueprint("main", __name__, url_prefix="/") - from pymongo import MongoClient -import urllib.parse +from .config import dbadmin, dbpassword + +main = Blueprint("main", __name__, url_prefix="/") -username = urllib.parse.quote_plus('dbadmin') -password = urllib.parse.quote_plus('BrOp48la%Mrops') +username = urllib.parse.quote_plus(dbadmin) +password = urllib.parse.quote_plus(dbpassword) myclient = MongoClient('mongodb://%s:%s@149.202.41.75:27017' % (username, password)) mydb = myclient["actesdb"] dbhouse = mydb["house"] diff --git a/requirements.txt b/requirements.txt index cc364e7..88a7665 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,3 +21,5 @@ Werkzeug==2.3.7 Flask-WTF==1.1.1 WTForms==3.0.1 zipp==3.9.0 +pyyaml-6.0.1 + From cf28d5cdc5e67665d5c0e2fbf62a62556ed7096a Mon Sep 17 00:00:00 2001 From: gwen Date: Thu, 14 Sep 2023 21:23:39 +0200 Subject: [PATCH 5/7] configuration in params --- app/config.py | 1 + app/routes.py | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/config.py b/app/config.py index 5775352..b661201 100644 --- a/app/config.py +++ b/app/config.py @@ -17,3 +17,4 @@ 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'] diff --git a/app/routes.py b/app/routes.py index 7eaa67c..8a8fb51 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,15 +4,17 @@ import urllib.parse from flask import Blueprint, abort, render_template, request, send_from_directory from pymongo import MongoClient -from .config import dbadmin, dbpassword +from .config import dbadmin, dbpassword, server_ip + main = Blueprint("main", __name__, url_prefix="/") +# database connexion username = urllib.parse.quote_plus(dbadmin) password = urllib.parse.quote_plus(dbpassword) -myclient = MongoClient('mongodb://%s:%s@149.202.41.75:27017' % (username, password)) -mydb = myclient["actesdb"] -dbhouse = mydb["house"] +dbclient = MongoClient(f'mongodb://{username}:{password}@{server_ip}:27017') +actesdb = dbclient["actesdb"] +housecol = actesdb["house"] # (unproudly borrowed from the flask-pymongo's helpers :) @@ -58,7 +60,7 @@ def corpora_all(): lists houses sample_house_names = ["Bourbon", "Berry", "Anjou", ...] """ - houses = list(dbhouse.find()) + houses = list(housecol.find()) return render_template("corpora_all.html", houses=houses) From 0024ee323195444cd5ffefebfc1c0e0e5e9df4a3 Mon Sep 17 00:00:00 2001 From: gwen Date: Thu, 14 Sep 2023 22:05:17 +0200 Subject: [PATCH 6/7] 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""" From 7537386a7dea86d9cd20e29403bb86bfa201087e Mon Sep 17 00:00:00 2001 From: gwen Date: Fri, 15 Sep 2023 10:34:59 +0200 Subject: [PATCH 7/7] helper --- app/helper.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/helper.py b/app/helper.py index dfb37e8..29918c0 100644 --- a/app/helper.py +++ b/app/helper.py @@ -11,6 +11,8 @@ def find_one_or_404(collection, **kwargs): .. code-block:: python + usercollection = mydb['usercollection'] + @app.route("/user/") def user_profile(username): userfound = find_one_or_404(usercollection, {"_id": username})