def story(request, base62_id): action_keys = filter(lambda i: i in ['delete', 'publish'], request.POST) action_key = action_keys[0] if action_keys else None if action_key: method = {u'delete': _delete_story, u'publish': _publish_story}.get(action_key) if method: story = get_object_or_404(Story, id=base62.to_decimal(base62_id), owner=request.user) return method(request, story) statuses_in = [Story.DRAFT, Story.PUBLISHED] if request.user.is_authenticated() \ else [Story.PUBLISHED] story = get_object_or_404(Story, id=base62.to_decimal(base62_id), status__in=statuses_in, owner__is_active=True) story_is_visible = story.is_visible_for(request.user) comments = Comment.objects\ .filter(story=story, status=Comment.PUBLISHED, owner__is_active=True)\ .select_related('owner__profile') comments = paginated(request, comments, settings.COMMENTS_PER_PAGE) if request.user.is_authenticated(): if request.method == 'POST' and request.POST.get('action') ==\ 'create_comment': comment_form = CommentForm(request.POST, owner=request.user, story=story) if comment_form.is_valid(): comment = comment_form.save() return HttpResponseRedirect(comment.get_absolute_url()) else: comment_form = CommentForm(owner=request.user, story=story) else: comment_form = None return render(request, 'story/story_detail.html', {'story': story, 'current_site': Site.objects.get_current(), 'story_is_visible': story_is_visible, 'comments': comments, 'comment_form': comment_form})
def art_comment(request, art_pk): #book = models.Art.objects.filter(id=int(art_pk)) #方法1 art = get_object_or_404(models.Art, pk=art_pk) #方法2 if request.method == "POST": form = CommentForm(data=request.POST) if form.is_valid(): #评论表单合法 cmt = Comment(name=form.cleaned_data['name'], title=form.cleaned_data['title'], text=form.cleaned_data['text'], art=art) cmt.save() comment_list = art.comment_set.all() #通过外键反查所有评论 comment_count = comment_list.count(), context = dict( art=art, form=form, comment_list=comment_list, comment_count=comment_count, ) return render(request, "book_detail.html", context=context) else: comment_list = art.comment_set.all() # 通过外键反查所有评论 comment_count = comment_list.count(), context = dict( art=art, form=form, comment_list=comment_list, comment_count=comment_count, ) flash(request, "error", "用户提交评论失败!") return render(request, "book_detail.html", context=context) return redirect(art)
def comment_threads(request, comment_id): comment = None if not comment_id is None: try: comment = Comment.objects.get(id=int(comment_id)) content_object = comment.content_object content_id = comment.content_object.id except: pass initial_setup = { "content_type": content_object.get_instance_content_type, "object_id": comment.object_id } # print(comment.is_parent) post = comment.content_object form = CommentForm(data=request.POST or None, initial=initial_setup) if form.is_valid(): # print(request.POST) cd = form.cleaned_data ctype = cd.get('content_type') content_type = ContentType.objects.get(model=ctype) obj_id = cd.get('object_id') content = cd.get('content') parent_obj = None try: parent_id = int(request.POST.get('parent_id')) except: parent_id = None if parent_id: parent_qry = Comment.objects.filter(id=parent_id) if parent_qry.exists() and parent_qry.count() == 1: parent_obj = parent_qry.first() comment_obj = Comment( user=request.user, content=content, content_type=content_type, object_id=obj_id, parent=parent_obj, ) comment_obj.save() create_action(request.user, 'replied', comment_obj) return redirect('comment:comment-threads', comment_id=comment_id) # redirect to parent comment context = {} context['comment'] = comment context['post'] = post context['form'] = form template = 'comment/comment_threads.html' return render(request, template, context)
def addcomment(): form = CommentForm() error = 'Sorry, Post Comments Error!' if form.validate_on_submit(): print 1 comment = Comment(author_ip=request.environ['HTTP_X_FORWARDED_FOR']) form.populate_obj(comment) db.session.add(comment) post = Post.query.getpost_id(comment.post_id) post.comment_count += 1 db.session.commit() return redirect(url_for('article', postid=comment.post_id)) return render_template('/error.html', content=error)
def addcomment(): form = CommentForm() error = 'Sorry, Post Comments Error!' if form.validate_on_submit(): author_ip = request.environ.get('HTTP_X_FORWARDED_FOR') or '127.0.0.1' comment = Comment(author_ip=author_ip) form.populate_obj(comment) db.session.add(comment) post = Post.query.getpost_id(comment.post_id) post.comment_count += 1 db.session.commit() return redirect(url_for('article', postid=comment.post_id)) return render_template('/error.html', content=error)
def addcomment(): form = CommentForm() error = "Sorry, Post Comments Error!" if form.validate_on_submit(): print 1 comment = Comment(author_ip=request.environ["HTTP_X_FORWARDED_FOR"]) form.populate_obj(comment) db.session.add(comment) post = Post.query.getpost_id(comment.post_id) post.comment_count += 1 db.session.commit() return redirect(url_for("article", postid=comment.post_id)) return render_template("/error.html", content=error)
def article_detail(request, id): # article = ArticlePost.objects.get(id=id) comments = Comment.objects.filter(article=id) article.total_views += 1 article.save(update_fields=['total_views']) #将markdown语法渲染成html样式 # 修改 Markdown 语法渲染 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) article.body = md.convert(article.body) comment_form = CommentForm() # 新增了md.toc对象 context = { 'article': article, 'toc': md.toc, 'comments': comments, 'comment_form': comment_form, } return render(request, 'article/detail.html', context)
def post(self, request, post_id, *args, **kwargs): post = Post.objects.get(id=int(post_id)) comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.post = post comment.save() url = reverse('blog:post_detail', kwargs={'pk': post_id}) return redirect(url) else: comment_list = post.comment_set.all() context = {'post': post, 'form': comment_form, 'comment_list': comment_list } return render(request, 'single.html', context=context)
def article_1(postid=5): categorys = Category.query.getall() hot = Post.query.hottest()[:20] new = Post.query.newpost()[:20] tag = Tag.query.getall() shuffle(tag) tag = tag[:20] comments = Comment.query.newcomment()[:20] articles = Post.query.getall() shuffle(articles) articles = articles[:5] post = Post.query.get_or_404(postid) form = CommentForm() postcoments = post.comments.all() post.view_num += 1 db.session.commit() return render_template('/post.html', post=post, articles=articles, categorys=categorys, hotarticles=hot, newpost=new, tags=tag, comments=comments, postcoments=postcoments, form=form)
def story(request, base62_id): action_keys = filter(lambda i: i in ["delete", "publish"], request.POST) action_key = action_keys[0] if action_keys else None if action_key: method = {u"delete": _delete_story, u"publish": _publish_story}.get(action_key) if method: story = get_object_or_404(Story, id=base62.to_decimal(base62_id), owner=request.user) return method(request, story) statuses_in = [Story.DRAFT, Story.PUBLISHED] if request.user.is_authenticated() else [Story.PUBLISHED] story = get_object_or_404(Story, id=base62.to_decimal(base62_id), status__in=statuses_in, owner__is_active=True) blocked_user_ids = compute_blocked_user_ids_for(request.user) story_is_visible = story.is_visible_for(request.user, blocked_user_ids=blocked_user_ids) comments = ( Comment.objects.filter(story=story) .from_active_owners() .visible_for(request.user) .select_related("owner__profile") ) comments = paginated(request, comments, settings.COMMENTS_PER_PAGE) if request.user.is_authenticated(): if request.method == "POST" and request.POST.get("action") == "create_comment": comment_form = CommentForm(request.POST, owner=request.user, story=story) if comment_form.is_valid(): comment = comment_form.save() return HttpResponseRedirect(comment.get_absolute_url()) else: comment_form = CommentForm(owner=request.user, story=story) else: comment_form = None return render( request, "story/story_detail.html", { "story": story, "current_site": Site.objects.get_current(), "story_is_visible": story_is_visible, "comments": comments, "comment_form": comment_form, }, )
def post_comment(request, id): article = get_object_or_404(Article, id=id) # 处理 POST 请求 if request.method == 'POST': ip = base64.b64encode(request.META['REMOTE_ADDR'].encode()) data = QueryDict(mutable=True) data.update(request.POST) data.update({'user': '******' + '_' + ip.decode()}) print(data) comment_form = CommentForm(data) if comment_form.is_valid(): new_comment = comment_form.save(commit=False) new_comment.article = article new_comment.save() return redirect(article) else: #print(comment_form.clean_data) print(comment_form.errors.as_data()) return HttpResponse("表单内容有误,请重新填写。") # 处理错误请求 else: return HttpResponse("发表评论仅接受POST请求。")
def story(request, base62_id): action_keys = filter(lambda i: i in ['delete', 'publish'], request.POST) action_key = action_keys[0] if action_keys else None if action_key: method = { u'delete': _delete_story, u'publish': _publish_story }.get(action_key) if method: story = get_object_or_404(Story, id=base62.to_decimal(base62_id), owner=request.user) return method(request, story) statuses_in = [Story.DRAFT, Story.PUBLISHED] if request.user.is_authenticated() \ else [Story.PUBLISHED] story = get_object_or_404(Story, id=base62.to_decimal(base62_id), status__in=statuses_in, owner__is_active=True) story_is_visible = story.is_visible_for(request.user) comments = Comment.objects\ .filter(story=story, status=Comment.PUBLISHED, owner__is_active=True)\ .select_related('owner__profile') comments = paginated(request, comments, settings.COMMENTS_PER_PAGE) if request.user.is_authenticated(): if request.method == 'POST' and request.POST.get('action') ==\ 'create_comment': comment_form = CommentForm(request.POST, owner=request.user, story=story) if comment_form.is_valid(): comment = comment_form.save() return HttpResponseRedirect(comment.get_absolute_url()) else: comment_form = CommentForm(owner=request.user, story=story) else: comment_form = None return render( request, 'story/story_detail.html', { 'story': story, 'current_site': Site.objects.get_current(), 'story_is_visible': story_is_visible, 'comments': comments, 'comment_form': comment_form })
def post_detail(request, slug): template_name = 'blog/post_detail.html' if cache.get(slug): post = cache.get(slug) else: try: post = get_object_or_404(Post, slug=slug) cache.set(slug, post) except Post.DoesNotExist: return redirect('/') comments = post.comments.filter(active=True) new_comment = None if request.method == 'POST': comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): new_comment = comment_form.save(commit=False) new_comment.post = post new_comment.user = request.user new_comment.email = request.user.email new_comment.active = True new_comment.save() else: ip_address = request.user.ip_address if ip_address not in post.hits.all(): post.hits.add(ip_address) comment_form = CommentForm() return render( request, template_name, { 'object': post, 'comments': comments, 'new_comment': new_comment, 'form': comment_form })
def post_details(request,post_author_username,post_slug): is_auth = functions.is_user_authenticated(request)# user js - to toggle auth-social-modal post = get_object_or_404(Post, author__username = post_author_username, slug = post_slug) # print(post.__class__) authors_post_count = Post.authors_blog_post_count(post) post_url = request.build_absolute_uri(post.get_absolute_url()) facebook_share = "https://www.facebook.com/sharer/sharer.php?u={0}".format(post_url) google_plus_share = "https://plus.google.com/share?url={0}".format(post_url) flag = Like.user_has_liked_post(post.id,request.user) post_likes_count = post.likes.count() context = dict() # comment form initial_setup = { "content_type":post.get_instance_content_type, "object_id": post.id } # print(post.get_instance_content_type) form = CommentForm(data = request.POST or None,initial = initial_setup) if form.is_valid(): # print(request.POST) cd = form.cleaned_data ctype = cd.get('content_type') content_type = ContentType.objects.get(model = ctype) obj_id = cd.get('object_id') content = cd.get('content') parent_obj = None try: parent_id = int(request.POST.get('parent_id')) except: parent_id = None if parent_id: parent_qry = Comment.objects.filter(id = parent_id) if parent_qry.exists() and parent_qry.count() == 1: parent_obj = parent_qry.first() comment = Comment( user = request.user, content = content, content_type = content_type, object_id = obj_id, parent = parent_obj, ) comment.save() create_action(request.user,'commented',comment) return redirect(post.get_absolute_url()) context['form'] = form comments = post.post_comments session_key = 'viewed_post_{}'.format(post.id) if not request.session.get(session_key,False): post.views += 1 post.save() request.session[session_key] = True context['post'] = post context['author_posts_count'] = authors_post_count context['facebook_share'] = facebook_share context['google_plus_share'] = google_plus_share context['post_url'] = post_url context['is_likes'] = flag context['likes_count'] = post_likes_count context['is_auth'] = is_auth context['comments'] = comments template = 'blog/detail.html' return TemplateResponse(request,template,context)