Skip to content

Commit

Permalink
feat: Formation is re-writed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Oct 29, 2023
1 parent 0261ad6 commit 9c010d8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ flask-migrate = "==3.1.0"
xmltodict = "==0.13.0"
secure = "==0.3.0"
loguru = "*"
cuid2 = "*"
sqlalchemy-serializer = "*"

[dev-packages]

Expand Down
17 changes: 8 additions & 9 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""empty message
"""Add UUID in formation table.
Revision ID: ee56a7eaab4a
Revises: 16c8bc08a922
Expand Down
45 changes: 27 additions & 18 deletions src/blueprints/favoris.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from flask import Blueprint, jsonify, request, Response, abort
from sqlalchemy import and_, exists
from werkzeug.exceptions import HTTPException
from typing import Tuple
from uuid import UUID

import validators
from flask_jwt_extended import get_jwt_identity, jwt_required
from flasgger import swag_from
from flask import Blueprint, Response, abort, jsonify, request
from flask_jwt_extended import get_jwt_identity, jwt_required
from sqlalchemy import and_, exists
from werkzeug.exceptions import HTTPException

from src import db
from src.constants.http_status_codes import (
HTTP_200_OK,
HTTP_201_CREATED,
HTTP_400_BAD_REQUEST,
HTTP_404_NOT_FOUND,
HTTP_409_CONFLICT,
)
from src import db
from src.models import UserFavori

from typing import Tuple

from src.models.formation import Formation

favoris = Blueprint("favoris", __name__, url_prefix="/api/v1/favoris")
Expand Down Expand Up @@ -71,11 +71,16 @@ def post_favori_by_user_id() -> Tuple[Response, int] | HTTPException:
def get_favoris_by_user_id() -> Tuple[Response, int]:
current_user = get_jwt_identity()
favoris = (
UserFavori.query.filter(UserFavori.request_user_id == current_user)
.order_by(UserFavori.created_at.asc())
db.session.query(Formation)
.join(UserFavori, UserFavori.formation_id == Formation.id)
.filter(UserFavori.user_id == current_user)
.all()
)
return jsonify({"size": len(favoris), "results": favoris}), HTTP_200_OK

result = [favori.to_dict() for favori in favoris]
response_data = {"size": len(favoris), "results": result}

return jsonify(response_data), HTTP_200_OK


# Remove_favoris function need JWT token and delete favoris for this user
Expand All @@ -85,22 +90,26 @@ def get_favoris_by_user_id() -> Tuple[Response, int]:
@jwt_required()
def get_favoris_ids() -> Tuple[Response, int]:
current_user = get_jwt_identity()
result = (
UserFavori.query.with_entities(UserFavori.url_et_id_onisep, UserFavori.id)
.filter(UserFavori.request_user_id == current_user)
favoris = (
db.session.query(Formation.url, Formation.id)
.join(UserFavori, UserFavori.formation_id == Formation.id)
.filter(UserFavori.user_id == current_user)
.all()
)
favori_data = [{"id": row.id, "url": row.url_et_id_onisep} for row in result]
favori_data = [{"id": row.id, "url": row.url} for row in favoris]
return jsonify({"favori_ids": favori_data}), HTTP_200_OK


@favoris.delete("/<int:id>")
@favoris.delete("/<string:id>")
@jwt_required()
@swag_from("../docs/favoris/remove.yaml")
def remove_favori(id: int) -> Tuple[Response, int] | HTTPException:
def remove_favori(id: str) -> Tuple[Response, int] | HTTPException:
current_user = get_jwt_identity()

favori = UserFavori.query.filter_by(request_user_id=current_user, id=id).first()
favori = UserFavori.query.filter(
UserFavori.user_id == current_user,
UserFavori.formation_id == UUID(id),
).first()

if not favori:
abort(HTTP_404_NOT_FOUND, "Favoris not found")
Expand Down
2 changes: 1 addition & 1 deletion src/docs/favoris/remove.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ delete:
description: Supprimer un favori.
in: path
required: true
type: integer
type: string

responses:
204:
Expand Down
6 changes: 0 additions & 6 deletions src/models/formation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import uuid
from dataclasses import dataclass
from email.policy import default
from enum import unique
from typing import Callable

from cuid2 import Cuid, cuid_wrapper
from sqlalchemy import UUID

from src import db
from src.models.base_model import BaseModel
Expand Down
9 changes: 4 additions & 5 deletions src/models/user_favori.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from dataclasses import dataclass
from typing import Callable

from cuid2 import cuid_wrapper
from sqlalchemy import UUID
from src import db
from src.models.base_model import BaseModel
from src.models.helpers.UUIDType import UUIDType
from sqlalchemy_serializer import SerializerMixin


@dataclass
class UserFavori(BaseModel):
class UserFavori(BaseModel, SerializerMixin):
__tablename__ = "user_favori"

serialize_only = "formation_id"

formation_id = db.Column(
UUIDType,
db.ForeignKey("formation.id", ondelete="CASCADE"),
Expand Down

0 comments on commit 9c010d8

Please sign in to comment.