@@ -29,15 +29,17 @@ def serve():
2929def dump_sorted ():
3030 req_data = request .get_json ()
3131 cars = Car .query
32+ sort = False
3233 big_cars = []
33-
3434 if req_data ["Price" ]:
35+ sort = True
3536 for price in req_data ["Price" ]:
3637 mini_cars = cars .filter (Car .MSRP <= price [1 ])\
3738 .filter (Car .MSRP >= price [0 ])
3839 big_cars .append (mini_cars )
3940
4041 if req_data ["Type" ]:
42+ sort = True
4143 if len (big_cars ) == 0 :
4244 for car_type in req_data ["Type" ]:
4345 mini_cars = cars .filter_by (type = car_type )
@@ -51,6 +53,7 @@ def dump_sorted():
5153 big_cars = poses .copy ()
5254
5355 if req_data ["Seats" ]:
56+ sort = True
5457 if len (big_cars ) == 0 :
5558 for seat in req_data ["Seats" ]:
5659 mini_cars = cars .filter (Car .seats .between (
@@ -63,18 +66,22 @@ def dump_sorted():
6366 pos = big_cars [i ].filter (Car .seats .between (seat [0 ], seat [1 ]))
6467 poses .append (pos )
6568 big_cars = poses .copy ()
66- final_list = []
67- for car in big_cars :
68- for k in car .all ():
69- if k not in final_list :
70- final_list .append (k )
71- if req_data ["sort_by" ] == "MSRP" :
72- cars = sorted ([car .serialize () for car in final_list ],
73- key = lambda x : x [req_data ["sort_by" ]])
69+ if sort :
70+ final_list = []
71+ for car in big_cars :
72+ for k in car .all ():
73+ if k not in final_list :
74+ final_list .append (k )
75+ if req_data ["sort_by" ] == "MSRP" :
76+ cars = sorted ([car .serialize () for car in final_list ],
77+ key = lambda x : x [req_data ["sort_by" ]])
78+ else :
79+ cars = sorted ([car .serialize () for car in final_list ],
80+ key = lambda x : x [req_data ["sort_by" ]], reverse = True )
7481 else :
75- cars = sorted ([car .serialize () for car in final_list ],
82+ cars = sorted ([car .serialize () for car in cars . all () ],
7683 key = lambda x : x [req_data ["sort_by" ]], reverse = True )
77-
84+
7885 return jsonify (split (cars , 3 ))
7986
8087
@@ -83,13 +90,16 @@ def dump_rating():
8390 req_data = request .get_json ()
8491 cars = Car .query
8592 big_cars = []
93+ sort = False
8694 if req_data ["Price" ]:
95+ sort = True
8796 for price in req_data ["Price" ]:
8897 mini_cars = cars .filter (Car .MSRP <= price [1 ])\
8998 .filter (Car .MSRP >= price [0 ])
9099 big_cars .append (mini_cars )
91100
92101 if req_data ["Type" ]:
102+ sort = True
93103 if len (big_cars ) == 0 :
94104 for car_type in req_data ["Type" ]:
95105 mini_cars = cars .filter_by (type = car_type )
@@ -103,6 +113,7 @@ def dump_rating():
103113 big_cars = poses .copy ()
104114
105115 if req_data ["Seats" ]:
116+ sort = True
106117 if len (big_cars ) == 0 :
107118 for seat in req_data ["Seats" ]:
108119 mini_cars = cars .filter (Car .seats .between (
@@ -115,13 +126,17 @@ def dump_rating():
115126 pos = big_cars [i ].filter (Car .seats .between (seat [0 ], seat [1 ]))
116127 poses .append (pos )
117128 big_cars = poses .copy ()
118- final_list = []
119- for car in big_cars :
120- for k in car .all ():
121- if k not in final_list :
122- final_list .append (k )
123- cars = sorted ([car .serialize () for car in final_list ],
124- key = lambda x : x [req_data ["sort_by" ]], reverse = True )
129+ if sort :
130+ final_list = []
131+ for car in big_cars :
132+ for k in car .all ():
133+ if k not in final_list :
134+ final_list .append (k )
135+ cars = sorted ([car .serialize () for car in final_list ],
136+ key = lambda x : x [req_data ["sort_by" ]], reverse = True )
137+ else :
138+ cars = sorted ([car .serialize () for car in cars .all ()],
139+ key = lambda x : x [req_data ["sort_by" ]], reverse = True )
125140
126141 return jsonify (cars )
127142
0 commit comments