Refactorisation utils

This commit is contained in:
Colin Maudry
2026-04-18 19:01:07 +02:00
parent e4e1438220
commit af3b3464e4
5 changed files with 126 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
import logging
import os
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
DEVELOPMENT = os.getenv("DEVELOPMENT", "False").lower() == "true"
logger = logging.getLogger("decp.info")
if DEVELOPMENT:
logger.setLevel(logging.DEBUG)
DOMAIN_NAME = (
"test.decp.info" if os.getenv("DEVELOPMENT").lower() == "true" else "decp.info"
)
View File
+27
View File
@@ -0,0 +1,27 @@
from src.utils.data import DATA_SCHEMA
def get_button_properties(height):
if height > 65000:
download_disabled = True
download_text = "Téléchargement désactivé au-delà de 65 000 lignes"
download_title = " Ajoutez des filtres pour réduire le nombre de lignes, Excel ne supporte pas d'avoir plus de 65 000 URLs dans une même feuille de calcul."
elif height == 0:
download_disabled = True
download_text = "Pas de données à télécharger"
download_title = ""
else:
download_disabled = False
download_text = "Télécharger au format Excel"
download_title = "Télécharger les données telles qu'affichées au format Excel"
return download_disabled, download_text, download_title
def get_enum_values_as_dict(column_name):
try:
options = {}
for value in DATA_SCHEMA[column_name]["enum"]:
options[value] = value
return options
except KeyError:
return {"not_found": "not found"}
+52
View File
@@ -0,0 +1,52 @@
from src.utils import DOMAIN_NAME
from src.utils.data import get_annuaire_data
def make_org_jsonld(org_id, org_type, org_name=None, type_org_id="SIRET") -> dict:
org_types = {"acheteur": "GovernmentOrganization", "titulaire": "Organization"}
address = None
if type_org_id.lower() == "siret" and len(org_id) == 14:
annuaire_data = get_annuaire_data(org_id)
annuaire_address = annuaire_data["matching_etablissements"][0]
code_postal = annuaire_address["code_postal"]
commune = annuaire_address["libelle_commune"]
address = (
{
"@type": "PostalAddress",
"streetAddress": annuaire_address.get("adresse", "")
.replace(code_postal, "")
.replace(commune, "")
.strip(),
"addressLocality": commune,
"postalCode": code_postal,
"addressCountry": "FR",
},
)
jsonld = {
"@type": org_types[org_type],
"name": org_name,
"url": f"https://decp.info/{org_type}s/{org_id}",
"sameAs": f"https://annuaire-entreprises.data.gouv.fr/etablissement/{org_id}",
"identifier": {
"@type": "PropertyValue",
"propertyID": type_org_id.lower(),
"value": org_id,
},
}
if address:
jsonld["address"] = address
return jsonld
META_CONTENT = {
"image_url": f"https://{DOMAIN_NAME}/assets/decp.info.png",
"title": "decp.info - exploration des marchés publics français",
"description": (
"Explorez et analysez les données des marchés publics français avec cet outil libre et gratuit. "
"Pour une commande publique accessible à toutes et tous."
),
}
+30
View File
@@ -0,0 +1,30 @@
import os
import uuid
from time import localtime
from httpx import post
from src.utils import DEVELOPMENT
def track_search(query, category):
if len(query) >= 4 and not DEVELOPMENT and os.getenv("MATOMO_DOMAIN"):
url = "https://decp.info"
params = {
"idsite": os.getenv("MATOMO_ID_SITE"),
"url": url,
"rec": "1",
"action_name": "search" if category == "home_page_search" else "filter",
"search_cat": category,
"rand": uuid.uuid4().hex,
"apiv": "1",
"h": localtime().tm_hour,
"m": localtime().tm_min,
"s": localtime().tm_sec,
"search": query,
"token_auth": os.getenv("MATOMO_TOKEN"),
}
post(
url=f"https://{os.getenv('MATOMO_DOMAIN')}/matomo.php",
params=params,
).raise_for_status()