def edit(postid, ignored=None): post = db.get_post(postid) if request.method == 'GET': if not post: abort(404) if post.deleted: abort(400) if session.get('username') != post.author: abort(401) return render_template('edit.html', post=post, postid=postid) else: form = request.form if 'title' in form and 'content' in form and 'tags' in form and session.get('username') == post.author: if len(form['title']) and len(form['content']): db.edit_post(post, form['title'], form['content'], form['tags']) return redirect(url_for('.show_post', postid=postid)) missing = [] if len(form['title']) == 0: missing.append('title') if len(form['content']) == 0: missing.append('content') flash('Missing ' + ' & '.join(missing) + '!') post.title = form['title'] post.content = form['content'] post.tags = form['tags'].split(' ') return render_template('edit.html', post=post, postid=postid) return redirect(url_for('.show_all'))
def test_star_post_again(self): user_id1 = "user_id1" # Write a post by a user. now = datetime(2013, 9, 26) insert_data = PostInsertData(user_id1, "data1", ["hash_tag"], now) post_id = self._add_post(insert_data) # The user stars the post. now1 = datetime(2012, 8, 25) db.star_post(user_id1, post_id, now1) self._assert_post(insert_data, db.get_post(user_id1, post_id), True, 1) # The user stars the post again. Assert that its num_stars value is unchanged. now2 = datetime(2013, 9, 26) db.star_post(user_id1, post_id, now2) self._assert_post(insert_data, db.get_post(user_id1, post_id), True, 1)
def entry(self, postid,name=None,title=None,body=None,submit=None): if cherrypy.request.method.lower() == "get": post = dereference_author(db.get_post(postid)) comments = db.get_comments(postid) return {"page_title": post["title"], "post": post, "comments":comments} else: db.add_comment(postid,name,title,body) raise cherrypy.HTTPRedirect("")
def view_post(request): if request.method == 'GET': post_id = request.args.get('id') instance = request.args.get('instance') success, post = get_post(session, post_id, instance) if success: return render_template('post.html', post=post) else: return render_template('404.html')
def _assert_stars(self, post_id, expected_stars): """Asserts that the post with the given identifier has the given stars.""" post = db.get_post(self.client_id, post_id) self.assertIsNotNone(post) self.assertEqual(len(expected_stars), post.num_stars) stars = db.get_stars(post_id) self.assertSequenceEqual(expected_stars, stars.items)
def post(post_id): post = db.get_post(flask.g.user_id, post_id) if not post: flask.abort(requests.codes.not_found) renderable_post = _get_renderable_post(post) summary = resource_summary.summary_for_renderable_post(renderable_post) now = datetime.utcnow() return flask.render_template("post.html", post=renderable_post, resource_summary=summary, now_datetime=now)
def GET(self): i = web.input(id=None) if not i.id: return json_response({"error": "id not provided"}) post = db.get_post(i.id) if not post: return json_response({"error": "post not found"}) return json_response(post)
def post(post_id): post = db.get_post(post_id) if request.method == 'GET': return render_template('post.html', post=post) elif request.method == 'PUT': title = request.form['title'] content = request.form['content'] if not (title and content): flash('Title and content are required!') else: db.edit_post(post_id, title, content) return render_template('post.html', post=post) elif request.method == 'DELETE': post = db.get_post(post_id) db.delete_post(post_id) flash(f"Post {post['title']} was successfully deleted") return redirect(url_for('index'))
def view_post(): post_id = request.args.get("id", None) post = None if post_id is not None: if post_id.isdigit(): post_id = int(post_id) ok, post = get_post(post_id) return render_template("view.html", post=post)
def show_post(postid, ignored=None): post = db.get_post(postid) if not post: abort(404) url = url_for('.show_post', postid=postid) if request.path != url + post.url: return redirect(url + post.url) if post.deleted: g.postauthor = post.author abort(410) return render_template('show_post.html', post=post, postid=postid)
def GET(self): i = web.input(id=None) if not i.id: return json_response({"error": "id not provided"}) post = db.get_post(i.id) if not post: return json_response({"error": "post not found"}) db.delete_post(i.id) response = {"status": "created"} return json_response(response)
def get_post(postID): """get a post by ID :param postID: ID of post :type postID: str :rtype: Post """ post = db.get_post(postID) if post: return post else: return get_error("the post was not found"), 404
def test_unstar_post_again(self): user_id1 = "user_id1" # Write a post by a user. now = datetime(2013, 9, 26) insert_data = PostInsertData(user_id1, "data1", ["hash_tag"], now) post_id = self._add_post(insert_data) # The user unstars a post that is not starred. Assert that its num_stars value is unchanged. unstar_now = datetime(2012, 8, 25) db.unstar_post(user_id1, post_id, unstar_now) self._assert_post(insert_data, db.get_post(user_id1, post_id), False, 0)
def view_post(): if "uuid" not in session: abort(404) post_id = request.args.get("id", None) if not post_id or not post_id.isdigit(): abort(404) post_id = int(post_id) ok, contents = get_post(session["uuid"], post_id) if not ok: abort(404) return render_template("post.html", contents=contents, post_id=post_id)
def delete_comment(self, comment_id=None, submit=None): if cherrypy.request.method.lower() != "post": error("Cannot service non-post requests.") author = get_author() if author is None: error("You need to be logged in to delete comments.") comment = db.get_comment(comment_id) if comment is None: error("Invalid comment id.") post = db.get_post(comment["post_id"]) if post["author_id"] == author["id"]: if not db.del_comment(comment_id): error("Failed to delete comment.") raise cherrypy.HTTPRedirect("/entry/%s" % post["id"])
def POST(self): i = web.input(id=None) if not i.id: return json_response({"error": "id not provided"}) post = db.get_post(i.id) if not post: return json_response({"error": "post not found"}) payload = jsondata() data = payload["post"] # TODO: validate data db.update_post(i.id, data['title'], data['body']) response = {"status": "updated"} return json_response(response)
def test_get_posts(self): # Write the first post by one user. hash_tags1 = ["hash_tag1", "hash_tag2"] now1 = datetime(2013, 9, 26) insert_data1 = PostInsertData("user_id1", "data1", hash_tags1, now1) post_id1 = self._add_post(insert_data1) # Write the second post by another user. hash_tags2 = ["hash_tag3"] now2 = datetime(2014, 10, 27) insert_data2 = PostInsertData("user_id2", "data2", hash_tags2, now2) post_id2 = self._add_post(insert_data2) # Assert that the first post is read correctly. self._assert_post(insert_data1, db.get_post(self.client_id, post_id1)) # Assert that the second post is read correctly. self._assert_post(insert_data2, db.get_post(self.client_id, post_id2)) # Get both posts. all_posts = db.get_posts(self.client_id) self.assertEqual(2, len(all_posts)) # Assert that the second post is returned first because it is more recent. self._assert_post(insert_data2, all_posts[0], 0) # Assert that the first post is returned last because it is less recent. self._assert_post(insert_data1, all_posts[1], 0)
def comment(): if not app.config.get('allow_comments', True): abort(403) form = request.form if form.get('postid') and form.get('author') and form.get('content'): postid = form.get('postid') author = form.get('author') content = form.get('content') post = db.get_post(postid) if post: reply_to = parseInt(form.get('reply_to', 0), 0) db.new_comment(postid, author, content, date.today(), reply_to) return redirect(url_for('.show_post', postid=postid)) abort(404) abort(400)
def test_get_stars(self): user_id1 = "user_id1" user_id2 = "user_id2" hash_tag = "hash_tag" creator_id = "creator_id" # Write the first post by a user. insert_now1 = datetime(2010, 6, 23) insert_data1 = PostInsertData(creator_id, "data1", [hash_tag], insert_now1) post_id1 = self._add_post(insert_data1) # Write the second post by a user. insert_now2 = datetime(2011, 7, 24) insert_data2 = PostInsertData(creator_id, "data2", [hash_tag], insert_now2) post_id2 = self._add_post(insert_data2) # The first user stars the first post. starred_now1 = datetime(2012, 8, 25) db.star_post(user_id1, post_id1, starred_now1) self._assert_post(insert_data1, db.get_post(user_id1, post_id1), True, 1) self._assert_post(insert_data1, db.get_post(user_id2, post_id1), False, 1) # The second user stars the first post. starred_now2 = datetime(2013, 9, 26) db.star_post(user_id2, post_id1, starred_now2) self._assert_post(insert_data1, db.get_post(user_id1, post_id1), True, 2) self._assert_post(insert_data1, db.get_post(user_id2, post_id1), True, 2) # The first user stars the second post. starred_now3 = datetime(2014, 10, 27) db.star_post(user_id1, post_id2, starred_now3) self._assert_post(insert_data2, db.get_post(user_id1, post_id2), True, 1) self._assert_post(insert_data2, db.get_post(user_id2, post_id2), False, 1) # Get both stars for the first post. # Assert that the second user is returned first because its star is more recent. self._assert_stars(post_id1, [user_id2, user_id1]) # Assert that the second post is starred only by the first user. self._assert_stars(post_id2, [user_id1]) def _assert_user_id1_posts(user_id1_posts): self.assertEqual(2, len(user_id1_posts)) self._assert_post(insert_data2, user_id1_posts[0], True, 1) self._assert_post(insert_data1, user_id1_posts[1], True, 2) def _assert_user_id2_posts(user_id2_posts): self.assertEqual(2, len(user_id2_posts)) self._assert_post(insert_data2, user_id2_posts[0], False, 1) self._assert_post(insert_data1, user_id2_posts[1], True, 2) # Assert that the stars are seen by each user for all posts. _assert_user_id1_posts(db.get_posts(user_id1)) _assert_user_id2_posts(db.get_posts(user_id2)) # Assert that the stars are seen by each user for all posts with a hash tag. _assert_user_id1_posts(db.get_posts_with_hashtag(user_id1, hash_tag)) _assert_user_id2_posts(db.get_posts_with_hashtag(user_id2, hash_tag)) # Assert that the stars are seen by each user for all posts by a given user. _assert_user_id1_posts(db.get_posts_by_user(user_id1, creator_id)) _assert_user_id2_posts(db.get_posts_by_user(user_id2, creator_id))
def delete(postid, ignored=None): post = db.get_post(postid) if request.method == 'GET': if not post: abort(404) if post.deleted: abort(400) if session.get('username') == post.author: return render_template('delete.html', post=post, postid=postid) abort(401) form = request.form if post and not post.deleted and session.get('username') == post.author: db.delete_post(post) flash('Deleted post') return redirect(url_for('.show_all')) abort(400)
def restore(postid, ignored=None): post = db.get_post(postid) if not post.deleted: return redirect(url_for('.show_post', postid=postid)) if request.method == 'GET': if not post: abort(404) if not post.deleted: abort(400) if session.get('username') != post.author: abort(401) return render_template('restore.html', post=post, postid=postid) form = request.form if post and session.get('username') == post.author: db.restore_post(post) flash('Restored post ' + post.title) return redirect(url_for('.show_post', postid=postid)) abort(403)
def GET(self, slug): p = db.get_post(slug) if not p: raise web.notfound() return render.site(render.post(p))
def fetch_new_posts(): print("Fetching new posts.", time.time()) for post in r_all.new(): if not db.get_post(post.id): db.insert_post(post)
def test_get_missing_post(self): missing_post_id = "missing_post_id" post = db.get_post(self.client_id, missing_post_id) self.assertIsNone(post)