Commit 78feea0e authored by Willard's avatar Willard

Add filters for searching

parent dd91f897
from canteeneo import app, db, auth
from flask import jsonify, request, g
from flask.views import MethodView
from models import Dish, Stall, Location, User, DishReview, StallReview
from models import Dish, Stall, Location, User, DishReview, StallReview, DishType, DishCuisine
from datetime import datetime
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired
@app.route('/api/locations')
def locations():
data = [{'id': loc.id, 'name': loc.name} for loc in Location.query.all()]
return jsonify(data)
@app.route('/api/types')
def types():
data = [{'id': dish_type.id, 'name': dish_type.name} for dish_type in DishType.query.all()]
return jsonify(data)
@app.route('/api/cuisines')
def cuisines():
data = [{'id': cuisine.id, 'name': cuisine.name} for cuisine in DishCuisine.query.all()]
return jsonify(data)
@app.route('/api/all')
def all():
data = {'update_time': str(datetime.now()), 'dishes': [], 'stalls': [], 'locations': []}
......@@ -31,8 +46,15 @@ def all():
@app.route('/api/search/dish')
def search():
query = request.args.get('name')
result = Dish.query.filter(Dish.name.like('%' + query + '%')).all()
name = request.args.get('name')
types = [int(s) for s in request.args.get('type_filters')]
cuisines = [int(s) for s in request.args.get('cuisine_filters')]
locations = [int(s) for s in request.args.get('location_filters')]
result = Dish.query.filter(
Dish.name.like('%' + name + '%'),
Dish.type_id.in_(types), Dish.cuisine_id.in_(cuisines)
).join(Dish.stall).filter(Stall.location_id.in_(locations)).all()
data = []
for dish in result:
data.append({
......@@ -42,6 +64,7 @@ def search():
'stall_name': dish.stall.name,
'image_path': dish.image_path
})
return jsonify(data)
@app.route('/api/users/new', methods=['POST'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment