def add_ingredient_wiki_link(self, request): ''' Add a Wikipedia link to an ingredient of a product ''' # retrieve the user user = User.get_by_urlsafe_key(request.user_key) if not user: message = 'No user with the key "%s" exists.' % request.user_key raise endpoints.NotFoundException(message) # retrieve the ingredient ingredient = Ingredient.get_by_urlsafe_key(request.ingredient_key) if not ingredient: message = 'No ingredient with the key "%s" exists.' % request.ingredient_key raise endpoints.NotFoundException(message) # retrieve the language language = Language.find_by_code(request.language) if not language: language = Language.get_unknown() link = request.wiki_link.link is_valid = request.wiki_link.is_valid wiki_link = None if IngredientWikiLink.exists(ingredient, language): wiki_link = IngredientWikiLink.find_by_ingredient_and_language(ingredient, language) wiki_link.link = link wiki_link.is_valid = is_valid wiki_link.put() else: wiki_link = IngredientWikiLink.create(ingredient, link, language, user, is_valid) return DatastoreKeyResponse(key=wiki_link.key.urlsafe())
def change_category(self, request): ''' Change the category of a Ingredient ''' # retrieve the requested Ingredient ingredient = Ingredient.get_by_urlsafe_key(request.ingredient_key) if not ingredient: message = 'No Ingredient with the key "%s" exists.' % request.ingredient_key raise endpoints.NotFoundException(message) # retrieve the user user = User.get_by_urlsafe_key(request.user_key) if not user: message = 'No user with the key "%s" exists.' % request.user_key raise endpoints.NotFoundException(message) # retrieve the category category = IngredientCategory.get_by_urlsafe_key(request.category_key) if not category: message = 'No category with the key "%s" exists.' % request.category_key raise endpoints.NotFoundException(message) # remove the ingredient from the old category old_category = ingredient.get_category() old_mapping = IngredientCategoryMapping.load(ingredient, old_category) if old_mapping: old_mapping.key.delete() # set the ingredient under the given category IngredientCategoryMessage.load(ingredient, category, user) # register the user as editor of the ingredient IngredientCategoryMessage.add_or_update(ingredient, user) return message_types.VoidMessage()
def map_ingredient(self, request): ''' Map an existing ingredient to a product ''' # retrieve the requested product product = Product.get_by_urlsafe_key(request.product_key) if not product: message = 'No product with the key "%s" exists.' % request.product_key raise endpoints.NotFoundException(message) # retrieve the user user = User.get_by_urlsafe_key(request.user_key) if not user: message = 'No user with the key "%s" exists.' % request.user_key raise endpoints.NotFoundException(message) # retrieve the ingredient ingredient = Ingredient.get_by_urlsafe_key(request.ingredient_key) if not ingredient: message = 'No ingredient with the key "%s" exists.' % request.ingredient_key raise endpoints.NotFoundException(message) # create the mapping mapping = ProductIngredient.add_entry(product, ingredient, user) return DatastoreKeyResponse(key=mapping.key.urlsafe())
def ingredient_list(self, request): ''' ingredients List ''' #retrive the requested ingredient ingredient = Ingredient.get_by_urlsafe_key(request.ingredient_key) if not ingredient: message = 'No product with the key "%s" exists.' % request.ingredient_key raise endpoints.NotFoundException(message) # retrieve the user user = User.get_by_urlsafe_key(request.user_key) if not user: message = 'No user with the key "%s" exists.' % request.user_key raise endpoints.NotFoundException(message) #Find lable name for the ingredient label_name = request.label_name if LabelName.exists(label_name): label_name = LabelName.find_by_name(label_name) else: label_name = None # retrieve the translation tanslation = Language.find_by_code(request.ingredient.language) if not language: tanslation = Language.get_unknown() #Find wiki-link for the ingredient link = request.wiki_link.link is_valid = request.wiki_link.is_valid wiki_link = None if IngredientWikiLink.exists(ingredient, tanslation): wiki_link = IngredientWikiLink.find_by_ingredient_and_language(ingredient, tanslation) wiki_link.link = link wiki_link.is_valid = is_valid wiki_link.put() else: wiki_link = None #Find Hazards for the Ingrdient hazards = HStatement.getClassifications(hazards) if not hazards: hazards = None #Find E_Number for the Ingredient e_numbers = request.e_numbers if Ingredient.e_numbers.exists(e_number): e_number = Ingredient.find_by_e_number(e_number)