def save_recipe( name, markdown ): try: logging.info(name) logging.info(markdown) recipe = Recipe(name=name, markdown=markdown) if recipe.valid_filename() and recipe.valid_markdown(): app.storage.save(recipe) return response(key='recipe.was.saved.succesfully', statuscode=httpcode.OK) elif not recipe.valid_filename(): return response(key='recipe.was.save.failed.name.error', statuscode=httpcode.NOT_ACCEPTABLE) else: return response(key='recipe.was.save.failed.text.too.sort.error', statuscode=httpcode.NOT_ACCEPTABLE) except KeyError as error: app.logger.error(error) return response(key='missing.key', arguments=(str(error),), statuscode=httpcode.NOT_AVAILABLE) except ValueError as error: app.logger.error(error) return response(key='request.is.expecting.json', statuscode=httpcode.NOT_AVAILABLE)
def put_recipe(): if not is_authenticated(): return response(key='not.authority', statuscode=httpcode.UNAUTHORIZED) app.logger.info( "%s saved a recipe %s " % (session.get('username'), request.json[u'name'])) try: recipe = Recipe(name=request.json[u'name'], markdown=request.json[u'markdown']) if recipe.valid_filename() and recipe.valid_markdown(): app.storage.save(recipe) return response(key='recipe.was.saved.succesfully', statuscode=httpcode.OK) elif not recipe.valid_filename(): return response(key='recipe.was.save.failed.name.error', statuscode=httpcode.NOT_ACCEPTABLE) else: return response(key='recipe.was.save.failed.text.too.sort.error', statuscode=httpcode.NOT_ACCEPTABLE) except KeyError as error: app.logger.error(error) return response(key='missing.key', arguments=(str(error),), statuscode=httpcode.NOT_AVAILABLE) except ValueError as error: app.logger.error(error) return response(key='request.is.expecting.json', statuscode=httpcode.NOT_AVAILABLE)
def test_validating_recipe(self): recipe = Recipe(name="MusikkaPiirakka.md", markdown="Mustikka Mansikka Piirkka", created=None, modified=None) self.assertEqual( True, recipe.valid_filename() ) recipe.name = 'MusikkaPiirakka' self.assertEqual( False, recipe.valid_filename() ) recipe.name = 'MusikkaPiirakka.' self.assertEqual( False, recipe.valid_filename() ) self.assertEqual( True, Recipe('1MusikkaPiirakka2.md', '').valid_filename()) self.assertEqual( True, Recipe('1MusikkaPiirakka2.md', '').valid_filename()) self.assertEqual( True, Recipe('2MusikkaPiirakka2.md', '').valid_filename()) self.assertEqual( False, Recipe('.md', '').valid_filename()) self.assertEqual( True, Recipe('2.md', '').valid_filename()) self.assertEqual( True, Recipe('äö.md', '').valid_filename()) self.assertEqual( True, Recipe('a.md', '').valid_filename()) self.assertEqual( False, Recipe('a.MD', '').valid_filename())
def test_turning_recipe_to_json(self): recipe = Recipe('MustikkaPiirakka.MD', 'Lahnaaa....') recipe.to_json()