def test(request): berg = Beverage(name='Ultimate Frozen Strawberry Margarita', description='A near perfect strawberry margarita with frozen strawberries and limeade concentrate.', image='http://images.media-allrecipes.com/userphotos/250x250/00/23/47/234705.jpg', recipe={'tequila':170, 'triple sec':56, 'frozen sliced strawberries':226}, tags=['sweet', 'cool']) berg.put() return HttpResponse("Hello, world. You're at the beverage index.")
def get_beverage(request): response_data = {} berg_name = None berg_tags = None berg_ingres = None if 'name' in request.GET: berg_name = request.GET['name'] if 'tags' in request.GET: tags = request.GET['tags'] berg_tags = [x.strip() for x in tags.split(",")] if 'ingredients' in request.GET: ingres = request.GET['ingredients'] berg_ingres = [x.strip() for x in ingres.split(",")] bergs = [] if berg_name is not None and berg_tags is not None: bergs = Beverage.query(Beverage.name == berg_name, Beverage.tags.IN(berg_tags)) elif berg_name is not None: bergs = Beverage.query(Beverage.name == berg_name) elif berg_tags is not None: bergs = Beverage.query(Beverage.tags.IN(berg_tags)) else: bergs = Beverage.query() for berg in bergs: if berg_ingres is not None: if not all (k in berg.recipe for k in berg_ingres): continue data = {} data['name'] = berg.name data['description'] = berg.description data['image'] = berg.image data['recipe'] = berg.recipe data['tags'] = berg.tags response_data[berg.key.id()] = data return HttpResponse(json.dumps(response_data, indent=2), content_type="application/json")
def add_beverage(request): if request.method != 'POST': return HttpResponse("should be post") if 'name' not in request.POST: return HttpResponse("name field is empty") if 'recipe' not in request.POST: return HttpResponse("name field is empty") berg = Beverage(name = request.POST['name'], recipe = json.loads(request.POST['recipe'])) if 'tags' in request.POST: tags = request.POST['tags'] berg.tags = [x.strip() for x in tags.split(",")] if 'description' in request.POST: berg.description = request.POST['description'] berg.put() return HttpResponse('completed')
def list_beverages(request): bergs = Beverage.query() response_data = {} for berg in bergs: data = {} data['name'] = berg.name data['description'] = berg.description data['image'] = berg.image data['recipe'] = berg.recipe data['tags'] = berg.tags response_data[berg.key.id()] = data return HttpResponse(json.dumps(response_data, indent=2), content_type="application/json")