def blog_comment_or_vote(self, redirect): """Comment or vote on a blog_post.""" voteType = self.request.get("voteDirection") comment_id = self.request.get("comment_id") comment = self.request.get("comment").strip() delete_comment = self.request.get("delete_comment") blog_id = self.request.get("blog_id") blog_entry = BlogEntity.get_by_id_str(blog_id) if voteType and blog_entry: try: blog_entry = BlogEntity.get_by_id_str(blog_id) blog_entry.vote(voteBy=self.user, voteType=voteType) self.render("/thanks.html", redirect=redirect) except myExceptions.VoteOnOwnPostNotAllowed: self.redirect("/error?errorType=VoteOnOwnPostNotAllowed") except BadValueError: self.redirect("/error?errorType=BadValueError") elif comment and blog_entry: self.blog_comment(comment, blog_entry, comment_id, delete_comment, redirect) elif blog_entry: self.redirect("/error?errorType=TooShort") else: self.redirect("/error?errorType=unknoswn")
def test_delete_post(self): """delete_post should delete the post and its comments and votes.""" james = UserEntity.register(username='******', password='******') kimmy = UserEntity.register(username='******', password='******') dane = UserEntity.register(username='******', password='******') joe = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) b = BlogEntity.create_blog_entry(parent=blog_key(), title='b', article='content', created_by=james) a.vote(voteBy=kimmy, voteType='up') a.vote(voteBy=dane, voteType='up') a.vote(voteBy=joe, voteType='down') b.vote(voteBy=kimmy, voteType='up') b.vote(voteBy=dane, voteType='up') b.vote(voteBy=joe, voteType='down') a.add_comment(commentBy=kimmy, comment='abc') a.add_comment(commentBy=dane, comment='abc') a.add_comment(commentBy=joe, comment='123') b.add_comment(commentBy=kimmy, comment='abc') b.add_comment(commentBy=dane, comment='abc') b.add_comment(commentBy=joe, comment='123') a.delete_post(james) votes = VotesEntity.all() for vote in votes: self.assertNotEqual(vote.voteOn.title, 'a', msg="Votes of post 'a' should be deleted") comments = CommentsEntity.all() for comment in comments: self.assertNotEqual(comment.commentOn.title, 'a', msg="Comments of post 'a' should be deleted") bloe_entries = BlogEntity.all() for blog_entity in bloe_entries: self.assertNotEqual(blog_entity.title, 'a', msg="Post 'a' should be deleted")
def post(self): """Create/edit a blog-entry if logged in and form filled correcly.""" title = self.request.get("title").strip() article = self.request.get("article").strip() # If user is editing a post, we should get a blog_id blog_id = self.request.get("blog_id") blog_entry = BlogEntity.get_by_id_str(blog_id) deletePost = bool(self.request.get("delete")) deletion_verified = bool(self.request.get("verify_deletion_checkbox")) cancel = bool(self.request.get("cancel")) if cancel: self.redirect('/blogs/%s' % blog_id) else: if not self.user: self.redirect('/login') elif deletePost: if deletion_verified: self.delete_post(blog_entry) else: self.render_this(blog_entry=blog_entry, title=title, article=article, verify_deletion=True) else: self.edit_post(blog_entry, title, article)
def test_get_comments_on_post(self): """get_comments_on_post should return all comments on post.""" james = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) c1 = CommentsEntity.comment_on_post(commentBy=james, commentOn=a, comment='abc') c2 = CommentsEntity.comment_on_post(commentBy=james, commentOn=a, comment='123') comments = CommentsEntity.get_comments_on_post(a) self.assertEqual(comments.count(), 2, msg='should return 2 comments') self.assertEqual(comments[0].comment, c1.comment, msg='should match first comment') self.assertEqual(comments[1].comment, c2.comment, msg='should match first comment')
def test_create_blog_entry_with_existing_title(self): """BlogEntity.register should fail with a non-unique title.""" james = UserEntity.register(username='******', password='******') BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) self.assertRaises( myExceptions.NotUnique, BlogEntity.create_blog_entry, parent=blog_key(), title='a', article='content', created_by=james, )
def dev_create_some_blog_posts(users): """Generator for random blog posts, for testing in development.""" for i in range(1, 46): article = "" article += "<h3>{}</h3>".format(pick_random_from_list(title_list)) for j in range(0, randint(4, 8)): if j > 0: if randint(0, 5) == 1: article += "<h4>{}</h4>".format( pick_random_from_list(title_list)) article += "<p>{}</p>".format( pick_random_from_list(article_paragraphgs)) a = BlogEntity( parent=blog_key(), created_by=users[randint(0, users.count() - 1)], title='{} {}'.format(i, pick_random_from_list(title_list)), article=article) a.put()
def get(self, blog_id): # noqa """Retrieve the blog-id from the url and show it.""" blog_entry = BlogEntity.get_by_id_str(blog_id) if not blog_entry: self.error(404) return self.render("blog_permalink.html", article=blog_entry, parser=self.render_blog_article)
def get(self): """If blog_id is in url, lookup BlogEntity and pass along""" blog_id = self.request.get("blog_id") blog_entry = BlogEntity.get_by_id_str(blog_id) if blog_entry: self.render_this(blog_entry=blog_entry, title=blog_entry.title, article=blog_entry.article) else: self.render('/error?error=NotValidBlogUser')
def test_blog_get_by(self): """get_by_id_str and by_title should return the correct BlogEntity""" james = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a title', article='a content', created_by=james) a_id = str(a.key().id()) self.assertEqual(BlogEntity.get_by_id_str(a_id).key().id(), a.key().id(), msg="'by_title' should return the 'a'-blog_entry") self.assertEqual( BlogEntity.by_title('a title').key().id(), a.key().id(), msg="'get_by_id_str' should return the 'a'-blog_entry")
def test_create_blog_entry(self): """.""" james = UserEntity.register(username='******', password='******') self.assertEqual(type( BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james)), BlogEntity, msg="'create_blog_entry' should return a BlogEntity")
def get(self): """Retrieve all the latest blog-entries and render them to user.""" page_to_show = self.request.get("page") if page_to_show.isdigit() and int(page_to_show) > 1: page_to_show = int(page_to_show) else: page_to_show = 1 limit = 5 offset = (page_to_show - 1) * limit totalArticles = BlogEntity.all().count(1000) totalPages = int(ceil(float(totalArticles) / limit)) articles = BlogEntity.all().order('-created').fetch(limit=limit, offset=offset) self.render("blogs.html", articles=articles, parser=self.render_blog_article, pages=totalPages, currentPage=page_to_show)
def test_delete_post_wrong_user(self): """delete_post should raise error if wrong user attempts to del it.""" james = UserEntity.register(username='******', password='******') kimmy = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) self.assertRaises(myExceptions.EditOthersPosts, a.delete_post, kimmy)
def test_edit_blog_entry_check_data(self): """edit_blog_post should fail if using a title from different post.""" james = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) a.edit_blog_entry(title='abc', article='123', created_by=james) self.assertEqual(a.title, 'abc', msg=None) self.assertEqual(a.article, '123', msg=None)
def test_edit_blog_entry_with_existing_title(self): """edit_blog_post should fail if using a title from different post.""" james = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) BlogEntity.create_blog_entry(parent=blog_key(), title='b', article='content', created_by=james) self.assertRaises( myExceptions.NotUnique, a.edit_blog_entry, title='b', article='contents', created_by=james, )
def test_blog_add_comment(self): """.add_comment should return a CommentEnity if valid""" james = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a title', article='a content', created_by=james) self.assertEqual( type(a.add_comment(commentBy=james, comment='Awesome')), CommentsEntity)
def test_blog_vote_on_own_post_fail(self): """user cannot vote on own post""" james = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a title', article='a content', created_by=james) self.assertRaises(myExceptions.VoteOnOwnPostNotAllowed, a.vote, voteBy=james, voteType='up')
def test_comment_on_post(self): """comment_on_post should return a CommentEntity.""" james = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) self.assertEqual( type( CommentsEntity.comment_on_post(commentBy=james, commentOn=a, comment='abc')), CommentsEntity)
def test_edit_blog_entry_wrong_user(self): """edit_blog_post should fail if using a title from different post.""" james = UserEntity.register(username='******', password='******') jimmy = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) self.assertRaises( myExceptions.EditOthersPosts, a.edit_blog_entry, title='a', article='contents', created_by=jimmy, )
def test_get_by_id_str(self): """get_by_id_str should return the correct comment.""" james = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) c = CommentsEntity.comment_on_post(commentBy=james, commentOn=a, comment='abc') comment_id = str(c.key().id()) self.assertEqual(type(CommentsEntity.get_by_id_str(comment_id)), CommentsEntity, msg=None)
def test_blog_vote_on_others_post(self): """user should be able to vote on other posts""" james = UserEntity.register(username='******', password='******') john = UserEntity.register(username='******', password='******') jimbo = UserEntity.register(username='******', password='******') jake = UserEntity.register(username='******', password='******') jonas = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a title', article='a content', created_by=james) a.vote(voteBy=john, voteType='up') a.vote(voteBy=jimbo, voteType='down') a.vote(voteBy=jake, voteType='up') a.vote(voteBy=jonas, voteType='up') self.assertEqual( a.getVotes(), { 'up': 3, 'down': 1 }, msg= "'getvotes' should return a dict with keyword 'up' and 'down, here with values 3 and 1'" ) # noqa self.assertEqual(a.getVotesFromUser(john), 'up', msg="'voting 'up' should set the vote to 'up'") a.vote(voteBy=john, voteType='up') self.assertEqual(a.getVotesFromUser(john), None, msg="'Voting 'up' twice should remove the vote") self.assertEqual(a.getVotesFromUser(jimbo), 'down', msg="'voting 'down' should set the vote to 'down'") a.vote(voteBy=jimbo, voteType='down') self.assertEqual(a.getVotesFromUser(jimbo), None, msg="'Voting 'down' twice should remove the vote")
def test_blog_get_comments(self): """.get_comment should return a list of all CommentEnities on a post""" james = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a title', article='a content', created_by=james) a.add_comment(commentBy=james, comment='test') self.assertEqual( type(a.get_comments()[0]), CommentsEntity, msg="'get_comments should return a query of CommentsEntity'") self.assertEqual(a.get_comments().count(), 1) self.assertEqual(a.get_comments()[0].comment, 'test')
def post(self): """Create a blog-entry if logged in and form filled out correcly.""" if not self.user: self.redirect('/login') else: title = self.request.get("title").strip() article = self.request.get("article") if title and article and self.user: try: a = BlogEntity.create_blog_entry(parent=blog_key(), created_by=self.user, title=title, article=article) self.redirect('/blogs/%s' % str(a.key().id())) except myExceptions.NotUnique: self.render_this(title=title, article=article, error_notUnique=True) else: self.render_this(title=title, article=article, error_missing_fields=True)
def test_edit_comment(self): """edit_comment should return a comment with corrected text.""" james = UserEntity.register(username='******', password='******') jane = UserEntity.register(username='******', password='******') a = BlogEntity.create_blog_entry(parent=blog_key(), title='a', article='content', created_by=james) c = CommentsEntity.comment_on_post(commentBy=james, commentOn=a, comment='abc') new_comment = '123' self.assertRaises(myExceptions.EditOthersComments, c.edit_comment, comment=new_comment, commentBy=jane) self.assertEquals( c.edit_comment(comment=new_comment, commentBy=james).comment, new_comment)
if j > 0: if randint(0, 5) == 1: article += "<h4>{}</h4>".format( pick_random_from_list(title_list)) article += "<p>{}</p>".format( pick_random_from_list(article_paragraphgs)) a = BlogEntity( parent=blog_key(), created_by=users[randint(0, users.count() - 1)], title='{} {}'.format(i, pick_random_from_list(title_list)), article=article) a.put() def assign_random_votes(blog_entries, users): """Assign random votes to articles.""" for blog_entry in blog_entries: for user in users: try: blog_entry.vote(voteBy=user, voteType=pick_random_from_list(['up', 'down'])) except myExceptions.VoteOnOwnPostNotAllowed: pass user_list = UserEntity.all() blog_entry_list = BlogEntity.all() dev_create_some_blog_posts(user_list) assign_random_votes(blog_entry_list, user_list)