def bookmarklet(self, request): """ Fetches the recipe for the url, saves the recipe, and returns a response to the chrome extension """ u = ICurrentUser(request) url = request.args['uri'][0] pageSource = yield treq.get(url).addCallback(treq.content) items = microdata.get_items(pageSource) recipesSaved = [] for i in items: itemTypeArray = [x.string for x in i.itemtype] if RECIPE_SCHEMA in itemTypeArray: recipe = i saveItem = Recipe.fromMicrodata(recipe, u.email) Recipe.saveOnlyOnce(saveItem) recipesSaved.append({ "name": saveItem.name, "urlKey": saveItem.urlKey }) break if len(recipesSaved) == 0: defer.returnValue( ClipResponse(status=RS.error, message=ResponseMsg.noRecipe)) defer.returnValue(ClipResponse(status=RS.ok, recipes=recipesSaved))
def deleteRecipe(self, request, urlKey): """ Delete a recipe from the recipe form """ recipe = Recipe.objects(urlKey=urlKey).first() if recipe: recipe.delete() return OK() else: return ERROR(message="Recipe not found")
def saveRecipe(self, request, urlKey): """ Save a recipe from the recipe edit form """ data = json.load(request.content) recipe = Recipe.objects(urlKey=urlKey).first() for k in data.keys(): if k not in ['user']: setattr(recipe, k, data[k]) recipe.save() return OK()
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)
def bookmarklet(self, request): """ Fetches the recipe for the url, saves the recipe, and returns a response to the chrome extension """ def returnResponse(status, recipes, message): """ Return the appropriate data structure to the http response """ data = {'status': status, 'recipes': recipes, 'message': message} defer.returnValue(json.dumps(data)) userEmail = self.user(request).email if not userEmail: returnResponse(status="error", recipes=[], message=ResponseMsg.not_logged_in) url = request.args['uri'][0] pageSource = yield treq.get(url).addCallback(treq.content) items = microdata.get_items(pageSource) recipeSaved = [] for i in items: itemTypeArray = [x.string for x in i.itemtype] if RECIPE_SCHEMA in itemTypeArray: recipe = i saveItem = Recipe.fromMicrodata(recipe, userEmail) Recipe.saveOnlyOnce(saveItem) recipeSaved.append({"name": saveItem.name, "urlKey": saveItem.urlKey}) break if len(recipeSaved) == 0: returnResponse(status="error", recipes=[], message=ResponseMsg.no_recipe) returnResponse(status="ok", recipes=recipeSaved, message=ResponseMsg.blank)
def createRecipeSave(self, request): # pragma: nocover # I suspect this is going to # drastically change when the chrome # extension branch lands, so i'm not # testing it. """ 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.author = data['author'] recipe.urlKey = urlify(recipe.user, recipe.name) for i in data['ingredients']: recipe.ingredients.append(i) for i in data['instructions']: recipe.instructions.append(i) recipe.save()
def getRecipe(self, request, urlKey): """ Return a specific recipe from its urlKey """ return Recipe.objects(urlKey=urlKey).first()
def recipeList(self, request): """ List all recipes """ # we are only sending limited information to the client because of security risk return Recipe.objects()
# """ # Modify the recipe objects by adding an urlKey # """ from mongoengine import connect from noms import DATABASE_NAME, urlify from noms.recipe import Recipe connect(db=DATABASE_NAME) recipes = Recipe.objects() for n in recipes: n.urlKey = urlify(n.user, n.name) n.save()
def recipeList(self, request): """ List all recipes """ return Recipe.objects()