diff --git a/app/dbinit.py b/app/dbinit.py index 272afb4..08dc147 100644 --- a/app/dbinit.py +++ b/app/dbinit.py @@ -48,10 +48,27 @@ def extract_princes_in_houses(): princes_in_houses = dict() for house in housecol.find(): housename = house['name'].lower() - query = list(actecol.aggregate([{"$match": {"house": housename}}, {'$group': {'_id': {'prince_name': '$prince_name', 'prince_code': '$prince_code'}}}])) + query = list(actecol.aggregate([{ + "$match": {"house": housename}}, + {'$group': {'_id': {'prince_name': '$prince_name', 'prince_code': '$prince_code'}} + }])) princes_in_houses[housename] = [pr['_id'] for pr in query] return princes_in_houses +def extract_prince_in_house(house, prince_code): + """ + :param house: sample: 'bourbon' + :param prince_code: sample: 'lo_i' + + :result: sample: {'prince_name': "Louis Ier d'Anjou", 'prince_code': 'lo_i'} + + """ + princes_in_houses = extract_princes_in_houses() + princes_in_house = princes_in_houses[house] + for pr in princes_in_house: + if pr['prince_code'] == prince_code: + return pr + def normalize_trigrams(trigram): """normalizes names, usefull for the uris routes @@ -62,37 +79,23 @@ def normalize_trigrams(trigram): return {unidecode(value):key for key, value in trigram.items()} # ______________________________________________________________________________ -# in memory storage extracted meta informations on the database +# in memory storage extracted 'meta' informations upon the database # TODO: if it takes too much time at launch, put it in something like # a `flask init` procedure helpers_dicts = helpers.find_one() house_trigram = helpers_dicts["house_trigram"] prince_bigram = helpers_dicts["prince_bigram"] - princes_in_houses = extract_princes_in_houses() -# normalized_trigrams + +""" +house_trigram sample: +{'Alencon': 'alb', 'Anjou': 'anj', 'Armagnac': 'arm', 'Bourbon': 'brb', 'Bretagne': 'bre', 'Bourgogne': 'brg', 'Berry': 'bry', 'Orleans': 'orl', 'Savoie': 'sav'} +""" house_trigram = normalize_trigrams(house_trigram) +""" +prince_bigram sample: +{'Agnes': 'agn', 'Alain': 'al', 'Amedee': 'am', 'Anne': 'ann', 'Arthur': 'ar', 'Bernard': 'be', 'Beatrice': 'bea', 'Blanche': 'bla', 'Bonne': 'bon', 'Catherine': 'cat', 'Charles': 'ch', 'Edouard': 'ed', 'Francois': 'fr', 'Francoise': 'fra', 'Jean': 'je', 'Jeanne': 'jea', 'Louis': 'lo', 'Marguerite': 'mag', 'Marie': 'mar', 'Philippe': 'ph', 'Pierre': 'pi', 'Rene': 're', 'Isabelle': 'isa', 'Yolande': 'yol'} +""" prince_bigram = normalize_trigrams(prince_bigram) -def bigram_prince(prince): - "Translates Charles_i -> ch_i" - name, number = prince.split("_") - return prince_bigram[name] + "_" + number - -# TODO: write tests in the datascience project `datascience/tests` -#print(bigram_prince("Agnes")) -#print(bigram_prince("Arthur")) -#print(bigram_prince("Bernard")) -#print(trigram_house("Anjou")) -#print(trigram_house("Orleans")) - -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]) - - diff --git a/app/helper.py b/app/helper.py index 29918c0..df8b00e 100644 --- a/app/helper.py +++ b/app/helper.py @@ -1,26 +1,56 @@ -"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 - - usercollection = mydb['usercollection'] - - @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 +"""helper functions (is it really usefull?) + +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]) + +def make_timeitem_from_filename(filename): + """ + "anj_isa_i_1441_08_05a" -> "1441_08_05a" + """ + trs_fname = filename.split('_') + return "_".join(trs_fname[3:]) + +#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 +# +# usercollection = mydb['usercollection'] +# +# @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 48253e4..103d9ae 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,6 @@ -"""main flask app routes +"""main flask app routes handlers -There are two types of routes, first the static routes : +There are two types of routes, first the 'static' routes : - / - /about @@ -9,7 +9,7 @@ There are two types of routes, first the static routes : - /privacy - /termsofservice -Then the dynamic routes : +Then the 'dynamic' (calculated) routes : - /actes - /actes/ @@ -23,8 +23,8 @@ import typing as t from flask import Blueprint, abort, render_template, request, send_from_directory import folium - -from .helper import find_one_or_404 +from pymongo import ASCENDING +from .helper import * from .dbinit import * main = Blueprint("main", __name__, url_prefix="/") @@ -67,13 +67,6 @@ def actes(house): """ # house in the store shall be in lower case, but let's force it, just in case house = house.lower() - # the nosql query below is equivalent to this code, which is more readable but slower: - #princes = [] - #for act in actecol.find({"house":house}): - # prince_name = act['prince_name'].capitalize() - # prince_code = act['prince_code'].capitalize() - # if (prince_name, prince_code) not in princes: - # princes.append((prince_name, prince_code)) # sample result: # [('Louis II de Bourbon', 'lo_ii'), ('Anne Dauphine', 'ann_i'), ('Agnès de Bourgogne', 'agn_i'), ('Charles Ier de Bourbon', 'ch_i')] # [('Agnès de Bourgogne', 'agn_i'), ('Anne Dauphine', 'ann_i'), ('Charles Ier de Bourbon', 'ch_i'), ('Louis II de Bourbon', 'lo_ii') @@ -86,22 +79,40 @@ 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**""" + """copora prince, *timeline* view + """ + prince_code = prince.lower() house = house.lower() - # prince bigram -> prince_code - # sample uri: /actes/Anjou/lo_i -> Louis_i -> Louis Ier d'Anjou - prc_big, prc_num = prince.split("_") - prince_code = prince_bigram[prc_big] + "_" + prc_num -# for item in prince_acte: -# print("\n\n", item) -#info = [(t.date_time, t.date, t.filename, t.analysis, t.prod_place_acte, -# t.diplo_type_acte, t.state_doc) -# ['1418-12-20', '1418, 20 décembre', 'anj_yo_i_1418_12_20a', "Donation à Antoine de la Salle d'une maison à Arles", "Château d'Anger", 'Lettres patentes', 'Copie'] -# ['1421-06-28', '1421, 28 juin', 'anj_yo_i_1421_06_28a', "Confirmation par Yolande, duchesse d'Anjou, du douaire assigné à sa belle-fille, Isabelle de Lorraine", 'NS', 'Lettres patentes', 'Original'] -# ['1442-02-24', '1442 (n. st.), 24 février', 'anj_yo_i_1442_02_24a', 'Pierre Throvan, secrétaire de la reine, nommé trésorier général de Provence et de Languedoc du 16 juillet au 31 octobre 1441, puis pendant trois ans à partir du 1er novembre 1441', 'Château de Saumur', 'Lettres patentes', 'Copie'] - return render_template("prince_corpus.html", houseS=house, duke_name=prince_name, - lst_id=prince_acte) - + prince_long_name = extract_prince_in_house(house, prince_code)['prince_name'] + query = list(actecol.aggregate([ + { + "$match": {"house": house, "prince_code": prince_code} + }, + { + "$sort": {"date_time": ASCENDING} + }, + { + '$group': {'_id': {'prince_name': '$prince_name', + 'prince_code': '$prince_code', + 'filename': '$filename', + 'date': '$date', + "date_time": "$date_time", + "analysis": '$analysis', + "place": "$place.name", + #"ref_acte": "$ref_acte" + "diplo_state": "$diplo_state", + "diplo_type": "$diplo_type" + } + } + } + ])) + transformed_query = [pr['_id'] for pr in query] + # constructing the dateitem + for trs in transformed_query: + trs['timeitem'] = make_timeitem_from_filename(trs['filename']) + + return render_template("prince_corpus.html", house=house, duke_name=prince_long_name, + prince_code=prince_code.capitalize(), actes=transformed_query) @main.route("/acte///") # don't put a slash at the end def acte(house=None, prince=None, dateitem=None): diff --git a/app/templates/prince_corpus.html b/app/templates/prince_corpus.html index aa82911..29a2fc3 100644 --- a/app/templates/prince_corpus.html +++ b/app/templates/prince_corpus.html @@ -4,25 +4,25 @@ -

Actes de {{duke_name[0][0]}}

+

Actes de {{duke_name}}

- {% for id in lst_id %} + {% for acte in actes %}
-

{{id[1]}}

+

{{ acte['date'] }}

-

{{id[3]}}.

+

{{ acte['analysis'] }}.

- {% if id[4] != 'NS' %} - {{id[4]}} + {% if acte['place'] != 'NS' %} + {{ acte['place'] }} {% endif %} - {{id[6]}} - {{id[5]}} + {{ acte['diplo_state'] }} + {{ acte['diplo_type'] }}

@@ -30,4 +30,4 @@ {% endfor %}
-{% endblock %} \ No newline at end of file +{% endblock %}