def create_comment_view(request, post_id): if request.method == 'POST': post = Post.objects.get(pk=post_id) if request.user.is_authenticated(): if post: comment = Comment() comment.body = request.POST['body'] comment.pub_date = timezone.datetime.now() comment.post = post comment.author = request.user comment.save() return JsonResponse( { "status": "success", "message": "answer created", "comment_id": comment.pk }, status=201) else: return JsonResponse( { "status": "failure", "message": "post not exist" }, status=404) else: return JsonResponse( { "status": "failure", "message": "user need to login" }, status=403) else: return render(request, 'posts/create-commentd.html', {'post_id': post_id})
def import_data(): # Initial Imports from django.contrib.auth.models import User # Processing model: posts.models.Post from posts.models import Post posts_post_1 = Post() posts_post_1.author = importer.locate_object(User, "id", User, "id", 1, {'id': 1, 'password': '******', 'last_login': datetime.datetime(2018, 11, 27, 6, 31, 56, 449061, tzinfo=<UTC>), 'is_superuser': True, 'username': '******', 'first_name': '', 'last_name': '', 'email': '*****@*****.**', 'is_staff': True, 'is_active': True, 'date_joined': datetime.datetime(2018, 11, 27, 6, 31, 35, 527568, tzinfo=<UTC>)} ) posts_post_1.link = 'https://stackoverflow.com/questions/3330435/is-there-an-sqlite-equivalent-to-mysqls-describe-table' posts_post_1.title = 'Initial Data' posts_post_1.description = 'Cool Description' posts_post_1 = importer.save_or_locate(posts_post_1) posts_post_2 = Post() posts_post_2.author = importer.locate_object(User, "id", User, "id", 1, {'id': 1, 'password': '******', 'last_login': datetime.datetime(2018, 11, 27, 6, 31, 56, 449061, tzinfo=<UTC>), 'is_superuser': True, 'username': '******', 'first_name': '', 'last_name': '', 'email': '*****@*****.**', 'is_staff': True, 'is_active': True, 'date_joined': datetime.datetime(2018, 11, 27, 6, 31, 35, 527568, tzinfo=<UTC>)} ) posts_post_2.link = 'https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html' posts_post_2.title = 'Reset Generator' posts_post_2.description = 'https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html' posts_post_2 = importer.save_or_locate(posts_post_2) # Processing model: posts.models.Vote from posts.models import Vote posts_vote_1 = Vote() posts_vote_1.voter = importer.locate_object(User, "id", User, "id", 1, {'id': 1, 'password': '******', 'last_login': datetime.datetime(2018, 11, 27, 6, 31, 56, 449061, tzinfo=<UTC>), 'is_superuser': True, 'username': '******', 'first_name': '', 'last_name': '', 'email': '*****@*****.**', 'is_staff': True, 'is_active': True, 'date_joined': datetime.datetime(2018, 11, 27, 6, 31, 35, 527568, tzinfo=<UTC>)} ) posts_vote_1.post = posts_post_1 posts_vote_1.liked = True posts_vote_1 = importer.save_or_locate(posts_vote_1) posts_vote_2 = Vote() posts_vote_2.voter = importer.locate_object(User, "id", User, "id", 2, {'id': 2, 'password': '******', 'last_login': None, 'is_superuser': False, 'username': '******', 'first_name': '', 'last_name': '', 'email': '', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2018, 11, 27, 6, 35, 12, 746625, tzinfo=<UTC>)} ) posts_vote_2.post = posts_post_1 posts_vote_2.liked = True posts_vote_2 = importer.save_or_locate(posts_vote_2) # Processing model: posts.models.Comment from posts.models import Comment posts_comment_1 = Comment() posts_comment_1.description = 'My Cool Comment on Post 1 yeh.' posts_comment_1.author = importer.locate_object(User, "id", User, "id", 1, {'id': 1, 'password': '******', 'last_login': datetime.datetime(2018, 11, 27, 6, 31, 56, 449061, tzinfo=<UTC>), 'is_superuser': True, 'username': '******', 'first_name': '', 'last_name': '', 'email': '*****@*****.**', 'is_staff': True, 'is_active': True, 'date_joined': datetime.datetime(2018, 11, 27, 6, 31, 35, 527568, tzinfo=<UTC>)} ) posts_comment_1.post = posts_post_1 posts_comment_1 = importer.save_or_locate(posts_comment_1)
def test_comment(self): comment = Comment() post = Post() user = User.objects.create_user('Testy McTest', '*****@*****.**', 'testpassword') user.save() group = Group() group.name = "Test Group" group.save() membership = Membership() membership.user = User.objects.get(id = user.id) membership.group = Group.objects.get(id = group.id) membership.save() post.author = Membership.objects.get(id = membership.id) post.message = "Testing321" post.save() comment.author = Membership.objects.get(id = membership.id) comment.message = "123Testing" comment.post = Post.objects.get(id = post.id) comment.save() test_comment = Comment.objects.get(id = post.id) self.assertEquals(test_comment, comment) self.assertEquals(test_comment.author, Membership.objects.get(id = membership.id)) self.assertEquals(test_comment.message, "123Testing") self.assertEquals(test_comment.post, Post.objects.get(id = post.id)) comment.delete() post.delete() membership.delete() group.delete() user.delete()