From b15df2d4c5aa73fbb271edcc03fc3f61db3bab1d Mon Sep 17 00:00:00 2001 From: gwen Date: Wed, 20 Sep 2023 15:00:36 +0200 Subject: [PATCH] refactoring --- app/dbinit.py | 39 +++++++++++++++++++++++++++------------ app/helper.py | 29 +++++++++-------------------- app/routes.py | 7 +++++-- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/app/dbinit.py b/app/dbinit.py index 08dc147..b90133b 100644 --- a/app/dbinit.py +++ b/app/dbinit.py @@ -1,19 +1,17 @@ """ Mongodb storage init -- collections creation -- helper queries +- collections queries initialization TODO : maybe put the mongodb connector into the flask app object with the pymongo library. """ import urllib.parse -from unidecode import unidecode # to remove accents in house and prince names in the urls from pymongo import MongoClient from .config import dbadmin, dbpassword, server_ip - +from .helper import normalize_trigrams # ______________________________________________________________________________ # database connexion @@ -37,6 +35,11 @@ helpers = actesdb["helpers"] # ______________________________________________________________________________ # storage extractor utilities +def make_acteid_from_route(house=None, prince=None, date_and_item=None): + "/acte/Anjou/Isabelle_i/1441_08_05a -> anj_isa_i_1441_08_05a" + return "_".join([trigram_house(house), bigram_prince(prince), date_and_item]) + + def extract_princes_in_houses(): """Extracts all princes from a house by queries in the storage, (not by using csv metadatas) @@ -70,14 +73,6 @@ def extract_prince_in_house(house, prince_code): return pr -def normalize_trigrams(trigram): - """normalizes names, usefull for the uris routes - - sample: Alençon -> Alencon - Orléans -> Orleans - """ - return {unidecode(value):key for key, value in trigram.items()} - # ______________________________________________________________________________ # in memory storage extracted 'meta' informations upon the database # TODO: if it takes too much time at launch, put it in something like @@ -99,3 +94,23 @@ prince_bigram sample: """ prince_bigram = normalize_trigrams(prince_bigram) + +def inverted_prince_bigram(bigram): + "Translates ch -> Charles" + inverted_prince_bigram = {value: key for key, value in prince_bigram.items()} + return inverted_prince_bigram[bigram] + + +def bigram_prince(prince): + "Translates Charles_i -> ch_i" + name, number = prince.split("_") + return prince_bigram[name] + "_" + number + + +def trigram_house(house): + return house_trigram[house] + + + + + diff --git a/app/helper.py b/app/helper.py index df8b00e..1f691c6 100644 --- a/app/helper.py +++ b/app/helper.py @@ -2,26 +2,7 @@ TODO: maybe all these calculations are to be put in the db storage """ - -def inverted_prince_bigram(bigram): - "Translates ch -> Charles" - inverted_prince_bigram = {value: key for key, value in prince_bigram.items()} - return inverted_prince_bigram[bigram] - - -def bigram_prince(prince): - "Translates Charles_i -> ch_i" - name, number = prince.split("_") - return prince_bigram[name] + "_" + number - - -def trigram_house(house): - return house_trigram[house] - - -def make_acteid_from_route(house=None, prince=None, date_and_item=None): - "/acte/Anjou/Isabelle_i/1441_08_05a -> anj_isa_i_1441_08_05a" - return "_".join([trigram_house(house), bigram_prince(prince), date_and_item]) +from unidecode import unidecode # to remove accents in house and prince names in the urls def make_timeitem_from_filename(filename): """ @@ -30,6 +11,14 @@ def make_timeitem_from_filename(filename): trs_fname = filename.split('_') return "_".join(trs_fname[3:]) +def normalize_trigrams(trigram): + """normalizes names, usefull for the uris routes + + sample: Alençon -> Alencon + Orléans -> Orleans + """ + return {unidecode(value):key for key, value in trigram.items()} + #from flask import abort # #def find_one_or_404(collection, **kwargs): diff --git a/app/routes.py b/app/routes.py index 103d9ae..2a11a8b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -24,8 +24,9 @@ import typing as t from flask import Blueprint, abort, render_template, request, send_from_directory import folium from pymongo import ASCENDING -from .helper import * + from .dbinit import * +from .helper import make_timeitem_from_filename main = Blueprint("main", __name__, url_prefix="/") @@ -57,7 +58,7 @@ def corpora_all(): @main.route("/actes/") # dont put a slash at the end def actes(house): - """actes route + """*actes* route, sample route: '/actes/Bourbon' or '/actes/Berry' shows the princes in the selected house @@ -80,6 +81,8 @@ def actes(house): @main.route("/actes//") # don't put a slash at the end def prince_corpus(house=None, prince=None): """copora prince, *timeline* view + + sample route: '/actes/Berry/je_i' or '/actes/Anjou/lo_i' """ prince_code = prince.lower() house = house.lower()