Skip to content

Commit

Permalink
feat: add endpoint and quick fix on size.
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Oct 31, 2023
1 parent 6ef77cf commit 8d074c6
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 21 deletions.
40 changes: 40 additions & 0 deletions migrations/versions/1932d4fa450b_alter_table_formation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Update size for 'demain' again.
Revision ID: 1932d4fa450b
Revises: 3d2e4be5a52b
Create Date: 2023-10-31 01:10:05.699656
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "1932d4fa450b"
down_revision = "3d2e4be5a52b"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column(
"formation",
"domain",
existing_type=mysql.VARCHAR(length=255),
type_=sa.Text(),
existing_nullable=False,
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column(
"formation",
"domain",
existing_type=sa.Text(),
type_=mysql.VARCHAR(length=255),
existing_nullable=False,
)
# ### end Alembic commands ###
104 changes: 104 additions & 0 deletions migrations/versions/3d2e4be5a52b_alter_formation_table_update_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""Alter table formation upate size.
Revision ID: 3d2e4be5a52b
Revises: ee56a7eaab4a
Create Date: 2023-10-31 00:49:13.312551
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "3d2e4be5a52b"
down_revision = "ee56a7eaab4a"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column(
"formation",
"type",
existing_type=mysql.VARCHAR(length=120),
type_=sa.String(length=255),
existing_nullable=False,
)
op.alter_column(
"formation",
"libelle",
existing_type=mysql.VARCHAR(length=120),
type_=sa.String(length=255),
existing_nullable=False,
)
op.alter_column(
"formation",
"tutelle",
existing_type=mysql.VARCHAR(length=120),
type_=sa.String(length=255),
existing_nullable=False,
)
op.alter_column(
"formation",
"niveau_de_sortie",
existing_type=mysql.VARCHAR(length=120),
type_=sa.String(length=255),
existing_nullable=False,
)
op.alter_column(
"formation",
"duree",
existing_type=mysql.VARCHAR(length=15),
type_=sa.String(length=255),
existing_nullable=False,
)
op.drop_constraint("favori_formation_id_fk", "user_favori", type_="foreignkey")
op.create_foreign_key(
None, "user_favori", "formation", ["formation_id"], ["id"], ondelete="CASCADE"
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, "user_favori", type_="foreignkey")
op.create_foreign_key(
"favori_formation_id_fk", "user_favori", "formation", ["formation_id"], ["id"]
)
op.alter_column(
"formation",
"duree",
existing_type=sa.String(length=255),
type_=mysql.VARCHAR(length=15),
existing_nullable=False,
)
op.alter_column(
"formation",
"niveau_de_sortie",
existing_type=sa.String(length=255),
type_=mysql.VARCHAR(length=120),
existing_nullable=False,
)
op.alter_column(
"formation",
"tutelle",
existing_type=sa.String(length=255),
type_=mysql.VARCHAR(length=120),
existing_nullable=False,
)
op.alter_column(
"formation",
"libelle",
existing_type=sa.String(length=255),
type_=mysql.VARCHAR(length=120),
existing_nullable=False,
)
op.alter_column(
"formation",
"type",
existing_type=sa.String(length=255),
type_=mysql.VARCHAR(length=120),
existing_nullable=False,
)
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion src/blueprints/favoris.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def post_favori_by_user_id() -> Tuple[Response, int] | HTTPException:
db.session.add(favori)
db.session.commit()

return jsonify(favori), HTTP_201_CREATED
return jsonify(favori.to_dict()), HTTP_201_CREATED


@favoris.route("/", methods=["GET"])
Expand Down
20 changes: 15 additions & 5 deletions src/blueprints/formations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from src.blueprints.route_handler import HttpMethod, route_handler
from src.business_logic.formation.scrap.get_formation import (
get_libelle_type_formation,
get_main_formations,
search_formations,
)
from src.constants.http_status_codes import (
Expand All @@ -21,13 +22,22 @@ def _filter_by_link(formations: list[dict[str, Any]], for_id: str) -> dict[str,
return filtered_list[0] if filtered_list else {}


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

return get_main_formations(limit, offset)


@route_handler(
formations,
"/<string:id>",
HttpMethod.GET,
"../docs/formations/formation.yaml",
)
def get_formation_by_id(id: str) -> Tuple[Response, int] | HTTPException:
def resolve_get_formation_by_id(id: str) -> Tuple[Response, int] | HTTPException:
with open("assets/formation/data.json", "r") as json_file:
result = _filter_by_link(json.load(json_file)["formations"]["formation"], id)
return result, HTTP_200_OK if len(result) > 0 else HTTP_200_OK
Expand All @@ -39,7 +49,7 @@ def get_formation_by_id(id: str) -> Tuple[Response, int] | HTTPException:
HttpMethod.POST,
"../docs/formations/searchFormation.yaml",
)
def get_search_formation() -> Tuple[Response, int] | HTTPException:
def resolve_get_search_formation() -> Tuple[Response, int] | HTTPException:
post = request.get_json()
query = post.get("query")
limit = post.get("limit")
Expand All @@ -54,8 +64,8 @@ def get_search_formation() -> Tuple[Response, int] | HTTPException:
HttpMethod.POST,
"../docs/formations/formationByLibelle.yaml",
)
def get_formation_by_libelle() -> Tuple[Response, int] | HTTPException:
post = request.get_json()
query = post.get("query")
def resolve_get_formation_by_libelle() -> Tuple[Response, int] | HTTPException:
data = request.get_json()
query = data.get("query")

return get_libelle_type_formation(query)
2 changes: 1 addition & 1 deletion src/blueprints/route_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def route_handler(
blueprint: Blueprint, route: str, method: HttpMethod, swag_yaml: str = None
) -> Callable:
def decorator(func: Callable) -> Callable:
@ swag_from(swag_yaml) if swag_yaml else lambda: None
@swag_from(swag_yaml if swag_yaml else lambda: None)
def inner_wrapper(*args, **kwargs):
try:
return jsonify(func(*args, **kwargs)), HTTP_200_OK
Expand Down
31 changes: 23 additions & 8 deletions src/business_logic/formation/scrap/get_formation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ def _get_data(params: str) -> dict:
return response.json()


def search_formations(query: str, limit: int, offset: int = None) -> SearchedFormations:
params = f"/search?q={query}&size={limit}"
if offset:
params += f"&from={offset}"
data = _get_data(params)

formated_formations = [
def _format_formations(data: list[dict]) -> list[Formation]:
return [
Formation(
int(formation["code_nsf"] or 0),
formation["sigle_type_formation"] or formation["libelle_type_formation"],
Expand All @@ -36,13 +31,33 @@ def search_formations(query: str, limit: int, offset: int = None) -> SearchedFor
formation["niveau_de_sortie_indicatif"],
formation["duree"],
)
for formation in data["results"]
for formation in data
]


def search_formations(query: str, limit: int, offset: int = None) -> SearchedFormations:
params = f"/search?q={query}&size={limit}"
if offset:
params += f"&from={offset}"
data = _get_data(params)

formated_formations = _format_formations(data["results"])

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


def get_libelle_type_formation(query: str) -> list[Facet]:
params = f"/search?q={query}"
data = _get_data(params)
return data["facets"]["libelle_type_formation"]


def get_main_formations(limit: int = 10, offset: int = None) -> SearchedFormations:
params = f"/search?&size={limit}"
if offset:
params += f"&from={offset}"
data = _get_data(params)

formated_formations = _format_formations(data["results"])

return SearchedFormations(data["total"], formated_formations)
14 changes: 8 additions & 6 deletions src/models/formation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import uuid

from sqlalchemy import Text

from src import db
from src.models.base_model import BaseModel
from src.models.helpers.UUIDType import UUIDType
Expand All @@ -20,10 +22,10 @@ class Formation(BaseModel):
primary_key=True,
)
code_nsf = db.Column(db.Integer, nullable=False)
type = db.Column(db.String(120), nullable=False)
libelle = db.Column(db.String(120), nullable=False)
tutelle = db.Column(db.String(120), nullable=False)
type = db.Column(db.String(255), nullable=False)
libelle = db.Column(db.String(255), nullable=False)
tutelle = db.Column(db.String(255), nullable=False)
url = db.Column(db.String(255), nullable=False, unique=True)
domain = db.Column(db.String(255), nullable=False)
niveau_de_sortie = db.Column(db.String(120), nullable=False)
duree = db.Column(db.String(15), nullable=False)
domain = db.Column(Text, nullable=False)
niveau_de_sortie = db.Column(db.String(255), nullable=False)
duree = db.Column(db.String(255), nullable=False)

0 comments on commit 8d074c6

Please sign in to comment.