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 comment(post_id): post = Post.query.get_or_404(post_id) form = CommentForm() if form.validate_on_submit(): comment = Comment(content=form.content.data, date_posted=datetime.now(timezone('Asia/Kolkata')), user_id=current_user.id, post_id=post_id) db.session.add(comment) db.session.commit() flash('You have added a comment!', 'success') return redirect(url_for('post', post_id=post.id)) return render_template('comment.html', title=post.title, post=post, form=form)
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 view_task(task_id): user = User.get_by_username(session["user_name"]) task = Task.get_task_by_id(task_id, session["user_name"]) comments = Comment.query.filter_by(task_id=task_id).all() form = CommentForm() if form.validate_on_submit(): if user.blocked: form.comment.errors.append( "Вы заблокированы и не можете оставлять комментарии") return render_template("task_view.html", form=form, task=task, comments=comments, user=user) db.session.add( Comment(task_id=task_id, username=session["user_name"], comment=form.comment.data)) db.session.commit() return render_template("task_view.html", form=form, task=task, comments=comments, user=user)
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, }, )