Coverage for src/resources/user_resource.py : 100%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from flask import request
2from flask_restx import Resource
3from flask_jwt_extended import jwt_required, get_jwt_identity
5from src.service import UserService, ExternalService
6from src.dto import UserDto
8from src.schemas import UpdateUserDataSchema
9from src.utils import validation_error
12api = UserDto.api
13data_resp = UserDto.data_resp
14export_user_resp = UserDto.export_user_resp
15search_data_resp = UserDto.search_data_resp
16update_schema = UpdateUserDataSchema()
19@api.route("/<uuid:uuid>")
20class UserResource(Resource):
21 @api.doc(
22 "Get a specific user",
23 responses={
24 200: ("User data successfully sent", data_resp),
25 401: ("Authentication required"),
26 404: "User not found!",
27 },
28 )
29 @jwt_required
30 def get(self, uuid):
31 """ Get a specific user's data by their uuid """
32 user_uuid = get_jwt_identity()
33 return UserService.get_user_data(uuid, user_uuid)
35 @api.doc(
36 "Delete a specific user account",
37 responses={
38 200: ("User account successfully deleted", data_resp),
39 401: ("Authentication required"),
40 403: ("Unable to delete an account which is not your's"),
41 404: "User not found!",
42 },
43 )
44 @jwt_required
45 def delete(self, uuid):
46 """ Delete a specific user's account by their uuid """
47 user_uuid = get_jwt_identity()
48 return UserService.delete_account(uuid, user_uuid)
50 user_data = UserDto.user_data
52 @api.doc(
53 "Update a specific username",
54 responses={
55 200: ("Username successfully updated", data_resp),
56 401: ("Authentication required"),
57 403: ("Unable to update an account which is not your's"),
58 404: "User not found!",
59 },
60 )
61 @jwt_required
62 @api.expect(user_data, validate=True)
63 def patch(self, uuid):
64 user_uuid = get_jwt_identity()
65 data = request.get_json()
66 # Validate data
67 if (errors := update_schema.validate(data)):
68 return validation_error(False, errors)
69 return UserService.update_user_data(uuid, user_uuid, data)
72@api.route("/preferences_defined")
73class UserResource(Resource):
74 @api.doc(
75 "Set preferences defined to true",
76 responses={
77 201: ("User data successfully sent"),
78 401: ("Authentication required"),
79 403: ("Unable to update an account which is not your's"),
80 404: "User not found!",
81 },
82 )
83 @jwt_required
84 def put(self):
85 """ Set preferences defined to true """
86 user_uuid = get_jwt_identity()
88 return UserService.set_preferences_defined(user_uuid)
91@api.route("/search/<string:search_term>", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}}})
92class UserSearchResource(Resource):
93 @api.doc(
94 "Search users",
95 responses={
96 200: ("User data successfully sent", search_data_resp),
97 401: ("Authentication required"),
98 },
99 )
100 @jwt_required
101 def get(self, search_term):
102 """ Get list of track's data by term """
103 user_uuid = get_jwt_identity()
104 try:
105 page = int(request.args.get('page'))
106 except (ValueError, TypeError):
107 page = 1
108 return UserService.search_user_data(search_term, page, user_uuid)
111@api.route("/genre")
112class UserGenresResource(Resource):
113 @api.doc(
114 "Get liked genres (connected user)",
115 responses={
116 201: ("Successfully send"),
117 401: ("Authentication required"),
118 404: "User not found!",
119 }
120 )
121 @jwt_required
122 def get(self):
123 """ Get liked genres (connected user) """
124 user_uuid = get_jwt_identity()
126 return UserService.get_genres(user_uuid)
129@api.route("/genre/<int:genre_id>")
130class UserGenreResource(Resource):
131 @api.doc(
132 "Like a genre (connected user)",
133 responses={
134 201: ("Successfully send"),
135 401: ("Authentication required"),
136 404: "User or Genre not found!",
137 }
138 )
139 @jwt_required
140 def put(self, genre_id):
141 """ Like a genre (connected user) """
142 user_uuid = get_jwt_identity()
144 return UserService.like_genre(genre_id, user_uuid)
146 @api.doc(
147 "Unlike a genre (connected user)",
148 responses={
149 201: ("Successfully send"),
150 401: ("Authentication required"),
151 404: "User or Genre not found!",
152 }
153 )
154 @jwt_required
155 def delete(self, genre_id):
156 """ Unlike a genre (connected user) """
157 user_uuid = get_jwt_identity()
159 return UserService.unlike_genre(genre_id, user_uuid)
162@api.route("/export")
163class UserExportResource(Resource):
164 @api.doc(
165 "Export data of a specific user",
166 responses={
167 200: ("User data successfully sent", export_user_resp),
168 401: ("Authentication required"),
169 404: "User not found!",
170 },
171 )
172 @jwt_required
173 def get(self):
174 """ Export data of a specific user """
175 user_uuid = get_jwt_identity()
177 return UserService.export_all_user_data(user_uuid)