Пример #1
0
 def handle_ingredient(self, recipe, name, amount, ingredientid, grams):
     self.stdout.write(u"\t - ingredient: %s | %s" % (amount, name))
     # Look for food
     ingredient = Ingredient.objects.search(name)
     if not ingredient:
         if self.create_foods:
             ingredient = Ingredient()
             ingredient.NDB_No = self.ingredient_NDB_No_generator();
             ingredient.Shrt_Desc = name
             ingredient.Long_Desc = name
             ingredient.save()
         else:
             raise Exception("Ingredient failed!")
     ing_amount = IngredientWithAmount(Recipe=recipe, Ingredient=ingredient)
     ing_amount.HumanReadable = u"%s %s" % (amount, name)
     ing_amount.GramsEquiv = grams
     # Quantity and Units
     items = amount.split()
     try:
         ing_amount.Quantity = float(sum(Fraction(part) for part in items[0].split()))
         if len(items) == 1:
             ing_amount.Units = u"unit"
         else:
             ing_amount.Units = items[-1]
         self.stdout.write(u"\t   + ingredient: %s" % (ingredient.Shrt_Desc))
         self.stdout.write(u"\t   + quantity: %s" % (ing_amount.Quantity))
         self.stdout.write(u"\t   + units: %s" % (ing_amount.Units))
         ing_amount.save()
     except Exception as e:
         self.stderr.write(str(e))
         ingredient.delete()
         raise Exception("Quanty and units not parseable: %r" % amount)
 def handle_ingredient(self, recipe, name, amount, ingredientid, grams):
     self.stdout.write(u"\t - ingredient: %s | %s" % (amount, name))
     # Look for food
     ingredient = Ingredient.objects.search(name)
     if not ingredient:
         if self.create_foods:
             ingredient = Ingredient()
             ingredient.NDB_No = self.ingredient_NDB_No_generator()
             ingredient.Shrt_Desc = name
             ingredient.Long_Desc = name
             ingredient.save()
         else:
             raise Exception("Ingredient failed!")
     ing_amount = IngredientWithAmount(Recipe=recipe, Ingredient=ingredient)
     ing_amount.HumanReadable = u"%s %s" % (amount, name)
     ing_amount.GramsEquiv = grams
     # Quantity and Units
     items = amount.split()
     try:
         ing_amount.Quantity = float(
             sum(Fraction(part) for part in items[0].split()))
         if len(items) == 1:
             ing_amount.Units = u"unit"
         else:
             ing_amount.Units = items[-1]
         self.stdout.write(u"\t   + ingredient: %s" %
                           (ingredient.Shrt_Desc))
         self.stdout.write(u"\t   + quantity: %s" % (ing_amount.Quantity))
         self.stdout.write(u"\t   + units: %s" % (ing_amount.Units))
         ing_amount.save()
     except Exception as e:
         self.stderr.write(str(e))
         ingredient.delete()
         raise Exception("Quanty and units not parseable: %r" % amount)
Пример #3
0
    def handle_recipe(self, data, url):
        if Recipe.objects.filter(URL=url).exists():
            self.stdout.write(u"\t   already in database")
            return 1, False 

        recipe = Recipe(Title=data[u"name"], URL=url)
        self.stdout.write(u"\t - author: %s" % data.get(u"author", None))
        recipe.Author = data.get(u"author", None)
        self.stdout.write(u"\t - image: %s" % data.get(u"image", None))
        recipe.Image = data.get(u"image", None)
        self.stdout.write(u"\t - ratingValue: %s" % data.get(u"ratingValue", None))
        recipe.Rating = data.get(u"ratingValue", None)
        self.stdout.write(u"\t - description: %s" % data.get(u"description", '')[:60])
        recipe.Description = data.get(u"description", None)


        # Produces
        self.stdout.write(u"\t - Produces: %s" % data[u"name"])
        ingredient = Ingredient.objects.search(data[u"name"])
        if not ingredient:
            if self.create_foods:
                ingredient = Ingredient()
                ingredient.NDB_No = self.ingredient_NDB_No_generator();
                ingredient.Shrt_Desc = data[u"name"]
                ingredient.Long_Desc = data[u"name"]
                ingredient.save()
            else:
                return None, False
        recipe.Produces = ingredient

        # Yields
        self.stdout.write(u"\t - yields: %s" % data.get(u"yields", None))
        yields = data.get(u"yields", None)
        if yields:
            items = [it for it in yields.split() if it.isdigit()]
            if items:
                recipe.Yields = items[0]
            self.stdout.write(u"\t   + yields: %s -> %s" % (yields, recipe.Yields))

        # PrepTime and CookTime
        def minutes(value_str):
            if value_str:
                t = parse_duration(value_str)
                return int(t.seconds/60.)
            return None
        self.stdout.write(u"\t - prepTime: %s" % minutes(data.get(u"prepTime", None)))
        self.stdout.write(u"\t - cookTime: %s" % minutes(data.get(u"cookTime", None)))
        recipe.PrepTime = minutes(data.get(u"prepTime", None))
        recipe.CookTime = minutes(data.get(u"cookTime", None))

        recipe.save()

        # Ingredients
        try:
            for name, amount, ingredientid, grams in zip(data[u"ingredients-name"], data[u"ingredients-amount"], data["ingredients-data-ingredientid"], data[u"ingredients-data-grams"]):
                self.handle_ingredient(recipe, name, amount, ingredientid, grams)
        except Exception as e:
            self.stderr.write(str(e))
            recipe.delete() # Al borrar el recipe, se borran todos los relacionados (IngredientWithAmount) para no quedar huérfanos
            return None, False

        #Directions
        try:
            i = 1
            self.stdout.write(u"\t - directions:")
            for direction in data.get(u"directions"):
                d = Direction(Recipe=recipe, StepNumber=i)
                d.Description = direction
                d.save()
                self.stdout.write(u"\t   + %s" % d)
                i += 1
        except Exception as e:
            self.stderr.write(str(e))
            recipe.delete() # Al borrar el recipe, se borran todos los relacionados (IngredientWithAmount) para no quedar huérfanos
            return None, False

        return recipe, True
    def handle_recipe(self, data, url):
        if Recipe.objects.filter(URL=url).exists():
            self.stdout.write(u"\t   already in database")
            return 1, False

        recipe = Recipe(Title=data[u"name"], URL=url)
        self.stdout.write(u"\t - author: %s" % data.get(u"author", None))
        recipe.Author = data.get(u"author", None)
        self.stdout.write(u"\t - image: %s" % data.get(u"image", None))
        recipe.Image = data.get(u"image", None)
        self.stdout.write(u"\t - ratingValue: %s" %
                          data.get(u"ratingValue", None))
        recipe.Rating = data.get(u"ratingValue", None)
        self.stdout.write(u"\t - description: %s" %
                          data.get(u"description", '')[:60])
        recipe.Description = data.get(u"description", None)

        # Produces
        self.stdout.write(u"\t - Produces: %s" % data[u"name"])
        ingredient = Ingredient.objects.search(data[u"name"])
        if not ingredient:
            if self.create_foods:
                ingredient = Ingredient()
                ingredient.NDB_No = self.ingredient_NDB_No_generator()
                ingredient.Shrt_Desc = data[u"name"]
                ingredient.Long_Desc = data[u"name"]
                ingredient.save()
            else:
                return None, False
        recipe.Produces = ingredient

        # Yields
        self.stdout.write(u"\t - yields: %s" % data.get(u"yields", None))
        yields = data.get(u"yields", None)
        if yields:
            items = [it for it in yields.split() if it.isdigit()]
            if items:
                recipe.Yields = items[0]
            self.stdout.write(u"\t   + yields: %s -> %s" %
                              (yields, recipe.Yields))

        # PrepTime and CookTime
        def minutes(value_str):
            if value_str:
                t = parse_duration(value_str)
                return int(t.seconds / 60.)
            return None

        self.stdout.write(u"\t - prepTime: %s" %
                          minutes(data.get(u"prepTime", None)))
        self.stdout.write(u"\t - cookTime: %s" %
                          minutes(data.get(u"cookTime", None)))
        recipe.PrepTime = minutes(data.get(u"prepTime", None))
        recipe.CookTime = minutes(data.get(u"cookTime", None))

        recipe.save()

        # Ingredients
        try:
            for name, amount, ingredientid, grams in zip(
                    data[u"ingredients-name"], data[u"ingredients-amount"],
                    data["ingredients-data-ingredientid"],
                    data[u"ingredients-data-grams"]):
                self.handle_ingredient(recipe, name, amount, ingredientid,
                                       grams)
        except Exception as e:
            self.stderr.write(str(e))
            recipe.delete(
            )  # Al borrar el recipe, se borran todos los relacionados (IngredientWithAmount) para no quedar huérfanos
            return None, False

        #Directions
        try:
            i = 1
            self.stdout.write(u"\t - directions:")
            for direction in data.get(u"directions"):
                d = Direction(Recipe=recipe, StepNumber=i)
                d.Description = direction
                d.save()
                self.stdout.write(u"\t   + %s" % d)
                i += 1
        except Exception as e:
            self.stderr.write(str(e))
            recipe.delete(
            )  # Al borrar el recipe, se borran todos los relacionados (IngredientWithAmount) para no quedar huérfanos
            return None, False

        return recipe, True