def view_post(request, slug): post = get_object_or_404(Post, slug=slug) post_tags = post.tags.all() tags = PracticeArea.objects.all() authors = Attorney.objects.all() if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): first = form.cleaned_data['first_name'] last = form.cleaned_data['last_name'] body = form.cleaned_data['body'] Comment.objects.create(first_name=first, last_name=last, body=body, post=post) return redirect('/blog') else: form = CommentForm() comments = Comment.objects.all() data = { "post": post, "comment_form": form, "comments": comments, "tags": tags, "post_tags": post_tags, "authors": authors } return render(request, 'view_post.html', data)
def create_comment(request, post_slug): try: post = Post.objects.get(slug=post_slug) except Post.DoesNotExist: post = None if post is None: return redirect("/") user = User.objects.get(id=request.user.id) user_profile = UserProfile.objects.get(user=user) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): if post: comment = form.save(commit=False) comment.post = post comment.user = user_profile comment.save() return redirect( reverse( "website:view_post", kwargs={"post_slug": post_slug}, )) form = CommentForm() context_dict = {"form": form, "post": post} return render(request, "SkyView/createComment.html", context=context_dict)
def summary(request, subject, category, subcategory, summary_id): try: summary = Summary.objects.get(pk=summary_id) except Summary.DoesNotExist: summary = "DOES NOT EXIST!" subject = Subject.objects.get(name=subject) request.session.save() if not View.objects.filter(summary=summary, session=request.session.session_key): view = View(summary=summary, ip=request.META['REMOTE_ADDR'], date_created=datetime.datetime.now(), session=request.session.session_key) view.save() context_dict = { 'subject': subject, 'summary': summary, } if request.method == 'POST': comment_form = CommentForm(data=request.POST) context_dict["comment_form"] = comment_form if comment_form.is_valid(): if throttle(request): return render(request, "summary.html", context_dict) comment = comment_form.save(commit=False) comment.user = User.objects.get(id=request.user.id) comment.summary = summary comment.save() messages.add_message(request, messages.SUCCESS, 'התגובה שלך נוספה בצלחה') return redirect(comment) else: print(comment_form.errors) else: comment_form = CommentForm() context_dict["comment_form"] = comment_form return render(request, 'summary.html', context_dict)
def view_question(request, slug): question = Question.objects.get(id=slug) comments = Comment.objects.filter(parent__pk=slug) current_user = User.objects.get(id=request.user.id) comment_form = CommentForm() reply_forms = {} for comment in comments: reply_forms[comment] = ReplyForm(prefix=comment.pk) if request.method == 'POST': # parse the comment form (Good Rating) if "comment_sub_a" in request.POST: comment_form = CommentForm(request.POST) if comment_form.is_valid(): question.rating = question.rating + 1 question.save() comment = comment_form.save(commit=False) comment.user = current_user comment.parent = question comment.save() return redirect('view_question', slug) # parse the comment form (Bad Rating) elif "comment_sub_b" in request.POST: comment_form = CommentForm(request.POST) if comment_form.is_valid(): question.rating = question.rating - 1 question.save() comment = comment_form.save(commit=False) comment.user = current_user comment.parent = question comment.save() return redirect('view_question', slug) # parse the reply forms elif "reply_sub" in request.POST: reply_forms = {} for comment in comments: reply_forms[comment] = ReplyForm(request.POST,prefix=comment.pk) for reply_form_index in reply_forms: reply_form = reply_forms.get( reply_form_index ) if reply_form.is_valid(): reply = reply_form.save(commit=False) reply.user = current_user reply.parent = Comment.objects.get(id=reply_form.prefix) reply.save() return redirect('view_question', slug) return render(request, 'question.html', { 'question' : get_object_or_404(Question, pk=slug), 'comment_form' : comment_form, 'reply_forms' : reply_forms, }, )