/
QueryViews.py
135 lines (110 loc) · 4.72 KB
/
QueryViews.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import logging
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from django.template.context import RequestContext
from django.utils import simplejson
from models import Recipe, Ingredient, Tag
from helpers import uniq
def bucketize(arr, extractor):
buckets = {}
for i in arr:
key = extractor(i).lower()[:1]
if not key in buckets:
buckets[key] = []
buckets[key].append(i)
keys = buckets.keys()
keys.sort()
return buckets,keys
################################################################################
class RecipesByTitleHandler(webapp.RequestHandler):
def get(self):
q = Recipe.all()
recipes = [r for r in q]
buckets,keys = bucketize(recipes, lambda x: x.title)
templatevalues = RequestContext(self.request, {
'recipes' : recipes,
'buckets' : buckets,
'keys' : keys,
})
path = os.path.join(os.path.dirname(__file__), 'recipes_by_title.html')
self.response.out.write(template.render(path, templatevalues))
################################################################################
class IngredientsByNameHandler(webapp.RequestHandler):
def get(self):
q = Ingredient.all()
ingredients = [i for i in q if i.quantifiedingredient_set.count() > 0]
buckets,keys = bucketize(ingredients, lambda x: x.name)
templatevalues = RequestContext(self.request, {
'ingredients' : ingredients,
'buckets' : buckets,
'keys' : keys
})
path = os.path.join(os.path.dirname(__file__), 'ingredients_by_name.html')
self.response.out.write(template.render(path, templatevalues))
################################################################################
class RecipesByIngredientHandler(webapp.RequestHandler):
def get(self, ingredient_key):
i = Ingredient.get(ingredient_key)
if i == None:
# TODO: error page?
return
# Gather all the recipes for all the QIs for this ingredient
recipes = uniq([qi.recipe for qi in i.quantifiedingredient_set],
lambda x: x.title)
buckets,keys = bucketize(recipes, lambda x: x.title)
templatevalues = RequestContext(self.request, {
'ingredient' : i,
'recipes' : recipes,
'buckets' : buckets,
'keys' : keys
})
path = os.path.join(os.path.dirname(__file__), 'recipes_by_ingredient.html')
self.response.out.write(template.render(path, templatevalues))
################################################################################
class RecipesByStickyHandler(webapp.RequestHandler):
def get(self):
have = []
havenot = []
for r in Recipe.all():
if r.stickies.count(1) > 0: have.append(r)
else: havenot.append(r)
templatevalues = RequestContext(self.request, {
'have' : have,
'havenot' : havenot,
})
path = os.path.join(os.path.dirname(__file__), 'recipes_by_sticky.html')
self.response.out.write(template.render(path, templatevalues))
################################################################################
class TagsByNameHandler(webapp.RequestHandler):
def get(self):
q = Tag.all()
tags = [t for t in q]
buckets,keys = bucketize(tags, lambda t: t.tag)
templatevalues = RequestContext(self.request, {
'tags' : tags,
'buckets' : buckets,
'keys' : keys,
})
path = os.path.join(os.path.dirname(__file__), 'tags_by_name.html')
self.response.out.write(template.render(path, templatevalues))
################################################################################
class RecipesByTagHandler(webapp.RequestHandler):
def get(self, tag_name):
t = Tag.get_by_name(tag_name)
if t == None:
# TODO: error page?
return
# Gather all the recipes for all the QIs for this ingredient
recipes = uniq([Recipe.get(key) for key in t.tagged],
lambda x: x.title)
buckets,keys = bucketize(recipes, lambda x: x.title)
templatevalues = RequestContext(self.request, {
'tag' : t,
'recipes' : recipes,
'buckets' : buckets,
'keys' : keys
})
path = os.path.join(os.path.dirname(__file__), 'recipes_by_tag.html')
self.response.out.write(template.render(path, templatevalues))