Skip to content

Commit 5d7e556

Browse files
Got filtering working for tags, along with list of tags
1 parent e88f5b0 commit 5d7e556

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

lib/app/RecipeQuery.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
class Queries(BaseQuery):
44

5-
def ingredientsInclude(self=[]):
5+
def ingredientsInclude(self,ingredients=[]):
66
return self.filter(any(i in self.type.ingredients for i in ingredients))
77

88
def ingredientsContainAll(self,ingredients=[]):
99
return self.filter(all(i in self.type.ingredients for i in ingredients))
1010

1111
def tagsInclude(self,tags=[]):
12-
self.filter(any(tag in self.type.tags for tag in tags))
12+
return self.filter({'tags':{'$in':tags}}).all()
1313

1414
def tagsIncludeAll(self,tags=[]):
15-
return self.filter(all(tag in self.type.tags for tag in tags))
15+
return self.filter({'tags':{'$in':tags}}).all()
1616

1717
def titleIsLike(self,match):
1818
return self.filter(match in self.type.title)

lib/app/TagsDelegate.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from app.recipe import Recipe
2+
class TagsDelegate:
3+
4+
def getTags(self):
5+
recipes=Recipe.query.all()
6+
tag_lists=map(lambda r: r.tags,recipes)
7+
return set([tag for tags in tag_lists for tag in tags])
8+
9+
def tagsInclude(self,tags):
10+
recipes = Recipe.query.tagsInclude(tags=tags)
11+
if recipes is None:
12+
return []
13+
return list(map((lambda r: r.toJson()), recipes))

lib/app/routes.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@
44
from flask.json import jsonify
55
from flask import request
66
from app.FileDelegate import FileDelegate
7+
from app.TagsDelegate import TagsDelegate
78
from app.recipe import Recipe
89

910

1011
@app.route('/', methods=['GET'])
1112
@app.route('/status')
1213
def index():
13-
response = {'status': "OK", 'Time': datetime.datetime.now(),
14-
'project': 'recipes'}
14+
response = {
15+
'status': "OK",
16+
'Time': datetime.datetime.now(),
17+
'project': 'recipes'
18+
}
1519
return jsonify(response)
1620

1721

1822
@app.route('/search/byTags', methods=['GET'])
1923
def searchByTags():
20-
return jsonify("{}")
24+
tags = request.args['tags'].split(',')
25+
recipes = TagsDelegate().tagsInclude(tags)
26+
return jsonify(recipes)
2127

2228

2329
@app.route('/search/byTags/all', methods=['GET'])
@@ -32,7 +38,6 @@ def getAll():
3238
return jsonify(toJson)
3339

3440

35-
3641
@app.route("/search/ByIngredients", methods=['GET'])
3742
def searchByIngredients():
3843
return jsonify("{}")
@@ -45,29 +50,34 @@ def searchByIngredientsAll():
4550

4651
@app.route("/recipes/put", methods=['POST'])
4752
def addRecipe():
48-
delegate=FileDelegate()
49-
new_file=request.files['file']
53+
delegate = FileDelegate()
54+
new_file = request.files['file']
5055
if new_file:
51-
json=delegate.addNew(file=request.files['file'],properties=request.args)
56+
json = delegate.addNew(
57+
file=request.files['file'], properties=request.args)
5258
return jsonify(json)
5359
return jsonify("{'error':'no file found'}")
5460

5561

5662
@app.route("/recipes/edit", methods=['POST'])
5763
def editRecipe():
58-
delegate=FileDelegate()
59-
json=delegate.edit(file=request.files['file'],properties=request.values)
64+
delegate = FileDelegate()
65+
json = delegate.edit(file=request.files['file'], properties=request.values)
6066
return jsonify(json)
6167

62-
@app.route("/recipes/remove",methods=['DELETE'])
68+
69+
@app.route("/recipes/remove", methods=['DELETE'])
6370
def removeRecipe():
64-
delegate=FileDelegate()
65-
json=delegate.remove(file=request.files['file'],properties=request.values)
71+
delegate = FileDelegate()
72+
json = delegate.remove(
73+
file=request.files['file'], properties=request.values)
6674
return jsonify(json)
6775

76+
6877
@app.route('/recipes/tags', methods=['GET'])
6978
def getTags():
70-
return jsonify("{}")
79+
tags = TagsDelegate().getTags()
80+
return jsonify(list(tags))
7181

7282

7383
@app.route('/recipes/ingredients', methods=['GET'])

0 commit comments

Comments
 (0)