def leave_comment(param): post = Post.query.filter_by(title=param).first_or_404() email = request.form.get('email', None) user = User.query.filter_by(email=email).first() if user and not user.confirmed: login_user(user) session.pop('browser_email',None) return redirect(url_for("auth.unconfirmed")) if not user: author = request.form.get('name', None) user = User(username=author, email=email) else: author = user.username body = request.form.get('body', None) comment = Comment(body=body, post=post, author_id=user.id) db.session.add(comment) replied_id = request.form.get('be_reply', None) if replied_id: replied_comment = Comment.query.get_or_404(replied_id) comment.replied = replied_comment if request.form.get('remember', None): session['browser_email'] = email session['browser_user'] = author db.session.commit() return redirect(url_for('blog.show_post', param=post.title) + '#comments')
def reply(request): if request.user.is_authenticated: if request.method == 'POST': comment_text = request.POST['comment'] parent_id = request.POST['parent'] comment = Comment.objects.get(pk = parent_id) blog = comment.post blog_id = blog.pk new_rep = Comment(post = blog,user = request.user,parent = comment,body = comment_text) new_rep.save() return redirect(reverse('Blog:post',args = [blog_id])) else: return redirect('/') else: return redirect('/')
def blog_detail(request, pk): post = Post.objects.get(pk=pk) form = CommentForm() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = Comment(author=form.cleaned_data["author"], detail=form.cleaned_data['detail'], post=post) comment.save() comments = Comment.objects.filter(post=post) context = { 'post': post, 'comments': comments, 'form': form, } return render(request, 'blog_detail.html', context)
def createComment(): comment = request.form["comment"] postId = request.form["postId"] newComment = Comment(body = comment ,userid = current_user.id ,postid = int(postId)) db.session.add(newComment) db.session.commit() return redirect(url_for('showPost', year=datetime.now().year, postId = postId))
def fake_comments(count=500): for i in range(count): comment = Comment( author=User.query.get(random.randint(1, User.query.count())), body=fake.sentence(), timestamp=fake.date_time_this_year(), post=Post.query.get(random.randint(1, Post.query.count()))) db.session.add(comment) db.session.commit() salt = int(count * 0.1) for i in range(salt): comment = Comment( body=fake.sentence(), timestamp=fake.date_time_this_year(), replied=Comment.query.get(random.randint(1, Comment.query.count())), post=Post.query.get(random.randint(1, Post.query.count())), author=User.query.get(random.randint(1, User.query.count()))) db.session.add(comment) db.session.commit()
def post(post_id): post = Post.query.get_or_404(post_id) form = CommentForm() if form.validate_on_submit(): comment_post = Comment(content=form.content.data, entry=post, author=current_user) db.session.add(comment_post) db.session.commit() flash('Your comment has been created!', 'success') return redirect(url_for('post',post_id=post.id)) comment = Comment.query.filter_by(post_id=post_id).order_by(Comment.date_posted.desc()).all() return render_template('post.html', post=post, form=form, comment=comment)
def create_comment(post_id): post = Post.query.get(post_id) form = CommentForm() if form.validate_on_submit(): comment = Comment(content=form.content.data, author=current_user, ref_post=post) db.session.add(comment) db.session.commit() return redirect(url_for('post', post_id=post.id)) return render_template('create_comment.html', title=post.title, post=post, legend='New Comment', form=form)
def comment_c(request): if request.method == 'POST': user = get_user(request) if user is None: return JsonResponse({'status': -1, 'message': 'invalid token'}) form = AddCommentForm(request.POST) if form.is_valid(): print('form is valid') text = form.cleaned_data['text'] post_id = form.cleaned_data['post_id'] post = Post.objects.filter(post_id=post_id).first() if post is not None: comment = Comment.create(post, text) comment.save() print(comment) ans = {'status': 1} temp = {'text': text, 'datetime': comment.time} ans['comment'] = temp print(ans) return JsonResponse({'status': 1, 'comment': ans}) else: print('form is not valid') return JsonResponse({'status': -1, 'message': 'Under Construction'})