|
|
"""main flask app routes handlers
|
|
|
|
|
|
There are two types of routes, first the 'static' routes :
|
|
|
|
|
|
- /
|
|
|
- /about
|
|
|
- /actes
|
|
|
- /contact
|
|
|
- /privacy
|
|
|
- /termsofservice
|
|
|
|
|
|
Then the 'dynamic' (calculated) routes :
|
|
|
|
|
|
- /actes
|
|
|
- /actes/<house>, sample: /actes/Anjou
|
|
|
- /actes/<house>/<prince>, sample: /actes/Anjou/isa_i
|
|
|
- /acte/<house>/<prince>/<dateitem>, sample: /acte/Anjou/Isabelle_i/1441_08_05a
|
|
|
- /geoloc
|
|
|
|
|
|
"""
|
|
|
import typing as t
|
|
|
|
|
|
|
|
|
from flask import Blueprint, render_template, request, url_for, redirect
|
|
|
import folium
|
|
|
from pymongo import ASCENDING
|
|
|
|
|
|
|
|
|
from .dbinit import *
|
|
|
from .helper import make_timeitem_from_filename
|
|
|
|
|
|
main = Blueprint("main", __name__, url_prefix="/")
|
|
|
|
|
|
# ______________________________________________________________________________
|
|
|
# routes
|
|
|
|
|
|
@main.route("/", methods=('GET', 'POST'))
|
|
|
def home():
|
|
|
"""home route"""
|
|
|
if request.method == 'POST':
|
|
|
search = request.form['search']
|
|
|
return render_template("form2.html", search=search)
|
|
|
return render_template("home.html")
|
|
|
|
|
|
|
|
|
@main.route("/about/", methods=('GET', 'POST'))
|
|
|
def about():
|
|
|
"""about route"""
|
|
|
return render_template("about.html")
|
|
|
|
|
|
|
|
|
@main.route("/actes/")
|
|
|
def corpora_all():
|
|
|
"""copora all
|
|
|
|
|
|
lists houses
|
|
|
sample_house_names = ["Bourbon", "Berry", "Anjou", ...]
|
|
|
"""
|
|
|
houses = list(housecol.find())
|
|
|
return render_template("corpora_all.html", houses=houses)
|
|
|
|
|
|
|
|
|
@main.route("/actes/<house>")
|
|
|
def actes(house):
|
|
|
"""*actes* route, sample route: '/actes/Bourbon' or '/actes/Berry'
|
|
|
|
|
|
shows the princes in the selected house
|
|
|
|
|
|
:param: the house in the url is the house name
|
|
|
with a capital letter at the beginning
|
|
|
example: `house = "Berry"`
|
|
|
"""
|
|
|
# house in the store shall be in lower case, but let's force it, just in case
|
|
|
house = house.lower()
|
|
|
# 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')
|
|
|
princes = princes_in_houses[house]
|
|
|
|
|
|
# TODO : modify the jinja template's design in order to suppress this ugly line
|
|
|
princes = [(prc['prince_name'], prc['prince_code']) for prc in princes]
|
|
|
return render_template("corpus.html", house=house.capitalize(), princes=princes)
|
|
|
|
|
|
|
|
|
@main.route("/actes/<house>/<prince>")
|
|
|
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()
|
|
|
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]
|
|
|
invert_prince_bigram = {val: key for key, val in prince_bigram.items()}
|
|
|
#print(invert_prince_bigram)
|
|
|
|
|
|
# constructing the dateitem
|
|
|
for trs in transformed_query:
|
|
|
trs['dateitem'] = make_timeitem_from_filename(trs['filename'])
|
|
|
bigram, number = trs['prince_code'].split('_')
|
|
|
long_prince_bigram = inverted_prince_bigram(bigram) + '_' + number
|
|
|
|
|
|
|
|
|
return render_template("prince_corpus.html", house=house, duke_name=prince_long_name,
|
|
|
prince_name=long_prince_bigram.capitalize(), actes=transformed_query)
|
|
|
|
|
|
@main.route("/acte/<house>/<prince>/<dateitem>")
|
|
|
def acte(house=None, prince=None, dateitem=None):
|
|
|
"""specific prince's acte view
|
|
|
|
|
|
:params: - house
|
|
|
- prince
|
|
|
- date + item (sample: 1441_08_05a)
|
|
|
:url location sample: /acte/Anjou/Isabelle_i/1441_08_05a
|
|
|
|
|
|
url transcription samples:
|
|
|
/acte/Anjou/Isabelle_i/1441_08_05a -> anj_isa_i_1441_08_05a
|
|
|
/acte/Bourbon/Anne_i/1388_09_15a -> brb_ann_i_1388_09_15a
|
|
|
"""
|
|
|
filestem = make_acteid_from_route(house, prince, dateitem)
|
|
|
result = actecol.find_one({'_id': filestem})
|
|
|
return render_template("acte.html", house=house, prince=result.get('prince_code'),
|
|
|
#infos=None, place=None, doc=None, arch=None,
|
|
|
#diplo=diplo_t[0].replace("_", " "), state=state[0],
|
|
|
output_doc=result.get('xmlcontent'), name_prince=result.get("prince_name"),
|
|
|
folium=result.get("folium"),
|
|
|
transcribers=result.get('transcribers'))
|
|
|
|
|
|
@main.route("/geolocalisation")
|
|
|
def geolocalisation():
|
|
|
"global folium/leaflet map"
|
|
|
m = folium.Map(location=[46.603354, 1.888334], zoom_start=6)
|
|
|
for result in actecol.find():
|
|
|
place = result['place']
|
|
|
if place.get('latitude') is not None:
|
|
|
folium.Marker(
|
|
|
location=[place['latitude'], place['longitude']],
|
|
|
popup=place['name'],
|
|
|
icon=folium.Icon(color='lightgray', icon="circle", prefix='fa')
|
|
|
#icon=folium.Icon(color='lightgray', icon='home', prefix='fa')
|
|
|
).add_to(m)
|
|
|
geolocalisation = m._repr_html_()
|
|
|
return render_template("map.html", geolocalisation=geolocalisation)
|
|
|
#geolocalisation = folium_map.find_one()
|
|
|
#geolocalisation = geolocalisation['globalmap']
|
|
|
#return render_template("map.html", geolocalisation=geolocalisation)
|
|
|
|
|
|
@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é")
|