Ejemplo n.º 1
0
def getIngredient(key):
	client = memcache.Client()
	ingredient = client.get(key)
	if ingredient == None:
		ingredientDb = Ingredient.get(key)
		if ingredientDb != None:
			ingredient = createIngredientDb(ingredientDb)
			client.set(key, ingredient)
	return client.get(key)
Ejemplo n.º 2
0
def modifyIngredient(ingredientDb):
	client = memcache.Client()
	key = str(ingredientDb.key())
	# Update dishes old category
	ingredientObject = getIngredient(key)
	categoryKey = None
	ret = "Modify "
	ret += str(ingredientObject['category'])
	if ingredientDb.category != None:
		ret += "Had category"
		ret = ret + "<br/>"
		categoryKey = str(ingredientDb.category.key())
	if ingredientObject != None and ingredientObject['category']!=None and ingredientObject['category']['key'] != categoryKey:
		# Update category objects in cacge
		ret = ret + "Must remove"
		ret = ret + "<br/>"
		ret = ret + removeIngredientFromCategory(ingredientObject['category']['key'], key)
	# Create object
	newIngredientObject=createIngredientDb(ingredientDb)
	if ingredientObject == None or (categoryKey != None and (ingredientObject["category"] == None or ingredientObject["category"]['key'] != categoryKey)):
		addIngredientToCategory(categoryKey, newIngredientObject)
	else:
		if categoryKey!= None:
			# Modify ingredientDb in category
			category = client.get(categoryKey)
			if category != None:
				newIngredients = []
				for ingredient in category['ingredients']:
					if ingredient['key'] == key:
						ingredient = newIngredientObject
					newIngredients.append(ingredient)
				category['ingredients'] = newIngredients
				client.set(categoryKey, category)
	# Adds ingredient
	# TODO modify dishes containing this ingredient
	if (differenceToPropagate(ingredientObject, newIngredientObject)):
		for dishIngredient in ingredientDb.dishes:
			dishKey = str(dishIngredient.dish.key())
			title = dishIngredient.dish.title
			subtitle = dishIngredient.dish.subtitle
			description = dishIngredient.dish.description
			dishCategory = dishIngredient.dish.category
			modifyDish(dishKey, title, subtitle, description, dishCategory, dishIngredient.dish.eggFree, dishIngredient.dish.milkFree, dishIngredient.dish.codeModifier)
	client.set(key, newIngredientObject)
	# Adds ingredient to ingredient list
	ingredients = client.get(INGREDIENTS_KEY)
	if ingredients != None:
		newIngredients = []
		for ingredient in ingredients:
			if ingredient['key'] == key:
				ingredient = newIngredientObject
			newIngredients.append(ingredient)
		client.set(INGREDIENTS_KEY, newIngredients)
	return ret
Ejemplo n.º 3
0
def getIngredients():
	client = memcache.Client()
	ingredients = client.get(INGREDIENTS_KEY)
	if ingredients == None:
		ingredients = []
		ingredientsDb = Ingredient.gql("ORDER BY name")
		if ingredientsDb != None:
			for ingredientDb in ingredientsDb:
				ingredient = createIngredientDb(ingredientDb)
				ingredients.append(ingredient)
			client.set(INGREDIENTS_KEY, ingredients)
	return ingredients
def getIngredientCategoryWithIngredients(key):
	client = memcache.Client()
	category = client.get(key)
	if category == None:
		categoryDb = IngredientCategory.get(key)
		if categoryDb != None:
			ingredient = []
			for ingredientDb in categoryDb.ingredients:
				ingredient.append(createIngredientDb(ingredientDb))
			category={
				'key':key,
				'name':categoryDb.name,
				'ingredients':ingredient
			}
			client.set(key, category)
		else:
			return None
	return category
Ejemplo n.º 5
0
def addIngredient(ingredientDb):
	client = memcache.Client()
	key = str(ingredientDb.key())
	categoryKey = None
	if ingredientDb.category != None:
		categoryKey = str(ingredientDb.category.key())
	ingredient=createIngredientDb(ingredientDb)
	if categoryKey != None:
		addIngredientToCategory(categoryKey, ingredient)
	client.set(key, ingredient)
	ingredients = client.get(INGREDIENTS_KEY)
	if ingredients != None:
		newIngredients = []
		found = False
		for actualIngredient in ingredients:
			if actualIngredient['key'] == key:
				found = True
			newIngredients.append(actualIngredient)
		if not found:
			newIngredients.append(ingredient)
		client.set(INGREDIENTS_KEY, newIngredients)