diff --git a/src/__init__.py b/src/__init__.py index 564d5d1..79f0de0 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,7 +1,8 @@ import os import sys -from flasgger import Swagger +from apispec.ext.marshmallow import MarshmallowPlugin +from apispec_webframeworks.flask import FlaskPlugin from flask import Flask from flask_cors import CORS from flask_jwt_extended import JWTManager @@ -10,17 +11,9 @@ from loguru import logger from config import load_config -from src.config.swagger import ( - get_swagger_api_spec, - swagger_config, -) -from src.models.schemas import schemas -from flasgger.utils import apispec_to_template from .errors import register_error_handlers from .middlewares import after_request -from apispec.ext.marshmallow import MarshmallowPlugin -from apispec_webframeworks.flask import FlaskPlugin db: SQLAlchemy = SQLAlchemy() plugins = [ @@ -44,13 +37,6 @@ def create_app(environment=None): with app.app_context(): register_blueprints(app) - template = apispec_to_template( - app=app, - spec=get_swagger_api_spec(config=config, plugins=plugins), - definitions=schemas, - ) - Swagger(app, config=swagger_config, template=template) - app.after_request(after_request) register_error_handlers(app) @@ -61,9 +47,9 @@ def register_blueprints(app: Flask): from src.blueprints.auth import auth from src.blueprints.favoris import favoris from src.blueprints.formations import formations - from src.blueprints.utils import utils - from src.blueprints.legal.views import legal from src.blueprints.graphql import graphql + from src.blueprints.legal.views import legal + from src.blueprints.utils import utils app.register_blueprint(utils) app.register_blueprint(auth) diff --git a/src/business_logic/formation/scrap/tests/unit/test_search_onisep_formations.py b/src/business_logic/formation/scrap/tests/unit/test_search_onisep_formations.py index 37c6aee..568490b 100644 --- a/src/business_logic/formation/scrap/tests/unit/test_search_onisep_formations.py +++ b/src/business_logic/formation/scrap/tests/unit/test_search_onisep_formations.py @@ -58,7 +58,7 @@ def test_search_formations_should_return_formations_without_favorite( mock_search_formations.return_value = MOKED_RESEARCH # Act - formations = search_formations("STHR", 1) + formations = search_formations(limit=1, offset=None, query="STHR1") # Assert moked_formation = MOKED_RESEARCH["results"] @@ -68,7 +68,7 @@ def test_search_formations_should_return_formations_without_favorite( ) assert formations == waited_result - mock_search_formations.assert_called_once_with("STHR", 1, None) + mock_search_formations.assert_called_once_with(limit=1, offset=None, query="STHR1") def test_authenticated_search_formations_should_return_formations_with_favorite( @@ -87,7 +87,9 @@ def test_authenticated_search_formations_should_return_formations_with_favorite( db_session.commit() # Act - formations = auth_search_formations(user.id, "STHR", 1) + formations = auth_search_formations( + user_id=user.id, query="STHR2", limit=1, offset=None + ) # Assert moked_formation = MOKED_RESEARCH["results"] @@ -97,4 +99,4 @@ def test_authenticated_search_formations_should_return_formations_with_favorite( ) assert formations == waited_result - mock_search_formations.assert_called_once_with("STHR", 1, None) + mock_search_formations.assert_called_once_with(query="STHR2", limit=1, offset=None) diff --git a/src/models/base_model.py b/src/models/base_model.py index 3cbab8b..c640281 100644 --- a/src/models/base_model.py +++ b/src/models/base_model.py @@ -1,17 +1,16 @@ -from sqlalchemy import Column, DateTime, func -from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy import func -Model = declarative_base(name="Model") +from src import db -class BaseModel(Model): +class BaseModel(db.Model): __abstract__: bool = True __allow_unmapped__: bool = True - created_at = Column(DateTime, nullable=False, default=func.now()) - updated_at = Column( - DateTime, nullable=False, default=func.now(), onupdate=func.now() + created_at = db.Column(db.DateTime, nullable=False, default=func.now()) + updated_at = db.Column( + db.DateTime, nullable=False, default=func.now(), onupdate=func.now() ) def to_dict(self): diff --git a/src/models/formation.py b/src/models/formation.py index 1f2c2ad..4da8427 100644 --- a/src/models/formation.py +++ b/src/models/formation.py @@ -5,6 +5,8 @@ from src.models.base_model import BaseModel from src.models.helpers.UUIDType import UUIDType +from src import db + def default_uuid5(): namespace = uuid.uuid4() @@ -16,16 +18,16 @@ def default_uuid5(): class Formation(BaseModel): __tablename__ = "formation" - id: uuid.UUID = Column( + id: uuid.UUID = db.Column( UUIDType, default=default_uuid5, primary_key=True, ) - code_nsf: int = Column(Integer, nullable=False) - type: str = Column(String(255), nullable=False) - libelle: str = Column(String(255), nullable=False) - tutelle: str = Column(String(255), nullable=False) - url: str = Column(String(255), nullable=False, unique=True) - domain: str = Column(Text, nullable=False) - niveau_de_sortie: str = Column(String(255), nullable=False) - duree: str = Column(String(255), nullable=False) + code_nsf: int = db.Column(db.Integer, nullable=False) + type: str = db.Column(db.String(255), nullable=False) + libelle: str = db.Column(db.String(255), nullable=False) + tutelle: str = db.Column(db.String(255), nullable=False) + url: str = db.Column(db.String(255), nullable=False, unique=True) + domain: str = db.Column(db.Text, nullable=False) + niveau_de_sortie: str = db.Column(db.String(255), nullable=False) + duree: str = db.Column(db.String(255), nullable=False) diff --git a/src/models/schemas/__init__.py b/src/models/schemas/__init__.py deleted file mode 100644 index f5692d0..0000000 --- a/src/models/schemas/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -from src.models.schemas.schemas import FormationSchema, UserFavoriSchema, UserSchema - - -schemas = [UserSchema, FormationSchema, UserFavoriSchema] diff --git a/src/models/schemas/schemas.py b/src/models/schemas/schemas.py deleted file mode 100644 index 9e045c3..0000000 --- a/src/models/schemas/schemas.py +++ /dev/null @@ -1,24 +0,0 @@ -from marshmallow_sqlalchemy import SQLAlchemyAutoSchema, fields -from src.models.formation import Formation -from src.models.user import User -from src.models.user_favori import UserFavori - - -class UserSchema(SQLAlchemyAutoSchema): - class Meta: - model = User - exclude = ("created_at", "updated_at", "password") - - -class FormationSchema(SQLAlchemyAutoSchema): - class Meta: - model = Formation - include_relationships = True - - -class UserFavoriSchema(SQLAlchemyAutoSchema): - class Meta: - model = UserFavori - - formation = fields.Nested(FormationSchema) - users = fields.Nested(UserSchema) diff --git a/src/models/user.py b/src/models/user.py index 1b09c98..8b37372 100644 --- a/src/models/user.py +++ b/src/models/user.py @@ -1,11 +1,10 @@ from typing import Optional -from sqlalchemy import Column, Integer, String, Text import strawberry + +from src import db from src.models.base_model import BaseModel from src.models.user_favori import UserFavori -from sqlalchemy.orm import relationship - # Create User row @@ -14,13 +13,13 @@ class User(BaseModel): __tablename__ = "user" - id: int = Column(Integer, primary_key=True) - username: str = Column(String(80), unique=True, nullable=False) - email: str = Column(String(200), unique=True, nullable=False) - password = Column(Text(), nullable=False) - profile_pic_url: Optional[str] = Column(Text) + id: int = db.Column(db.Integer, primary_key=True) + username: str = db.Column(db.String(80), unique=True, nullable=False) + email: str = db.Column(db.String(200), unique=True, nullable=False) + password = db.Column(db.Text(), nullable=False) + profile_pic_url: Optional[str] = db.Column(db.Text) - favoris: list[UserFavori] = relationship( + favoris: list[UserFavori] = db.relationship( "UserFavori", secondary=UserFavori.__tablename__, primaryjoin="User.id == UserFavori.user_id", diff --git a/src/models/user_favori.py b/src/models/user_favori.py index 15e30ea..fc3d5cb 100644 --- a/src/models/user_favori.py +++ b/src/models/user_favori.py @@ -1,9 +1,10 @@ -from sqlalchemy import Column, ForeignKey, Integer -from src.models.base_model import BaseModel +from sqlalchemy import ForeignKey +from sqlalchemy_serializer import SerializerMixin + +from src import db from src.models import Formation +from src.models.base_model import BaseModel from src.models.helpers.UUIDType import UUIDType -from sqlalchemy_serializer import SerializerMixin -from sqlalchemy.orm import relationship class UserFavori(BaseModel, SerializerMixin): @@ -11,19 +12,19 @@ class UserFavori(BaseModel, SerializerMixin): serialize_only = "formation_id" - formation_id = Column( + formation_id = db.Column( UUIDType, ForeignKey("formation.id", ondelete="CASCADE"), primary_key=True, index=True, ) - user_id = Column( - Integer, + user_id = db.Column( + db.Integer, ForeignKey("user.id", ondelete="CASCADE"), primary_key=True, index=True, ) - users = relationship("User", back_populates="favoris") - formation: list[Formation] = relationship("Formation") + users = db.relationship("User", back_populates="favoris") + formation: list[Formation] = db.relationship("Formation")