1
+ from tkinter import CURRENT
1
2
from unicodedata import category
2
3
from flask import Flask , request , abort , jsonify
3
4
from flask_cors import CORS
@@ -48,24 +49,23 @@ def after_request(response):
48
49
Create an endpoint to handle GET requests
49
50
for all available categories
50
51
"""
51
- @app .route ("/categories" )
52
+ @app .route ("/categories" , methods = [ "GET" ] )
52
53
def retrieve_categories ():
53
- selection = Category .query .order_by (Category .id ).all ()
54
- #current_categories = paginate_questions(request, selection)
55
- formatted_category = [category .format () for category in selection ]
56
- if len (formatted_category ) == 0 :
57
- abort (404 )
58
-
59
- return jsonify (
60
- {
61
- "success" : True ,
62
- "categories" : formatted_category ,
63
- "total_category" : len (Category .query .all ())
64
- }
65
- )
66
-
54
+ try :
55
+ selection = Category .query .order_by (Category .id ).all ()
56
+ print (f'selection:{ selection } ' )
57
+
58
+ formatted_category = [category .format () for category in selection ]
59
+ print (f'formatted_category:{ formatted_category } ' )
60
+ if len (formatted_category ) == 0 :
61
+ abort (404 )
67
62
68
-
63
+ response = {
64
+ "categories" : formatted_category ,
65
+ }
66
+ return jsonify (response )
67
+ except :
68
+ abort (404 )
69
69
70
70
"""
71
71
@TODO:
@@ -85,34 +85,38 @@ def retrieve_questions():
85
85
page = request .args .get ("page" , 1 , type = int )
86
86
print (f'page_num:{ page } ' )
87
87
CURRENT_CATEGORY_ID = page
88
- all_categories = Category .query .all ()
89
- formatted_cat = {cat .id :cat .type for cat in all_categories }
90
-
91
- #print(f'selection:{selection}')
92
- current_category = formatted_cat [CURRENT_CATEGORY_ID ]
93
- print (f"current_category:{ current_category } " )
94
-
95
- #print(f'current_question:{current_question}')
96
-
97
- selection = Question .query .filter (Question .category == CURRENT_CATEGORY_ID ).order_by (Question .id ).all ()
98
- #current_question = paginate_questions(request, selection)
99
- print (f'selection:{ selection } ' )
100
- questions = paginate_questions (request , selection )
101
- #questions = [question.format() for question in selection]
102
- print (f'questions:{ questions } ' )
103
- #if len(questions) == 0:
104
- #abort(404)
105
-
106
- return jsonify (
107
- {
108
- "success" : True ,
109
- "questions" : questions ,
110
- "totalQuestion" : len (selection ),
111
- "currentCategory" :current_category ,
112
- "categories" :formatted_cat ,
113
- },
88
+ try :
89
+ all_categories = Category .query .all ()
90
+ formatted_cat = {cat .id :cat .type for cat in all_categories }
91
+
92
+ #print(f'selection:{selection}')
93
+ current_category = formatted_cat [CURRENT_CATEGORY_ID ]
94
+ print (f"current_category:{ current_category } " )
114
95
115
- )
96
+ #print(f'current_question:{current_question}')
97
+
98
+ selection = Question .query .filter (Question .category == CURRENT_CATEGORY_ID ).order_by (Question .id ).all ()
99
+ #current_question = paginate_questions(request, selection)
100
+ print (f'selection:{ selection } ' )
101
+ if len (selection )>= 10 :
102
+ questions = paginate_questions (request , selection )
103
+ else :
104
+ print (f'length: { len (selection )} ' )
105
+ questions = [question .format () for question in selection ]
106
+ print (f'questions:{ questions } ' )
107
+ if len (questions ) == 0 :
108
+ abort (404 )
109
+
110
+ response = {
111
+ "questions" : questions ,
112
+ "total_questions" : len (Question .query .all ()),
113
+ "categories" :formatted_cat ,
114
+ "current_category" :current_category
115
+ }
116
+
117
+ return jsonify (response )
118
+ except :
119
+ abort (422 )
116
120
"""
117
121
@TODO:
118
122
Create an endpoint to DELETE question using a question ID.
@@ -132,14 +136,13 @@ def delete_book(question_id):
132
136
selection = Question .query .order_by (Question .id ).all ()
133
137
current_question = paginate_questions (request , selection )
134
138
135
- return jsonify (
136
- {
139
+ response = {
137
140
"success" : True ,
138
141
"deleted" : question_id ,
139
- "books " : current_question ,
142
+ "question " : current_question ,
140
143
"total_books" : len (Question .query .all ()),
141
144
}
142
- )
145
+ return jsonify ( response )
143
146
144
147
except :
145
148
abort (422 )
@@ -153,7 +156,7 @@ def delete_book(question_id):
153
156
the form will clear and the question will appear at the end of the last page
154
157
of the questions list in the "List" tab.
155
158
"""
156
- @app .route ("/question " , methods = ["POST" ])
159
+ @app .route ("/questions " , methods = ["POST" ])
157
160
def create_question ():
158
161
body = request .get_json ()
159
162
print (f'body:{ body } ' )
@@ -184,14 +187,12 @@ def create_question():
184
187
selection = question .query .order_by (Question .id ).all ()
185
188
current_questions = paginate_questions (request , selection )
186
189
187
- return jsonify (
188
- {
189
- "success" : True ,
190
+ response = {
190
191
"created" : question .id ,
191
192
"question" : current_questions ,
192
- "total_books " : len (Question .query .all ()),
193
+ "total_questions " : len (Question .query .all ()),
193
194
}
194
- )
195
+ return jsonify ( response )
195
196
196
197
except :
197
198
abort (422 )
@@ -206,7 +207,7 @@ def create_question():
206
207
Try using the word "title" to start.
207
208
"""
208
209
209
- @app .route ("/question " , methods = ["POST" ])
210
+ @app .route ("/questions " , methods = ["POST" ])
210
211
def search_question ():
211
212
body = request .get_json ()
212
213
print (f'body:{ body } ' )
@@ -215,20 +216,24 @@ def search_question():
215
216
new_category = body .get ("category" , None )
216
217
new_difficulty = body .get ("difficulty" ,None )
217
218
218
- search = body .get ("search " ,None )
219
-
219
+ search = body .get ("searchTerm " ,None )
220
+ print ( f'search: { search } ' )
220
221
221
222
try :
222
223
if search :
223
- selection = Question .query .order_by (Question .id ).filter (Question .question .ilike ("%{}%" .format (search )))
224
- current_questions = paginate_questions (request ,selection )
225
-
226
- return jsonify (
227
- {
228
- "success" : True ,
229
- "questions" : current_questions ,
230
- "total_questions" : len (selection .all ()),
231
- })
224
+ selection = Question .query .order_by (Question .id ).filter (Question .question .ilike ("%{}%" .format (search ))).all ()
225
+ print (f'selection:{ selection } ' )
226
+ if len (selection )>= 10 :
227
+ current_question = paginate_questions (request ,selection )
228
+ else :
229
+ current_question = [question .format () for question in selection ]
230
+ print (f'current_questions:{ current_question } ' )
231
+
232
+ response = {
233
+ "questions" : current_question ,
234
+ "total_questions" : len (selection ),
235
+ }
236
+ return jsonify (response )
232
237
else :
233
238
abort (404 )
234
239
# question = Question(question=new_question, answer=new_answer, category=new_category,difficulty=new_difficulty)
@@ -258,7 +263,37 @@ def search_question():
258
263
categories in the left column will cause only questions of that
259
264
category to be shown.
260
265
"""
266
+ @app .route ("/categories/<int:category_id>/questions" , methods = ["GET" ])
267
+ def retrieve_questions_by_category (category_id ):
268
+ try :
269
+ all_categories = Category .query .all ()
270
+ formatted_cat = {cat .id :cat .type for cat in all_categories }
271
+
272
+ #print(f'selection:{selection}')
273
+ current_category = formatted_cat [category_id ]
274
+ print (f"current_category:{ current_category } " )
275
+
276
+ #print(f'current_question:{current_question}')
277
+
278
+ selection = Question .query .filter (Question .category == category_id ).order_by (Question .id ).all ()
279
+ #current_question = paginate_questions(request, selection)
280
+ print (f'selection:{ selection } ' )
281
+ questions = paginate_questions (request , selection )
282
+ #questions = [question.format() for question in selection]
283
+ print (f'questions:{ questions } ' )
284
+ #if len(questions) == 0:
285
+ #abort(404)
261
286
287
+ response = {
288
+ "success" : True ,
289
+ "questions" : questions ,
290
+ "total_questions" : len (selection ),
291
+ "current_category" :current_category ,
292
+ "categories" :formatted_cat
293
+ }
294
+ return jsonify (response )
295
+ except :
296
+ abort (422 )
262
297
"""
263
298
@TODO:
264
299
Create a POST endpoint to get questions to play the quiz.
@@ -271,6 +306,40 @@ def search_question():
271
306
and shown whether they were correct or not.
272
307
"""
273
308
309
+ @app .route ("/quizzes" , methods = ["GET" ])
310
+ def play_quiz ():
311
+ #try:
312
+ quiz_request = request .get_json ()
313
+
314
+ print (f'body:{ quiz_request } ' )
315
+ previous_questions = quiz_request ["previous_question" ]
316
+ current_category_id = quiz_request ["quiz_category" ]["id" ]
317
+
318
+ print (f'previous_questions:{ previous_questions } ' )
319
+ print (f'quiz_category:{ current_category_id } ' )
320
+
321
+ if (current_category_id == 0 ):
322
+ questions_in_category = db .session .query (Question .id ).all ()
323
+ else :
324
+ questions_in_category = db .session .query (Question .id ).filter (Question .category == current_category_id ).all ()
325
+
326
+ flat_question_in_cat = [i for sub in questions_in_category for i in sub ]
327
+ filtered_questions_in_category = [q for q in flat_question_in_cat if q not in previous_questions ]
328
+
329
+ if (len (filtered_questions_in_category )== 0 ):
330
+ response = {"question" :None }
331
+ else :
332
+ new_question_id = random .choice (filtered_questions_in_category )
333
+ question = Question .query .get (new_question_id )
334
+ response = {"question" :question .format ()}
335
+ return jsonify (response )
336
+
337
+ #print(f'selection:{selection}')
338
+ #new_question = Question.query.filter(Question.id !=previous_questions).filter(Question.category == str(current_category)).all()
339
+
340
+
341
+ # except:
342
+ # abort(422)
274
343
"""
275
344
@TODO:
276
345
Create error handlers for all expected errors
0 commit comments