Skip to content

Commit

Permalink
feat: now we can see if formation is_favirite or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Nov 3, 2023
1 parent f463dae commit 7679505
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 74 deletions.
20 changes: 16 additions & 4 deletions src/blueprints/formations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
from typing import Any, Tuple

from flask import Blueprint, Response, request
from flask_jwt_extended import jwt_required
from flask_jwt_extended import get_jwt_identity, jwt_required
from werkzeug.exceptions import HTTPException

from src.blueprints.route_handler import HttpMethod, route_handler
from src.business_logic.formation.scrap.get_formation import (
get_libelle_type_formation,
from src.business_logic.formation.scrap.get_main_formation import (
auth_get_main_formations,
get_main_formations,
)
from src.business_logic.formation.scrap.get_repartition_formations import (
get_libelle_type_formation,
)
from src.business_logic.formation.scrap.search_formation import (
auth_search_formations,
search_formations,
)
from src.constants.http_status_codes import (
Expand All @@ -24,11 +30,14 @@ def _filter_by_link(formations: list[dict[str, Any]], for_id: str) -> dict[str,


@route_handler(formations, "/", HttpMethod.POST)
@jwt_required(optional=True)
def resolve_get_main_formations() -> Tuple[Response, int] | HTTPException:
data = request.get_json()
offset = data.get("offset")
limit = data.get("limit")

user_id = get_jwt_identity()
if user_id:
return auth_get_main_formations(user_id, limit, offset)
return get_main_formations(limit, offset)


Expand Down Expand Up @@ -57,6 +66,9 @@ def resolve_get_search_formation() -> Tuple[Response, int] | HTTPException:
limit = post.get("limit")
offset = post.get("offset")

user_id = get_jwt_identity()
if user_id:
return auth_search_formations(user_id, query, limit, offset)
return search_formations(query, limit, offset)


Expand Down
4 changes: 2 additions & 2 deletions src/business_logic/favoris/is_favorite.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from sqlalchemy import and_, exists
from src.business_logic.formation import get_formation_by_url
from src.business_logic.formation.get_formation_by_url import get_formation_by_url
from src.models.user_favori import UserFavori

from src import db


def is_favorite(user_id: str, url: str) -> bool:
def check_if_is_favorite(user_id: str, url: str) -> bool:
formation = get_formation_by_url(url)

if formation is None:
Expand Down
3 changes: 2 additions & 1 deletion src/business_logic/formation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from src.business_logic.formation.scrap.get_onisep_token import get_token

from src.business_logic.formation.scrap.utils.get_onisep_token import get_token


ONISEP_URL = "https://api.opendata.onisep.fr/api/1.0/dataset/"
Expand Down
65 changes: 0 additions & 65 deletions src/business_logic/formation/scrap/get_formation.py

This file was deleted.

31 changes: 31 additions & 0 deletions src/business_logic/formation/scrap/get_main_formation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from src.business_logic.formation.scrap.utils.format_formations import (
format_formation_with_is_favorite,
format_formations,
)
from src.business_logic.formation.scrap.types import FormationsWithTotal
from src.business_logic.formation.scrap.utils.get_onisep_data import get_onisep_data


def _get_raw_main_formations(limit: int = 10, offset: int = None) -> dict:
params = f"/search?&size={limit}"
if offset:
params += f"&from={offset}"
return get_onisep_data(params)


def get_main_formations(limit: int = 10, offset: int = None) -> FormationsWithTotal:
data = _get_raw_main_formations(limit, offset)

formated_formations = format_formations(data["results"])

return FormationsWithTotal(data["total"], formated_formations)


def auth_get_main_formations(
user_id: int, limit: int = 10, offset: int = None
) -> FormationsWithTotal:
data = _get_raw_main_formations(limit, offset)

formated_formations = format_formation_with_is_favorite(user_id, data["results"])

return FormationsWithTotal(data["total"], formated_formations)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from src.business_logic.formation.scrap.types import Facet
from src.business_logic.formation.scrap.utils.get_onisep_data import get_onisep_data


def get_libelle_type_formation(query: str) -> list[Facet]:
params = f"/search?q={query}"
data = get_onisep_data(params)
return data["facets"]["libelle_type_formation"]
32 changes: 32 additions & 0 deletions src/business_logic/formation/scrap/search_formation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from src.business_logic.formation.scrap.utils.format_formations import (
format_formation_with_is_favorite,
format_formations,
)
from src.business_logic.formation.scrap.types import FormationsWithTotal
from src.business_logic.formation.scrap.utils.get_onisep_data import get_onisep_data


def _get_raw_search_data(query: str, limit: int, offset: int = None) -> dict:
params = f"/search?q={query}&size={limit}"
if offset:
params += f"&from={offset}"
return get_onisep_data(params)


def search_formations(
query: str, limit: int, offset: int = None
) -> FormationsWithTotal:
data = _get_raw_search_data(query, limit, offset)

formated_formations = format_formations(data["results"])

return FormationsWithTotal(data["total"], formated_formations)


def auth_search_formations(
user_id: int, query: str, limit: int, offset: int = None
) -> FormationsWithTotal:
data = _get_raw_search_data(query, limit, offset)
formated_formations = format_formation_with_is_favorite(user_id, data["results"])

return FormationsWithTotal(data["total"], formated_formations)
10 changes: 8 additions & 2 deletions src/business_logic/formation/scrap/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class Facet:


@dataclass
class SearchedFormations:
class FormationIsFavortite:
formation: Formation
is_favorite: bool


@dataclass
class FormationsWithTotal:
total: int
formations: list[Formation]
formations: list[Formation | FormationIsFavortite]
35 changes: 35 additions & 0 deletions src/business_logic/formation/scrap/utils/format_formations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from src.business_logic.favoris.is_favorite import check_if_is_favorite
from src.business_logic.formation.scrap.types import (
FormationIsFavortite,
)
from src.models.formation import Formation


def _create_formation_from_dict(formation: dict) -> Formation:
return Formation(
code_nsf=int(formation.get("code_nsf") or 0),
type=formation.get("sigle_type_formation")
or formation.get("libelle_type_formation"),
libelle=formation.get("libelle_formation_principal"),
tutelle=formation.get("tutelle"),
url=formation.get("url_et_id_onisep"),
domain=formation.get("domainesous-domaine"),
niveau_de_sortie=formation.get("niveau_de_sortie_indicatif"),
duree=formation.get("duree"),
)


def format_formations(data: list[dict]) -> list[Formation]:
return [_create_formation_from_dict(formation).to_dict() for formation in data]


def format_formation_with_is_favorite(
user_id: int, data: list[dict]
) -> list[FormationIsFavortite]:
return [
FormationIsFavortite(
formation=_create_formation_from_dict(formation).to_dict(),
is_favorite=check_if_is_favorite(user_id, formation["url_et_id_onisep"]),
)
for formation in data
]
15 changes: 15 additions & 0 deletions src/business_logic/formation/scrap/utils/get_onisep_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Idéo-Formations initiales en France
# https://opendata.onisep.fr/data/5fa591127f501/2-ideo-formations-initiales-en-france.htm
import requests
from src.business_logic.formation import HEADERS, ONISEP_URL


DATASET = "5fa591127f501"


def get_onisep_data(params: str) -> dict:
url = ONISEP_URL + DATASET + params
response = requests.get(url, headers=HEADERS)
if response.status_code == 200:
return response.json()
raise Exception("Onisep API is down.", response.status_code)

0 comments on commit 7679505

Please sign in to comment.