def post_detail_view(request, year, month, day, post): post1 = Post.objects.get(title=post, status='published', publish__year=year) #publish__month=month) # publish__day=day) commentss = post1.commentss.filter(active=True) csubmit = False current_user = request.user if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): name = current_user.username email = current_user.email comment = request.POST.get('comment') data = Comments(name=name, email=email, comment=comment) data.post = post1 data.save() csubmit = True else: form = CommentForm() return render( request, 'blog/post_detail.html', { 'post2': post1, 'sidebar': True, 'comments': commentss, 'form': form, 'csubmit': csubmit })
def post_comment(request, article_id): """ function to handle post request from comment form. if valid form, save form data into database and return a redirected page. if not, return previous page. :param article_id: captured from url. :param request: http POST request :return: redirected page if valid; otherwise, previous page with prompt message """ # session input to auto-fill request.session['name'] = request.POST.get('name') request.session['email'] = request.POST.get('email') comment = Comments() # create a instance of Comments class comment.article = get_object_or_404(Post, pk=article_id) if request.POST.get('reply') != '0': # if reply to a comment comment.reply = Comments.objects.get(pk=request.POST.get('reply')) # get reply objective form = CommentForm(request.POST, instance=comment) # combine input form data and the instance to create a whole CommentForm instance if form.is_valid(): # if form is not valid try: messages.success(request, 'Your comment was added successfully!') form.save() # save posted form date into database request.session['content'] = '' # session nothing if successful return redirect('blog:article', article_id) except Exception: request.session['content'] = request.POST.get('content') # save input content in session if fail messages.warning(request, 'AN EXCEPTION OCCURS!') messages.error(request, 'Please correct your error above!') # if not a valid form
def blog_detail(request, pkey): post = Post.objects.get(pk=pkey) form = CommentForm() success_msg = "" if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = Comments( author=form.cleaned_data['author'], body=form.cleaned_data['body'], post=post ) comment.save() success_msg = "Successfully Submitted! :)" form = CommentForm() else: success_msg = "Failed to submit. :(" comments = Comments.objects.filter(post=post) contxt = {'title': post.title, 'post': post, 'comments': comments, 'form': form, 'success_msg': success_msg} return render(request, 'blog_detail.html', contxt)
def blog_detail(request, pk): post = Post.objects.get(pk=pk) posts = Post.objects.all().order_by('-created_on') #comments form = CommentsForm() if request.method == 'POST': form = CommentsForm(request.POST) if form.is_valid(): comment = Comments(author=form.cleaned_data["author"], body=form.cleaned_data["body"], post=post) comment.save() return redirect('blog_index') comments = Comments.objects.filter(post=post) context = { "post": post, "list": posts[:3], "comments": comments, "form": form } return render(request, "blog_detail.html", context)
def comment(request): if request.method == 'POST': #new_comm = CommForm(request.POST) comments = request.POST['newcomments'] if not comments.strip() == "": pid = request.POST['pid'] #pid = -1 timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) mid = request.POST['mid'] # if request.META.has_key('HTTP_X_FORWARDED_FOR'): # ip = request.META['HTTP_X_FORWARDED_FOR'] # else: # ip = request.META['REMOTE_ADDR'] ip = getIPFromDJangoRequest(request) iplocation = getIPLocationFromsite(ip) t = Mail.objects.get(id=mid) t.comment_num = t.comment_num + 1 t.save() floor = t.comment_num p = Comments(pid=pid, timestamp=timestamp, user_ip=ip, user_content=comments, comments_id=mid, user_location=iplocation, floor=floor) p.save() return render_to_response('blog/savedone.html', {'mid': mid}) else: mid = request.POST['mid'] return render_to_response('blog/savefalse.html', {'mid': mid}) else: pass
def post(request, id): if request.method == 'GET': # 获取文章内容 post = Post.objects.filter(id=id).first() comment_form = CommentsForm() # 添加阅读数 post.page_view += 1 post.save() context = {'post': post, 'comment_form': comment_form} return render(request, 'blog/post.html', context) elif request.method == 'POST': comment_form = CommentsForm(request.POST) print(comment_form) if comment_form.is_valid(): comm = request.POST['comment'] id = int(request.POST['id']) post = Post.objects.get(id=id) # user = BlogUser.objects.get(id=1) obj = Comments(post=post, content=comm) try: obj.save() except: return HttpResponse('error') return HttpResponseRedirect('/blog/post/' + str(id)) else: id = int(request.POST['id']) post = Post.objects.filter(id=id).first() comment_form = CommentsForm() context = {'post': post, 'comment_form': comment_form} return render(request, 'blog/post.html', context) else: pass
def post(self, request, *args, **kwargs): new_comment = Comments( Comment=request.POST.get('Comment'), author=self.request.user, post_connected=self.get_object(), ) new_comment.save() # return self.get(self, request, *args, **kwargs)
def comment(request, slug): if request.method == "POST": form = (request.POST) id = Articles.objects.get(slug=slug).id comment = Comments(comments=form['comment'], article_id=id) comment.save() messages.info(request, "Your comment has been successfully added") prev = request.POST.get('prev', '/') return (HttpResponseRedirect(prev))
def new_comment(post_id): post = Post.query.get_or_404(post_id) form = CommentForm() if form.validate_on_submit(): comment = Comments(post_id=post_id, author_id=post.author.id, content=form.content.data) db.session.add(comment) db.session.commit() flash('The comment has been created', 'success') return redirect(url_for('posts.post', post_id=post.id)) return render_template('add_comment.html', title='New Comment', form=form, legend='New Comment')
def submitComment(request, blog_id): blog = Blog.objects.get(pk=blog_id) blog.num_of_comments += 1 comment = Comments() comment.blog = blog comment.commenter = request.POST['commenter'] comment.email = request.POST['email'] comment.content = request.POST['content'] comment.posted = request.POST['posted'] comment.save() blog.save() return HttpResponseRedirect(reverse('entry', args=(blog.id, )))
def postComment(request): if request.method == 'POST': comment = request.POST.get('comment') user = request.user postSno = request.POST.get('postSno') post = BlogAppPost.objects.get(sno=postSno) parent = request.POST.get('parent') parentSno = request.POST.get('parentSno') if parentSno == "": comment = Comments(comment=comment, post=post, user=user) messages.success(request, "Your comment has been sent successfully.") else: parent = Comments.objects.get(sno=parentSno) comment = Comments(comment=comment, post=post, user=user, parent=parent) messages.success(request, "Your reply has been sent successfully.") comment.save() # messages.success(request,"Your comment has been sent successfully.") return redirect(f'/blog/{post.slug}')
def blog_detail(request, pk): post = Post.objects.get(pk=pk) form = comment_form() if request.method == 'POST': form = comment_form(request.POST) if form.is_valid(): comment = Comments(author=form.cleaned_data["author"], body=form.cleaned_data["body"], post=post) comment.save() comments = Comments.objects.filter(post=post) context = {"post": post, "comments": comments, "form": form} return render(request, "blog_detail.html", context)
def comment(request): if request.method=='POST': form=CommentForm(request.POST) comment_post_id=request.POST.get('comment_post_ID').strip() frequency_comment=request.session['frequency_comment'] if frequency_comment: contexts={'frequency_comment':'评论太频繁了!'} return article(request,comment_post_id,contexts) if form.is_valid(): comment_content=request.POST.get('comment') comment_author=request.POST.get('author').strip() comment_email=request.POST.get('email').strip() comment_url=request.POST.get('url').strip() comment_parent=request.POST.get('comment_parent').strip() comment_author_ip=get_client_ip(request) comment_agent=request.META.get('HTTP_USER_AGENT',None) if comment_post_id==None or comment_parent==None: return index(request) if comment_author=='' or comment_email=='' or comment_content=='': return article(request,comment_post_id) p=Posts.objects.get(pk=comment_post_id) comment=Comments(comment_post=p,comment_approved='0',comment_author=comment_author,comment_parent=comment_parent,comment_content=comment_content,comment_author_email=comment_email,comment_author_url=comment_url,comment_author_ip=comment_author_ip,comment_agent=comment_agent) # comment.comment_date=datetime() p.comment_count=p.comment_count+1 p.save() comment.save() request.session['comment_author']=comment_author request.session['comment_email']=comment_email #context={'test':comment_post_id} if(p.post_type=='post'): #return article(request,comment_post_id) return HttpResponseRedirect('/blog/article/'+comment_post_id+'#comment-%s'%comment.comment_id)#防止重复提交 elif(p.post_type=='page'): #return page(request,comment_post_id) return HttpResponseRedirect('/blog/page/'+comment_post_id)#防止重复提交 else: #t = loader.get_template('test.html') #return HttpResponse(t.render(Context({'form':form})) ) contexts={'comment_form':form} return article(request,comment_post_id,contexts) pass else: pass return index(request)
def comment(): commentId = request.form.get('CommentId') if commentId is None: comment = Comments(content=request.form.get('Content'), postId=request.form.get('PostId'), authorId=current_user.get_id()) db.session.add(comment) else: comment = Comments.query.filter_by(authorId=current_user.userId, commentId=commentId).first() if request.form.get('Delete'): db.session.delete(comment) else: comment.content = request.form.get('Content') comment.updatedAt = datetime.utcnow() db.session.commit() return jsonify({'status': 'Success'})
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 = Comments(author=form.cleaned_data['author'], body=form.cleaned_data['body'], post=post) comment.save() form = CommentForm() comments = Comments.objects.filter(post=post) context = { 'form': form, 'post': post, 'comments': comments, } return render(request, 'blogs/blog_detail.html', context)
def reply_comment(): bid = int(request.form.get('bid')) cid = int(request.form.get('cid')) content = request.form.get('content', '').strip() uid = session.get('uid') now = datetime.datetime.now() # 如果回复是空 if not content: bid = int(request.form.get('bid')) blog = Blogs.query.get(bid) comment = Comments.query.filter_by(bid=bid).order_by(Comments.create_time.desc()) return render_template('read.html', error=2, blog=blog, comment=comment) comment = Comments(uid=uid, bid=bid, cid=cid, content=content, create_time=now) db.session.add(comment) db.session.commit() return redirect(f'/blog/read?bid={bid}')
def post(post_id): post = Post.query.get_or_404(post_id) form = CommentForm() comments = Comments.query.filter_by(post_id=post.id).order_by( Comments.time_posted.desc()).all() if request.method == 'POST': comment = Comments(name=form.name.data, email=form.email.data, message=form.message.data, post_id=post.id) db.session.add(comment) # post.comment += 1 flash('Your comment has been submitted!', 'success') return redirect(url_for('posts.post', post_id=post.id)) return render_template('post.html', title=post.title, post=post, form=form, comments=comments)
def blog_detail(request, *args, **kwargs): blog = Blog.objects.get(id=id) selected_blog_id = kwargs['productId'] blog: Blog = Blog.objects.get_by_id(selected_blog_id) if blog is None or not blog.active: raise Http404('محصول مورد نظر یافت نشد') blog.visit_count += 1 blog.save() related_blogs = Blog.objects.get_queryset().filter( categories__blog=blog).distinct() grouped_related_blogs = my_grouper(3, related_blogs) comments = Comments.objects.all().filter(blog=blog) if request.user == "POST": form = CommentForm(request.POST) if form.is_valid(): new_name = form.cleaned_data['name'] new_email = form.cleaned_data['email'] new_message = form.cleaned_data['message'] new_Comments = Comments(blog=blog, name=new_name, email=new_email, message=new_message) new_Comments.save() context = { 'blog': blog, "related_blog": grouped_related_blogs, "comments": comments, } tag = Tag.objects.first() return render(request, 'blog_pages/blog_detail.html', context)
def save_comment(request, *args, **kwargs): post_id = request.GET.get('post_id') email = request.GET.get('email') name = request.GET.get('name') entry = request.GET.get('comment') # post = get_object_or_404(Post, pk=post_id) post = Post.objects.get(pk=post_id) comment = Comments(post=post, name=name, email=email, entry=entry) comment.save() response = { 'email': comment.email, 'name': comment.name, 'entry': comment.entry, 'publ_date': comment.get_display_date() } return HttpResponse( json.dumps(response), 'content-type: text/json', **kwargs )
def blog_detail(request, pk): post = Post.objects.get(pk=pk) logger.debug("start") form = CommentForm() if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = Comments( author=form.cleaned_data["author"], body=form.cleaned_data["body"], post=post, ) comment.save() else: logger.error("Invalid form") comments = Comments.objects.filter(post=post) context = {"post": post, "comments": comments, "form": form} return render(request, "blog_detail.html", context)
def post_comment(): comment = Comments(sender=current_user.id, post_id=request.form['post_id'], content=request.form['comment']) if request.form['comment']: db.session.add(comment) db.session.commit() return redirect(url_for('blog'))
def comment(request): if request.method=='POST': data=request.POST if request.user.is_authenticated(): data={} if request.user.user_nicename: data['author']=request.user.user_nicename elif request.user.display_name: data['author']=request.user.display_name elif request.user.user_login: data['author']=request.user.user_login else: data['author']='' data['email']=request.user.user_email data['url']=request.user.user_url data['comment']=request.POST.get('comment').strip() # print 'is is_authenticated',data form=CommentForm(data) comment_post_id=request.POST.get('comment_post_ID').strip() frequency_comment=request.session['frequency_comment'] if request.user.has_perm('blog.can_comment_unlimit_time'): frequency_comment=None if frequency_comment: contexts={'frequency_comment':'评论太频繁了!'} return article(request,comment_post_id,contexts) # print 'comment save3' if form.is_valid(): # print 'comment save2' comment_content=form.cleaned_data['comment'] # print comment_content comment_author=form.cleaned_data['author'] comment_email=form.cleaned_data['email'] comment_url=form.cleaned_data['url'] comment_parent=request.POST.get('comment_parent').strip() comment_author_ip=get_client_ip(request) comment_agent=request.META.get('HTTP_USER_AGENT',None) if comment_post_id==None or comment_parent==None: return index(request) if comment_author=='' or comment_email=='' or comment_content=='': return article(request,comment_post_id) comment_approved=0; if request.user.is_authenticated(): if request.user.has_perm('blog.can_comment_direct'): comment_approved=1 p=Posts.objects.get(pk=comment_post_id) comment=Comments(comment_post=p,comment_approved=comment_approved,comment_author=comment_author,comment_parent=comment_parent,comment_content=comment_content,comment_author_email=comment_email,comment_author_url=comment_url,comment_author_ip=comment_author_ip,comment_agent=comment_agent) # comment.comment_date=datetime() p.comment_count=p.comment_count+1 p.save() comment.save() # print 'comment save' request.session['comment_author']=comment_author request.session['comment_email']=comment_email #context={'test':comment_post_id} if(p.post_type=='post'): #return article(request,comment_post_id) return HttpResponseRedirect('/blog/article/'+comment_post_id+'#comment-%s'%comment.comment_id)#防止重复提交 elif(p.post_type=='page'): #return page(request,comment_post_id) return HttpResponseRedirect('/blog/page/'+comment_post_id)#防止重复提交 else: #t = loader.get_template('test.html') #return HttpResponse(t.render(Context({'form':form})) ) contexts={'comment_form':form} return article(request,comment_post_id,contexts) pass else: pass return index(request)
def blog_post(blog_post_id): post = BlogPost.query.get_or_404(blog_post_id) form = CommentForm() previous_comments = Comments.query.filter_by(blog_id=post.id).order_by( Comments.date.desc()).all() if (form.validate_on_submit() or request.method == "POST"): comment = Comments(blog_post_id, current_user.id, form.text.data) db.session.add(comment) if (current_user.id != post.author.id): notif = Notifications( post.author.id, f'{current_user.username} has commented on your blog "{post.title}"!', post.id, True) db.session.add(notif) db.session.commit() return redirect( url_for('blog_posts.blog_post', blog_post_id=blog_post_id)) if (current_user.is_authenticated and current_user.email != post.author.email): user = User.query.get_or_404(current_user.id) user.last_viewed_catagory3 = user.last_viewed_catagory2 user.last_viewed_catagory2 = user.last_viewed_catagory1 user.last_viewed_catagory1 = post.category db.session.commit() view = View.query.filter_by(user_id=current_user.id, blog_id=blog_post_id).first() if (not view): post.views += 1 view = View(current_user.id, blog_post_id) db.session.add(view) db.session.commit() if (current_user.is_authenticated): notifs = Notifications.query.filter_by( user_id=current_user.id).order_by(Notifications.date.desc()).all() else: notifs = [] if (current_user.is_authenticated): like_stat = Likes.query.filter_by(user_id=current_user.id, blog_id=blog_post_id).first() else: like_stat = None like_count = db.engine.execute(f''' select count(*) from Likes where blog_id={blog_post_id} and like={1} ''') dislike_count = db.engine.execute(f''' select count(*) from Likes where blog_id={blog_post_id} and like={0} ''') like_count = [res[0] for res in like_count][0] dislike_count = [res[0] for res in dislike_count][0] if (like_count == None): like_count = 0 if (dislike_count == None): like_count = 0 if (like_stat): like_val = like_stat.like else: like_val = None return render_template('blog_posts.html', title=post.title, date=post.date, post=post, category=post.category, notifs=notifs, like_val=like_val, like_count=like_count, dislike_count=dislike_count, previous_comments=previous_comments, form=form, User=User)