Ejemplo n.º 1
0
    def createRecipeSave(self, request):
        """
        Save recipes
        """
        data = json.load(request.content)
        data = {k.encode('utf-8'): v for (k, v) in data.items()}
        recipe = Recipe()
        recipe.name = data['name']
        recipe.recipeYield = str(data.get('recipeYield'))
        recipe.user = ICurrentUser(request)
        recipe.urlKey = urlify(recipe.user.email, recipe.name)
        if Recipe.objects(urlKey=recipe.urlKey).first():
            return ERROR(message=ResponseMsg.renameRecipe)

        recipe.author = data.get('author', USER().anonymous.givenName)
        for field in ['tags', 'ingredients', 'instructions']:
            if data.get(field):
                for i in data[field]:
                    recipe[field].append(i)

        recipe.save()
        return OK(message=recipe.urlKey)