Skip to content

Commit

Permalink
Merge pull request #70 from DUT-Info-Montreuil/feature/label-user
Browse files Browse the repository at this point in the history
add get label user
  • Loading branch information
G8LD committed Jan 17, 2024
2 parents 5d3a3b7 + ab7b3d3 commit 9b13406
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
40 changes: 16 additions & 24 deletions uniride_sme/route/user_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from uniride_sme.utils import email
from uniride_sme.utils.file import get_encoded_file
from uniride_sme.utils.jwt_token import revoke_token
from uniride_sme.utils.field import validate_fields
from uniride_sme.utils.role_user import RoleUser, role_required

user = Blueprint("user", __name__, url_prefix="/user")
Expand Down Expand Up @@ -143,29 +144,6 @@ def get_infos():

return response

@user.route("/infos/<int:user_id>", methods=["GET"])
def get_infos_by_id(user_id):
"""Get user infos by ID endpoint"""
try:
user_bo = user_service.get_user_by_id(user_id)
user_infos_dto = UserInfosDTO(
id=user_id,
login=user_bo.login,
student_email=user_bo.student_email,
firstname=user_bo.firstname,
lastname=user_bo.lastname,
gender=user_bo.gender,
phone_number=user_bo.phone_number,
description=user_bo.description,
role=user_bo.r_id,
profile_picture=get_encoded_file(user_bo.profile_picture, "PFP_UPLOAD_FOLDER"),
)
response = jsonify(user_infos_dto), 200
except ApiException as e:
response = jsonify(message=e.message), e.status_code

return response


@user.route("/role", methods=["GET"])
@jwt_required()
Expand Down Expand Up @@ -495,7 +473,6 @@ def delete_user(user_id):


@user.route("/infos/<user_id>", methods=["GET"])
@role_required(RoleUser.ADMINISTRATOR)
def user_information_token(user_id):
"""Informations user by token"""
try:
Expand Down Expand Up @@ -620,3 +597,18 @@ def get_actif_criterian(r_id):
except ApiException as e:
response = jsonify(message=e.message), e.status_code
return response


@user.route("/label/info", methods=["POST"])
@jwt_required()
def get_label():
"""Get label"""
try:
user_id = get_jwt_identity()["id"]
json_object = request.get_json()
validate_fields(json_object, {"trip_id": int})
data = user_service.get_label(json_object.get("trip_id", None), user_id)
response = jsonify({"message": "LABEL_DISPLAYED_SUCCESSFULLY", "label": data}), 200
except ApiException as e:
response = jsonify(message=e.message), e.status_code
return response
14 changes: 14 additions & 0 deletions uniride_sme/service/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from uniride_sme import connect_pg
from uniride_sme.model.bo.user_bo import UserBO
from uniride_sme.service.documents_service import update_role
from uniride_sme.service.trip_service import get_trip_by_id
from uniride_sme.utils.file import save_file, delete_file
from uniride_sme.utils.exception.exceptions import (
InvalidInputException,
Expand Down Expand Up @@ -879,3 +880,16 @@ def average_rating_user_id(id_user):
result = connect_pg.get_query(conn, query, (id_user,))
connect_pg.disconnect(conn)
return result[0][0]


def get_label(trip_id, user_id):
"""Get passenger label"""
conn = connect_pg.connect()
current_trip = get_trip_by_id(trip_id)
if user_id == current_trip.get("driver_id"):
query = "SELECT rc_id, rc_name, rc_description FROM uniride.ur_rating_criteria WHERE r_id = 2"
else:
query = "SELECT rc_id, rc_name, rc_description FROM uniride.ur_rating_criteria WHERE r_id = 1"
result = connect_pg.get_query(conn, query)
connect_pg.disconnect(conn)
return result
3 changes: 1 addition & 2 deletions uniride_sme/utils/exception/criteria_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from uniride_sme.utils.exception.exceptions import ApiException


class TooManyCriteriaException (ApiException):
class TooManyCriteriaException(ApiException):
"""Exception for when the documents isn't found"""

def __init__(self):
super().__init__("TOO_MANY_CRITERIA", 417)

2 changes: 1 addition & 1 deletion uniride_sme/utils/exception/user_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ class UserNotAUTHORIZED(ApiException):
"""Exception for when the user isn't authorized"""

def __init__(self):
super().__init__("USER_NOT_AUTHORIZED", 422)
super().__init__("USER_NOT_AUTHORIZED", 422)
12 changes: 6 additions & 6 deletions uniride_sme/utils/jwt_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from uniride_sme import cache, jwt


@jwt.token_in_blocklist_loader
def check_if_token_is_revoked(jwt_header, jwt_payload: dict): # pylint: disable=unused-argument
"""Check if token is revoked"""
jti = jwt_payload["jti"]
token_in_redis = cache.get(jti)
return token_in_redis is not None
# @jwt.token_in_blocklist_loader
# def check_if_token_is_revoked(jwt_header, jwt_payload: dict): # pylint: disable=unused-argument
# """Check if token is revoked"""
# jti = jwt_payload["jti"]
# token_in_redis = cache.get(jti)
# return token_in_redis is not None


def revoke_token():
Expand Down

0 comments on commit 9b13406

Please sign in to comment.