Coverage for src/resources/book_resource.py : 89%
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
1import uuid
2from flask import request
3from flask_restx import Resource, reqparse
4from flask_jwt_extended import jwt_required, get_jwt_identity
6from src.service import BookService, ContentService
7from src.dto import BookDto, UserDto
9api = BookDto.api
10data_resp = BookDto.data_resp
11meta_resp = UserDto.meta_resp
14@api.route("", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}}})
15class BookResource(Resource):
16 @api.doc(
17 "Get list of the most popular Books",
18 responses={
19 200: ("Book data successfully sent", data_resp),
20 401: ("Authentication required"),
21 },
22 )
23 @jwt_required
24 def get(self):
25 """ Get list of the most popular Books """
26 user_uuid = get_jwt_identity()
28 try:
29 page = int(request.args.get('page'))
30 except (ValueError, TypeError):
31 page = 1
32 return BookService.get_popular_books(page, user_uuid)
34 book_additional = BookDto.book_additional_base
36 @api.doc(
37 "Add additional Book for validation",
38 responses={
39 200: ("Additional book added for validation", meta_resp),
40 401: ("Authentication required"),
41 }
42 )
43 @jwt_required
44 @api.expect(book_additional, validate=True)
45 def post(self):
46 """ Add additional Book for validation"""
47 user_uuid = get_jwt_identity()
49 # Grab the json data
50 data = request.get_json()
52 return BookService.add_additional_book(user_uuid, data)
55@api.route("/user", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}, "reco_engine": {"in": "query", "type": "str", "default": None}}})
56class BookUserRecommendationResource(Resource):
57 @api.doc(
58 "Get list of the recommended books for the connected user",
59 responses={
60 200: ("Book data successfully sent", data_resp),
61 401: ("Authentication required"),
62 },
63 )
64 @jwt_required
65 def get(self):
66 """ Get list of the recommended books for the connected user """
67 user_uuid = get_jwt_identity()
69 parser = reqparse.RequestParser()
70 parser.add_argument('page', type=int, default=1)
71 parser.add_argument('reco_engine', type=str, default=None)
72 args = parser.parse_args()
74 return BookService.get_recommended_books_for_user(args["page"], user_uuid, args["reco_engine"])
77@api.route("/groups", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}, "reco_engine": {"in": "query", "type": "str", "default": None}}})
78class BookGroupRecommendationResource(Resource):
79 @api.doc(
80 "Get list of the recommended books for the groups of the connected user",
81 responses={
82 200: ("Book data successfully sent", data_resp),
83 401: ("Authentication required"),
84 },
85 )
86 @jwt_required
87 def get(self):
88 """ Get list of the recommended books for the groups of the connected user """
89 user_uuid = get_jwt_identity()
91 parser = reqparse.RequestParser()
92 parser.add_argument('page', type=int, default=1)
93 parser.add_argument('reco_engine', type=str, default=None)
94 args = parser.parse_args()
96 return BookService.get_recommended_books_for_group(args["page"], user_uuid, args["reco_engine"])
99@api.route("/search/<string:search_term>", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}}})
100class BookSearchResource(Resource):
101 @api.doc(
102 "Search books",
103 responses={
104 200: ("Book data successfully sent", data_resp),
105 401: ("Authentication required"),
106 },
107 )
108 @jwt_required
109 def get(self, search_term):
110 """ Getlist of book's data by term """
111 try:
112 page = int(request.args.get('page'))
113 except (ValueError, TypeError):
114 page = 1
115 uuid = get_jwt_identity()
116 return BookService.search_book_data(search_term, page, uuid)
119@api.route("/<int:content_id>/meta")
120class bookMetaResource(Resource):
121 @api.doc(
122 "Get book-user (connected user) meta",
123 responses={
124 200: ("Book-User meta data successfully sent", meta_resp),
125 401: ("Authentication required"),
126 }
127 )
128 @jwt_required
129 def get(self, content_id):
130 """ Get book-user (connected user) meta """
131 user_uuid = get_jwt_identity()
133 return ContentService.get_meta(user_uuid, content_id)
135 content_meta = UserDto.content_meta
137 @api.doc(
138 "Update book-user (connected user) meta",
139 responses={
140 201: ("Book-User meta data successfully sent"),
141 401: ("Authentication required"),
142 404: "User or Book not found!",
143 },
144 )
145 @jwt_required
146 @api.expect(content_meta, validate=True)
147 def patch(self, content_id):
148 """ Update book-user (connected user) meta """
149 user_uuid = get_jwt_identity()
151 # Grab the json data
152 data = request.get_json()
154 return ContentService.update_meta(user_uuid, content_id, data)
157@api.route("/<int:content_id>/bad_recommendation")
158class BookBadRecommendation(Resource):
159 bad_recommendation = BookDto.book_bad_recommendation
161 @api.doc(
162 "Add Book-user (connected user) bad recommendation",
163 responses={
164 200: ("Book-User bad recommendation successfully sent", meta_resp),
165 401: ("Authentication required"),
166 }
167 )
168 @jwt_required
169 @api.expect(bad_recommendation, validate=True)
170 def post(self, content_id):
171 """ Add Book-user (connected user) bad recommendation """
172 user_uuid = get_jwt_identity()
174 # Grab the json data
175 data = request.get_json()
177 return BookService.add_bad_recommendation(user_uuid, content_id, data)
179@api.route("/additional", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}}})
180class BookAdditionalResource(Resource):
181 @api.doc(
182 "Get list of the added Books (by user)",
183 responses={
184 200: ("Book data successfully sent", data_resp),
185 401: ("Authentication required"),
186 },
187 )
188 @jwt_required
189 def get(self):
190 """ Get list of the added Books (by user) """
191 user_uuid = get_jwt_identity()
193 parser = reqparse.RequestParser()
194 parser.add_argument('page', type=int, default=1)
195 args = parser.parse_args()
197 return BookService.get_additional_book(user_uuid, args["page"])
200@api.route("/additional/<int:book_id>")
201class BookAdditionalValidationResource(Resource):
202 @api.doc(
203 "Validate (put) added Books (by user)",
204 responses={
205 201: ("Additional book data successfully validated"),
206 401: ("Authentication required"),
207 403: ("Permission missing"),
208 404: ("User or book not found!"),
209 },
210 )
211 @jwt_required
212 def put(self, book_id):
213 """ Validate (put) added Books (by user) """
214 user_uuid = get_jwt_identity()
216 return BookService.validate_additional_book(user_uuid, book_id)
218 @api.doc(
219 "Decline (delete) added Books (by user)",
220 responses={
221 201: ("Additional book successfully deleted"),
222 401: ("Authentication required"),
223 403: ("Permission missing"),
224 404: ("User or book not found!"),
225 },
226 )
227 @jwt_required
228 def delete(self, book_id):
229 """ Decline (delete) added Books (by user) """
230 user_uuid = get_jwt_identity()
232 return BookService.decline_additional_book(user_uuid, book_id)