def create_post(): post_data = request.get_json(force=True, silent=True) if post_data == None: return "Bad request", 400 post = Post(post_data["title"], post_data["content"]) post.save() return json.dumps(post.to_dict()), 201
def test_14(self, m='Deleted post should be deleted'): p = Post({"title": "Another original post", "contents":"This is the content", "tags":"music,guitar,test", "author":"*****@*****.**"}) p.validate() p.save() retrieved_post = Post.get(p.pk()) retrieved_post.delete() retrieved_post.is_deleted() |should| equal_to(True) Post.get(p.pk()) |should| equal_to(None)
def test_12(self, m="Post should be retrieved by it's pk"): p = Post({"title": "This is the second test", "contents":"This is the content", "tags":"music,guitar,test", "author":"*****@*****.**"}) p.validate() p.is_new() |should| equal_to(True) p.save() p.is_new() |should_not| equal_to(True) retrieved_post = Post.get(p.pk()) retrieved_post |should| equal_to(p) retrieved_post.is_new() |should| equal_to(False)
def add(self, **kvals): if len(kvals): post = Post(kvals) post.validate() if post.is_valid(): post.save() return self.redirect("/post/%s" % post.pk()) else: post = Post() return self.render("post/add", post=post)
def add_new_post_service(title, body, tags=None): # cannot add two posts with the same title if Post.objects(title=title).first() is None: current_user = get_jwt_identity() new_post = Post(title=title, body=body, tags=tags, author=get_user_id(current_user)) new_post.save() return jsonify(new_post) else: return Response("Post with that title already exist!", status=409)
def post(self, user_id): """ Make a new post """ args = postParser.parse_args() content = args['content'] profile = Profile.objects(user=user_id).first() post = Post(user=user_id, user_profile=profile, content=content) post.save() return {'status': 'success'}
def create_post(): if request.method == 'GET': return render_template("new_post.html") elif request.method == 'POST': post_data = request.get_json(force=True, silent=True) if post_data == None: return "Bad request", 400 post = Post(post_data["title"], post_data["content"], post_data["price"], post_data["post_date"], post_data["available"], post_data["buyer"]) post.save() return redirect('/api/posts')
def test_01(self, m="Cassandra should enable finding posts by author"): yanekk_post = Post({"title": "This is the post by Yanekk", "contents":"yanekk yanekk yanekk", "tags":"music,guitar,test", "author":"*****@*****.**"}) yanekk_post.validate() yanekk_post.save() second_yanekk_post = Post({"title": "Another post by Yanekk", "contents":"yanekk yanekk yanekk", "tags":"music,guitar,test", "author":"*****@*****.**"}) second_yanekk_post.validate() second_yanekk_post.save() zbyszek_post = Post({"title": "Post by zbyszek", "contents":"yanekk yanekk yanekk", "tags":"music,guitar,test", "author":"*****@*****.**"}) zbyszek_post.validate() zbyszek_post.save() yanekk_posts = Post.by_index("author", "==", "*****@*****.**") zbyszek_posts = Post.by_index("author", "==", "*****@*****.**") yanekk_posts |should| contain(yanekk_post) yanekk_posts |should| contain(second_yanekk_post) yanekk_posts |should_not| contain(zbyszek_post) zbyszek_posts |should| contain(zbyszek_post) zbyszek_posts |should_not| contain(yanekk_post)
def test_11(self, m="Validated Post should be able to save"): p = Post({"title": "This is the test", "contents":"This is the content", "tags":"music,guitar,test", "author":"*****@*****.**"}) p.validate() p.is_valid() |should| equal_to(True) len(p.errors()) |should| equal_to(0) p.save() |should_not| throw(ModelNotValid)