def get_ingredient_name(self, request): ''' Retrieve a ingredients names ''' # get size of the name to return size = request.size # cursor state cursor = None if request.cursor: cursor = Cursor(urlsafe=request.cursor) # build the query query = Ingredient.query() names = None next_cursor = None more = False if cursor: names, next_cursor, more = query.fetch_page(size, start_cursor=cursor) else: names, next_cursor, more = query.fetch_page(size, start_cursor=cursor) if next_cursor: return IngredientHelper.create_collection(names, next_cursor.urlsafe(), more) return IngredientHelper.create_collection(names)
def handleGetRequest(self): # get the recent 10 ingredients added to datastore q = Ingredient.query(ancestor=INGREDIENT_KEY) ingredients = q.order(-Ingredient.created).fetch(10) self.addTemplateValue('ingredients', ingredients) self.setActivePage('Ingredients')
def _test_add_ingredient(self): # Input ingredients to database data = { 'ingredient': 'coffee' } res = self.app.post('/add_ingredients', data=data) ing = Ingredient.query(Ingredient.name == data['ingredient']).get() self.assertIsNotNone(ing, 'Ingredient not added')
def _test_add_duplicate_ingredient(self): # Attempt to add a same ingredient data = { # again 'ingredient': 'Tomatoes' } self.app.post('/add_ingredients', data=data) self.app.post('/add_ingredients', data=data) ingreds = Ingredient.query(Ingredient.name == data['ingredient']).fetch() self.assertEqual(len(ingreds), 1, "Duplicate ingredient added")
def get_ingredient_form(defaults=None, **kwargs): """Dynamically create the ingredients form, based on what is in the Ingredients datastore right now Returns an instance of the class, exactly as if we had called the constructor""" choices = [(x.name, x.name) for x in Ingredient.query().fetch()] class IngredientForm(Form): ingredients = MultiCheckboxField(choices=choices, default=defaults) new_ingredients = FieldList(StringField('Ingredient: '), label='Add New Ingredients', min_entries=5) def __init__(self, **kwargs): super(type(self), self).__init__(**kwargs) return IngredientForm(**kwargs)
def test_select_add_existing_ingredient(self): # Improved version of the data = { # _test_add_duplicate_ingredient test 'ingredient_names-0': 'cookies', # that checks a repeated item 'ingredient_checks-0': 'y', # will not be added 'ingredient_names-1': 'cookies', 'ingredient_checks-1': 'y' } res = self.app.post('/verify_ingredients', data=data) cookies = Ingredient.query(Ingredient.name == 'cookies').fetch() self.assertEqual(len(cookies), 1, 'Cookies added twice!')
def handlePostRequest(self): action = self.request.get('action') if action == 'sim-ocr-reading': ingredients = self.request.get('ocr_result') datastore = self.request.get('datastore') ingList = self.request.get('ingredients') if ingredients: reader = IngredientsFinder(ingredients) result = {} if datastore == "1": # use the ingredients from datastore ingList = Ingredient.query(ancestor=INGREDIENT_KEY).fetch() startTime = time.clock() for g in ingList: for n in g.inci_names: if reader.contains(n.lower()): result[n] = 'YES' else: result[n] = 'NO' elapsedTime = (time.clock() - startTime) self.addTemplateValue('ocrReadingResults', result.items()) self.addTemplateValue('elapsedTime', elapsedTime) else: if ingList: ings = ingList.splitlines() startTime = time.clock() for g in ings: if reader.contains(g): result[g] = 'YES' else: result[g] = 'NO' elapsedTime = (time.clock() - startTime) self.addTemplateValue('ocrReadingResults', result.items()) self.addTemplateValue('elapsedTime', elapsedTime) else: self.addTemplateValue( 'ocrReadingError', "You must either select the datastore as the provider of ingredients list or provide a list of ingredients (separated by new line)" ) else: self.addTemplateValue('ocrReadingError', "You must provide a valid OCR Result.") self.addCommon()
def tearDown(self): for ingred in Ingredient.query().fetch(): ingred.key.delete()