def setHash(self, request, hash): """ Put a new static hash in the database for cache-busting """ CONFIG.staticHash = hash print 'New hash=%r' % CONFIG.staticHash return OK(message='hash=%r' % CONFIG.staticHash)
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 test_setHash(mockConfig, apiServer, localapi): """ Do I update the static hash setting via the API? Am I able to access the API with token-based auth? - using requestJSON(...user=) """ rq = requestJSON([], user=localapi) resp = yield apiServer.handler('setHash', rq, hash='orange-banana-peach') assert resp == OK(message="hash='orange-banana-peach'")
def test_deleteRecipe(mockConfig, apiServer, reqJS, recipes): """ Does /api/recipe/urlKey/delete... delete a specific recipe? """ # Error: recipe does not exist resp = yield apiServer.handler('deleteRecipe', reqJS, urlKey="stuff") assert resp == ERROR(message="Recipe not found") # Success: recipe exists resp = yield apiServer.handler('deleteRecipe', reqJS, urlKey='weird-sandwich-cory-') assert resp == OK()
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 test_saveRecipe(mockConfig, apiServer, weirdo, recipes): """ Does /api/recipe/urlKey/save ... save a specific recipe? """ content = dict( name='Weird soup', author='Weird Soup Man', ingredients=['weirdness', 'soup'], instructions=['mix together ingredients', 'heat through'], ) reqJS = requestJSON([], content=content) resp = yield apiServer.handler('saveRecipe', reqJS, urlKey='weird-sandwich-cory-') assert resp == OK()
def test_createRecipeSave(mockConfig, apiServer, weirdo, weirdSoupPOST): """ Do we save data from the create form successfully? """ reqJS = requestJSON([], content=weirdSoupPOST, session_user=weirdo) resp = yield apiServer.handler('createRecipeSave', reqJS) assert resp == OK(message='weirdo-gmail-com-weird-soup-') # the second time we should get an error because it exists reqJS = requestJSON([], content=weirdSoupPOST, session_user=weirdo) resp = yield apiServer.handler('createRecipeSave', reqJS) assert resp == ERROR(message=server.ResponseMsg.renameRecipe) anonJS = requestJSON([]) with raises(Forbidden): yield apiServer.handler('createRecipeSave', anonJS)
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)