def calc_ingredient_name(cls, ingredient): label_name = ingredient.get_label_name() inci_name = None if len(ingredient.inci_names): inci_name = ingredient.inci_names[0] # build the name as "(INCI name|Label name) E-number" if len(ingredient.e_numbers) and not label_name and not inci_name: return [Language.get_by_code('en'), ingredient.e_numbers[0]] if len(ingredient.e_numbers) and inci_name: return [ Language.get_nc_lang(), '(%s) %s' % (inci_name, ingredient.e_numbers[0]) ] if len(ingredient.e_numbers) and label_name: return [ label_name.language.get(), '(%s) %s' % (label_name.name, ingredient.e_numbers[0]) ] # build the name as "INCI name" if inci_name: return [Language.get_nc_lang(), inci_name] # build the name as "Label name" if label_name: return [label_name.language.get(), label_name.name] return [Language.get_unknown(), 'UNKNOWN']
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 setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.users = [] self.users.append(User()) self.users[0].name = 'Foo Bar' self.users[0].put() self.langs = [] self.langs.append(Language()) self.langs[0].name = 'English' self.langs[0].code = 'en' self.langs[0].put() self.langs.append(Language()) self.langs[1].name = 'Danish' self.langs[1].code = 'da' self.langs[1].put() self.langs.append(Language()) self.langs[2].name = 'Default (INCI)' self.langs[2].code = 'nc' self.langs[2].put() self.label_names = [] self.label_names.append(LabelName()) self.label_names[0].name = 'Vand' self.label_names[0].language = self.langs[1].key self.label_names[0].put() self.label_names.append(LabelName()) self.label_names[1].name = 'Water' self.label_names[1].language = self.langs[0].key self.label_names[1].put() self.label_names.append(LabelName()) self.label_names[2].name = 'Aqua' self.label_names[2].language = self.langs[2].key self.label_names[2].put() self.wiki_links = [] self.wiki_links.append(IngredientWikiLink()) self.wiki_links[0].ingredient = None self.wiki_links[0].link = 'http://en.wikipedia.org/wiki/water' self.wiki_links[0].is_valid = True self.wiki_links[0].creator = None self.wiki_links[0].language = None self.wiki_links[0].put() self.wiki_links.append(IngredientWikiLink()) self.wiki_links[1].ingredient = None self.wiki_links[1].link = 'http://da.wikipedia.org/wiki/vand' self.wiki_links[1].is_valid = True self.wiki_links[1].creator = None self.wiki_links[1].language = None self.wiki_links[1].put() self.ingredients = [] self.ingredients.append(Ingredient()) self.ingredients[0].put()
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)
def add_ingredient(self, request): ''' Add a new 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 language language = Language.find_by_code(request.ingredient.language) if not language: language = Language.get_unknown() # find or create the label name that is requested is_new = True name = request.ingredient.label_name label_name = None if LabelName.exists(name): label_name = LabelName.find_by_name(name) is_new = False else: label_name = LabelName.create(name, language, user) # find or create the ingredient. If it is a new ingredient, we first # create it and then map it to the label name ingredient = None if is_new: if language.is_default(): ingredient = Ingredient.create(user, name) else: ingredient = Ingredient.create(user) IngredientLabelName.create(ingredient, label_name, user) else: ingredient = IngredientLabelName.find_by_label_name(label_name).ingredient.get() # create a mapping for the ingredient and the product mapping = ProductIngredient.add_entry(product, ingredient, user) return DatastoreKeyResponse(key=mapping.key.urlsafe())
def get_data(self, request): ''' Retrieve translation for data items ''' language = Language.get_by_code(request.lang) translationKey = DataTranslationKey.load(request.translationKey) translation = Translation.load(language.key, translationKey.key) return TranslationResponse(translation=translation.value)
def get_ui(self, request): ''' Retrieve translation for UI text ''' language = Language.get_by_code(request.lang) translationKey = UITranslationKey.load(request.translationKey) translation = Translation.load(language.key, translationKey.key) return TranslationResponse(translation=translation.value)
def get_list(self, request): ''' Returns a list of all the languages supported by aWare ''' languages = Language.query().fetch() collection = [] for language in languages: msg = LanguageMessage() msg.code = language.code msg.name = language.name collection.append(msg) return LanguageCollection(items=collection)