Guest User

Untitled

a guest
Aug 17th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. def add_comment(request, pk):
  2. """Add a new comment."""
  3. p = request.POST
  4.  
  5. if p.has_key("body") and p["body"]:
  6. author = "Anonymous"
  7. if p["author"]: author = p["author"]
  8. if p["email"]: email = p["email"]
  9. comment = Comment(post=Post.objects.get(pk=pk))
  10.  
  11. """Save Comment form"""
  12. if request.user.is_authenticated():
  13. cf = CommentForm(initial={'author': "foo", 'email': "foobar@bar.baz"}, instance=comment)
  14. else:
  15. cf = CommentForm(p, instance=comment)
  16. cf.fields["author"].required = False
  17. comment = cf.save(commit=False)
  18.  
  19. """save comment instance"""
  20. if request.user.is_authenticated():
  21. comment.author = "foo"
  22. comment.email = "foobar@bar.baz"
  23. else:
  24. comment.author = author
  25. comment.email = email
  26. notify = True
  27. if request.user.username == "ak": notify = False
  28.  
  29. comment.save(notify=notify)
  30. return HttpResponseRedirect(reverse("blog.views.post", args=[pk]))
Add Comment
Please, Sign In to add comment