def get(self, **kwargs): post_id = kwargs['post_id'] Post.get_by_id(int(post_id)).key.delete() # Cascade the delete to all comments associated with the post comments = Comment.query(Comment.post_id == int(post_id)) for comment in comments: comment.key.delete() self.redirect('/')
def get(self, id): if id == "newpost": self.render_form() elif str(id).isdigit(): p = Post.get_by_id([int(id)])[0] self.render("blog/post.html", post=p) elif re.match(r"\d+\.json", id): p = Post.get_by_id([int(str(id).split(".")[0])])[0] self.render_json(p.to_json()) elif str(id) == ".json": self.render_json(json.dumps(list(Post.select_all()), cls=ModelEncoder)) else: self.error(404)
def get(self, id): if id == 'newpost': self.render_form() elif str(id).isdigit(): p = Post.get_by_id([int(id)])[0] self.render('blog/post.html', post=p) elif re.match(r'\d+\.json', id): p = Post.get_by_id([int(str(id).split('.')[0])])[0] self.render_json(p.to_json()) elif str(id) == '.json': self.render_json(json.dumps(list(Post.select_all()), cls=ModelEncoder)) else: self.error(404)
def get(self, **kwargs): template = jinja_env.get_template('post.html') post_id = kwargs['post_id'] post = Post.get_by_id(int(post_id)) # Retrieve all comments comments_query = Comment.query(Comment.post_id == int(post_id)).order( Comment.submitted) comments = [comment for comment in comments_query] # Discern anonymous browsers from users cookie = self.request.cookies.get('user') user = None has_liked = None if validate_user_cookie(cookie): user = cookie.split("|")[0] has_liked = user_has_liked_post(user, post_id) # If this post exists, render it (otherwise, 404) self.write( template, { 'post': post, 'comments': comments, 'current_user': user, 'has_liked': has_liked })
def testUnlikedPostCanBeLiked(self): # Ensure that post has been liked post = Post.get_by_id(self.initial_post_to_like_key.integer_id()) post.liked_by.append("Valid Liker") post.put() # Ensure that the Unlike button appears self.testapp.set_cookie('user', create_user_cookie('Valid Liker')) response = self.testapp.request( '/posts/%d' % self.initial_post_to_like_key.integer_id()) like_button = response.html.select('.post-like')[0] self.assertEqual(like_button.get_text(), 'Unlike') # Click the Unlike button an ensure the like counter decrements unlike_response = response.click(description="Unlike").follow() like_counter = unlike_response.html.select('.post-likes')[0].get_text() self.assertEqual(int(like_counter), 1) # Now ensure that there is a Like button and that re-liking # increments the counter re_like_response = unlike_response.click(description="Like").follow() re_like_counter = re_like_response.html.select( '.post-likes')[0].get_text() self.assertTrue(int(re_like_counter) == int(like_counter) + 1)
def error_if_post_does_not_exist(*args, **kwargs): self = args[0] post_id = kwargs['post_id'] post = Post.get_by_id(int(post_id)) if post: fn(self, **kwargs) else: self.abort(404)
def get(self, **kwargs): post = Post.get_by_id(int(kwargs['post_id'])) if not user_has_liked_post(kwargs['user_id'], kwargs['post_id']): post.liked_by.append(kwargs['user_id']) post.put() else: post.liked_by.remove(kwargs['user_id']) post.put() self.redirect('/posts/%s' % kwargs['post_id'])
def get(self, **kwargs): template = jinja_env.get_template('edit-form.html') post_id = kwargs['post_id'] comment_id = kwargs['comment_id'] # Retrieve Post and its Comment(s) from the datastore post = Post.get_by_id(int(post_id)) comments_query = Comment.query(Comment.post_id == int(post_id)).order( Comment.submitted) comments = [comment for comment in comments_query] self.write(template, { 'post': post, 'edit_comment_id': int(comment_id), 'comments': comments })
def user_has_liked_post(user_id, post_id): """ Indicates whether a user has already liked a given post Args: user_id: The user id of the user to test against this post, as an int or str post_id: The post id for the post to test this user against, as an int or str Returns: True if the user with id user_id has 'liked' the post with id post_id False otherwise """ post = Post.get_by_id(int(post_id)) if post: return user_id in post.liked_by
def post(self, **kwargs): post_id = kwargs['post_id'] title = self.request.POST['title'] content = self.request.POST['content'] # Check that title and content are non empty and add the post to the # datastore; otherwise, rerender the form with validation errors if title != '' and content != '': post = Post.get_by_id(int(post_id)) post.title = title post.content = content post.put() self.redirect('/posts/' + post_id) else: template = jinja_env.get_template('new-post.html') form_data = {'title': title, 'content': content, 'error': True} self.write(template, {'form': form_data, 'new': True})
def get(self, id=None): if id is not None: # /post/{id}: Read data for specific post post = Post.get_by_id(int(id)) if post is not None: response = { 'id': post.key.id(), 'title': post.title, 'content': post.content, 'author': post.author, 'board': post.board, 'created': str(post.created), 'modified': str(post.modified) } self.response.write(json.dumps(response)) else: self.error(404) else: # /post: Read list of latest posts postsQuery = Post.query().fetch(25) posts = [] for post in postsQuery: posts.append({ 'id': post.key.id(), 'title': post.title, 'content': post.content, 'author': post.author, 'board': post.board, 'created': str(post.created), 'modified': str(post.modified) }) response = { 'posts': posts } self.response.write(json.dumps(response))
def get(self, id=None): if id is not None: # /post/{id}: Read data for specific post post = Post.get_by_id(int(id)) if post is not None: response = { 'id': post.key.id(), 'title': post.title, 'content': post.content, 'author': post.author, 'board': post.board, 'created': str(post.created), 'modified': str(post.modified) } self.response.write(json.dumps(response)) else: self.error(404) else: # /post: Read list of latest posts postsQuery = Post.query().fetch(25) posts = [] for post in postsQuery: posts.append({ 'id': post.key.id(), 'title': post.title, 'content': post.content, 'author': post.author, 'board': post.board, 'created': str(post.created), 'modified': str(post.modified) }) response = {'posts': posts} self.response.write(json.dumps(response))