From f40127b2648e619c6ce575700b696231954862b3 Mon Sep 17 00:00:00 2001 From: gwen Date: Thu, 14 Sep 2023 17:55:05 +0200 Subject: [PATCH] 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