Coverage for src/resources/application_resource.py : 90%
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, reqparse
3from flask_jwt_extended import jwt_required, get_jwt_identity
5from src.service import ApplicationService, ContentService
6from src.dto import ApplicationDto, UserDto
8api = ApplicationDto.api
9data_resp = ApplicationDto.data_resp
10genres_resp = UserDto.genres_resp
11meta_resp = UserDto.meta_resp
14@api.route("")
15class ApplicationResource(Resource):
16 @api.doc(
17 "Get list of the most popular Applications",
18 responses={
19 200: ("Application data successfully sent", data_resp),
20 401: ("Authentication required"),
21 },
22 params={"page": {"in": "query", "type": "int", "default": 1}}
23 )
24 @jwt_required
25 def get(self):
26 """ Get list of the most popular Applications """
27 user_uuid = get_jwt_identity()
29 try:
30 page = int(request.args.get('page'))
31 except (ValueError, TypeError):
32 page = 1
33 return ApplicationService.get_popular_applications(page, user_uuid)
35 application_additional = ApplicationDto.application_additional_base
37 @api.doc(
38 "Add additional Application for validation",
39 responses={
40 200: ("Additional application added for validation", meta_resp),
41 401: ("Authentication required"),
42 }
43 )
44 @jwt_required
45 @api.expect(application_additional, validate=True)
46 def post(self):
47 """ Add additional Application for validation"""
48 user_uuid = get_jwt_identity()
50 # Grab the json data
51 data = request.get_json()
53 return ApplicationService.add_additional_application(user_uuid, data)
56@api.route("/user", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}, "reco_engine": {"in": "query", "type": "str", "default": None}}})
57class ApplicationUserRecommendationResource(Resource):
58 @api.doc(
59 "Get list of the recommended Applications for the connected user",
60 responses={
61 200: ("Application data successfully sent", data_resp),
62 401: ("Authentication required"),
63 },
64 )
65 @jwt_required
66 def get(self):
67 """ Get list of the recommended Applications for the connected user """
68 user_uuid = get_jwt_identity()
70 parser = reqparse.RequestParser()
71 parser.add_argument('page', type=int, default=1)
72 parser.add_argument('reco_engine', type=str, default=None)
73 args = parser.parse_args()
75 return ApplicationService.get_recommended_applications_for_user(args["page"], user_uuid, args["reco_engine"])
78@api.route("/groups", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}, "reco_engine": {"in": "query", "type": "str", "default": None}}})
79class ApplicationGroupRecommendationResource(Resource):
80 @api.doc(
81 "Get list of the recommended Applications for the groups of the connected user",
82 responses={
83 200: ("Application data successfully sent", data_resp),
84 401: ("Authentication required"),
85 },
86 )
87 @jwt_required
88 def get(self):
89 """ Get list of the recommended Applications for the groups of the connected user """
90 user_uuid = get_jwt_identity()
92 parser = reqparse.RequestParser()
93 parser.add_argument('page', type=int, default=1)
94 parser.add_argument('reco_engine', type=str, default=None)
95 args = parser.parse_args()
97 return ApplicationService.get_recommended_applications_for_group(args["page"], user_uuid, args["reco_engine"])
100@api.route("/search/<string:search_term>", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}}})
101class ApplicationSearchResource(Resource):
102 @api.doc(
103 "Search applications",
104 responses={
105 200: ("Application data successfully sent", data_resp),
106 401: ("Authentication required"),
107 },
108 )
109 @jwt_required
110 def get(self, search_term):
111 """ Getlist of application's data by term """
112 user_uuid = get_jwt_identity()
113 try:
114 page = int(request.args.get('page'))
115 except (ValueError, TypeError):
116 page = 1
117 return ApplicationService.search_application_data(search_term, page, user_uuid)
120@api.route("/genres")
121class ApplicatioGenresResource(Resource):
122 @api.doc(
123 "Get application genres",
124 responses={
125 200: ("Application genres data successfully sent", genres_resp),
126 401: ("Authentication required"),
127 },
128 )
129 @jwt_required
130 def get(self):
131 """ Get application genres """
132 uuid = get_jwt_identity()
133 return ApplicationService.get_ordered_genres(uuid)
136@api.route("/<int:content_id>/meta")
137class ApplicationMetaResource(Resource):
138 @api.doc(
139 "Get application-user (connected user) meta",
140 responses={
141 200: ("Application-User meta data successfully sent", meta_resp),
142 401: ("Authentication required"),
143 }
144 )
145 @jwt_required
146 def get(self, content_id):
147 """ Get application-user (connected user) meta """
148 user_uuid = get_jwt_identity()
150 return ContentService.get_meta(user_uuid, content_id)
152 content_meta = UserDto.content_meta
154 @api.doc(
155 "Update application-user (connected user) meta",
156 responses={
157 201: ("Application-User meta data successfully sent"),
158 401: ("Authentication required"),
159 404: "User or Application not found!",
160 },
161 )
162 @jwt_required
163 @api.expect(content_meta, validate=True)
164 def patch(self, content_id):
165 """ Update application-user (connected user) meta """
166 user_uuid = get_jwt_identity()
168 # Grab the json data
169 data = request.get_json()
171 return ContentService.update_meta(user_uuid, content_id, data)
174@api.route("/<int:content_id>/bad_recommendation")
175class ApplicationBadRecommendation(Resource):
176 bad_recommendation = ApplicationDto.application_bad_recommendation
178 @api.doc(
179 "Add application-user (connected user) bad recommendation",
180 responses={
181 200: ("Application-User bad recommendation successfully sent", meta_resp),
182 401: ("Authentication required"),
183 }
184 )
185 @jwt_required
186 @api.expect(bad_recommendation, validate=True)
187 def post(self, content_id):
188 """ Add application-user (connected user) bad recommendation """
189 user_uuid = get_jwt_identity()
191 # Grab the json data
192 data = request.get_json()
194 return ApplicationService.add_bad_recommendation(user_uuid, content_id, data)
197@api.route("/additional", doc={"params": {"page": {"in": "query", "type": "int", "default": 1}}})
198class ApplicationAdditionalResource(Resource):
199 @api.doc(
200 "Get list of the added Applications (by user)",
201 responses={
202 200: ("Application data successfully sent", data_resp),
203 401: ("Authentication required"),
204 },
205 )
206 @jwt_required
207 def get(self):
208 """ Get list of the added Applications (by user) """
209 user_uuid = get_jwt_identity()
211 parser = reqparse.RequestParser()
212 parser.add_argument('page', type=int, default=1)
213 args = parser.parse_args()
215 return ApplicationService.get_additional_application(user_uuid, args["page"])
218@api.route("/additional/<int:app_id>")
219class ApplicationAdditionalValidationResource(Resource):
220 @api.doc(
221 "Validate (put) added Applications (by user)",
222 responses={
223 201: ("Additional application data successfully validated"),
224 401: ("Authentication required"),
225 403: ("Permission missing"),
226 404: ("User or application not found!"),
227 },
228 )
229 @jwt_required
230 def put(self, app_id):
231 """ Validate (put) added Applications (by user) """
232 user_uuid = get_jwt_identity()
234 return ApplicationService.validate_additional_application(user_uuid, app_id)
236 @api.doc(
237 "Decline (delete) added Applications (by user)",
238 responses={
239 201: ("Additional application successfully deleted"),
240 401: ("Authentication required"),
241 403: ("Permission missing"),
242 404: ("User or application not found!"),
243 },
244 )
245 @jwt_required
246 def delete(self, app_id):
247 """ Decline (delete) added Applications (by user) """
248 user_uuid = get_jwt_identity()
250 return ApplicationService.decline_additional_application(user_uuid, app_id)