def post(self, name): data = Post.parser.parse_args() if PostModel.find_by_title(data['title']): return {"message": "Title with the name '{}' already exist".format(data['title'])}, 400 post = PostModel(data['title'], data['desc']) try: post.save_to_db() return post.json(), 201 except: return {"message": "Error in order to save post"}, 500
def put(self, name): data = Post.parser.parse_args() post = PostModel.find_by_title(name) if post is None: post = PostModel(data['title'], data['desc']) else: post.title = data['title'] post.desc = data['desc'] post.save_to_db() return post.json()
def put(self, post_id): data = Post.parser.parse_args() post = PostModel.find_by_id(post_id) if post is None: post = PostModel(post_id, data['title']) else: post.title = data['title'] post.save_to_db() return post.json()
def put(self, title): data = Post.parser.parse_args() post = PostModel.find_by_title(title) if post: post.title = data['title'] else: post = PostModel(title, **data) post.save_to_db() return post.json()
def post(self, title): if PostModel.find_by_title(title): return {'message': "An post with title '{}' already exists.".format(title)}, 400 data = Post.parser.parse_args() data['user_id'] = current_identity.id post = PostModel(title, **data) try: post.save_to_db() except: return {"message": "An error occurred inserting the post."}, 500 return post.json(), 201
def post(self): data = Post.parser.parse_args() if PostModel.find_by_title(data['title']): return {'message': "A post with title '{}' already exists.".format(data['title'])}, 400 user_id = get_jwt_identity() # print(user_id) post = PostModel(data['title'], data['body'],user_id) try: post.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return post.json(), 201
def post(self, post_id): if PostModel.find_by_id(post_id): return { 'message': "An item with id: '{}' already exist".format(post_id) }, 400 data = Post.parser.parse_args() post = PostModel(post_id, data['title']) try: post.save_to_db() except: return {'message': 'An error inserting post'}, 500 return post.json(), 201
def post(self): data = self.parser.parse_args() # Foreign key constraint category_exist = CategoryModel.query.get(data['category_id']) if category_exist is None: return {'message': 'Category with such id doesnt exist.'} new_post = PostModel(**data) try: db.session.add(new_post) db.session.commit() except exc.IntegrityError as e: return {"message": e.args[0]}, 500 except: return {"message": "Something went wrong"}, 500 return {'data': new_post.json()}
class PostModelTests(unittest.TestCase): def setUp(self): self.post_model = PostModel( 1, 'www.test_perfil.com', 'www.test_photo.com', '@Test', 1, 10, [ { "login": "******", "comment": "comment1" }, { "login": "******", "comment": "comment2" } ] ) def test_post_model_params(self): self.assertEqual(1, self.post_model.id) self.assertEqual('www.test_perfil.com', self.post_model.url_perfil) self.assertEqual('www.test_photo.com', self.post_model.url_photo) self.assertEqual('@Test', self.post_model.login_user) self.assertEqual(1, self.post_model.liked) self.assertEqual(10, self.post_model.likers) self.assertEqual( [ { "login": "******", "comment": "comment1" }, { "login": "******", "comment": "comment2" } ], self.post_model.comments ) def test_json_return(self): output = { 'id': 1, 'url_perfil': 'www.test_perfil.com', 'url_photo': 'www.test_photo.com', 'login': '******', 'liked': 1, 'likers': 10, 'comments': [ { "login": "******", "comment": "comment1" }, { "login": "******", "comment": "comment2" } ] } self.assertEqual(output, self.post_model.json())