Пример #1
0
def post_comment(request, article_id):
    article = get_object_or_404(Article, id=article_id)

    if not request.user.is_authenticated:
        return JsonResponse({'code': 2, 'message': '你还没有登陆'})

    if request.method == "POST":
        form = CommentsForm(request.POST)
        if form.is_valid():
            comment = form.save(False)
            comment.article = article
            if request.POST['parent'] != '-1':
                parent = Comment.objects.get(id=request.POST['parent'])
                comment.parent = parent
            comment.author = request.user
            comment.save()
            comment.pub_time = timezone.localtime(comment.pub_time)
            res = {
                'code': 1,
                'message': '评论成功',
                'cur_comment': {
                    'cid': comment.id,
                    'ctext': comment.text,
                    'ctime': comment.pub_time.strftime('%Y-%m-%d %H:%M'),
                    'cparent': comment.parent_id,
                    'uid': comment.author.id,
                    'uname': comment.author.username,
                },
            }
            return JsonResponse(res)
        else:
            return JsonResponse({'code': 0, 'message': '数据验证错误,评论失败'})

    return JsonResponse({'code': 0, 'message': 'method错误,评论失败'})
Пример #2
0
    def post(self, request):
        # 接受参数
        data = request.POST
        data = dict(data)
        # 获取当前用户的id
        user_id = request.session.get('id')
        blog_id = int(data.get("bid")[0])
        # 判断该用户是否具备评价资格
        if UserModel.objects.get(pk=user_id).user_status != 1:
            return JsonResponse(json_msg(2, "您已经被禁言!7天后解除限制!"))

        # 判断评论id是否存在
        parent_id = int(data.get("parent_id")[0])

        # 主要是对评论内容进行判断合法性
        content = data.get("content")
        content = ''.join(content)
        # 判断评论的长度够不够
        # 创建form对象, 验证评论内容的合法性
        data["content"] = content
        form = CommentsForm(data)
        # 判断数据的合法性
        if form.is_valid():
            # 数据合法的情况下
            # 将数据保存到数据库中
            CommentsModel.objects.create(
                content=content,
                parent_id=parent_id,
                user_id=user_id,
                blogarticle_id=blog_id
            )
            return JsonResponse(json_msg(0, "评论成功!", data=blog_id))
        else:
            return JsonResponse(json_msg(4, "评论不能为空!", data=blog_id))
Пример #3
0
def add(request):
    '''
    обработка добавления коментария в базу
    '''
    try:
        page = request.META['HTTP_REFERER']
    except KeyError:
        page = False
    if page:
        if request.method == 'POST':
            comment_form = CommentsForm(request.POST)
            if comment_form.is_valid():
                comment_form.save()
                messages.info(request, 'Ваш коментарий успешно отправлен')
                messages.info(
                    request,
                    'После проверки модератором он будет доступен на сайте')
                return HttpResponseRedirect(page)
            else:
                messages.info(request, 'Ошибка добавления коментария')
                messages.info(request, 'Проверьте введенные данные')
                return HttpResponseRedirect(page)
    else:
        messages.info(request, 'Ошибка добавления коментария')
        messages.info(request, 'Проверьте введенные данные')
        return HttpResponseRedirect('/')
Пример #4
0
    def post(self, request):
        # 接受用户传来的数据
        data = request.POST
        data = dict(data)
        # 获取当前用户的id
        user_id = request.session.get("id")
        if user_id is None:
            return JsonResponse(json_msg(1, "您还未登录!"))
        # 判断用户的权限是否足够
        if UserModel.objects.get(pk=user_id).user_status != 1:
            return JsonResponse(json_msg(2, "您已经被禁言!7天后解除限制!"))
        # 获取留言的id
        parent_id = int(data.get("parent_id")[0])

        # 判断留言的合法性
        content = data.get("content")
        content = "".join(content)

        # 判断留言长度是否足够
        data["content"] = content
        # 创建form验证对象
        form = CommentsForm(data)
        # 判断
        if form.is_valid():
            # 数据合法  将数据保存到数据库
            BoardModel.objects.create(
                content=content,
                user_id=user_id,
                parent_id=parent_id
            )
            return JsonResponse(json_msg(0, "留言成功!"))
        else:
            # 数据不合法
            return JsonResponse(json_msg(3, "评论不能为空!"))
Пример #5
0
 def get_context_data(self, **kwargs):
     context = super(BlogDetailView, self).get_context_data(**kwargs)
     form = CommentsForm()
     commentlist = self.object.comments_set.all()
     context.update({
         'form': form,
         'commentlist': commentlist,
     })
     return context
Пример #6
0
    def get_context_data(self, **kwargs):
        # 复写get_context_data 的目的是因为除了将post传递给模板为(DetailView 已经帮我们完成)
        # 还要把评论表单,post下的评论列表传递给模板

        context = super(PostDetailView, self).get_context_data(**kwargs)
        form = CommentsForm()
        comment_list = self.object.comments_set.all()  #评论列表
        context.update({'form': form, 'comment_list': comment_list})
        return context
Пример #7
0
def detail(request, pk):
    article = get_object_or_404(Article, pk = pk)
    form = CommentsForm()
    comment_list = article.comments_set.all()
    context = {'article': article,
               'form': form,
               'comment_list': comment_list
               }
    return render(request, "myblog/detail.html", context )
Пример #8
0
 def get_context_data(self, **kwargs):
     # 覆写 get_context_data 的目的是因为除了将 article 传递给模板外(DetailView 已经帮我们完成),
     # 还要把评论表单、article 下的评论列表传递给模板。
     context = super(ArticleDetailView, self).get_context_data(**kwargs)
     form = CommentsForm()
     comment_list = self.object.comment_set.all()
     context.update({
         'form': form,
         'comment_list': comment_list,
     })
     return context
Пример #9
0
def details(request, article_id):
    article_detail = get_object_or_404(Article, pk=article_id)  #当前id 的文章
    article_detail.add_view()  #阅读量+1
    form = CommentsForm()  #表单
    comment_list = article_detail.comment_set.all()  #评论列表
    context = {
        'article_detail': article_detail,
        'comment_list': comment_list,
        'form': form
    }
    return render(request, 'blog/detail.html', context=context)
Пример #10
0
def detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    form = CommentsForm()
    comments_list = post.comments_set.all()
    comment_list = list(comments_list)
    comment_dict = tree_comments(comment_list)
    return render(request,
                  'blog/detail.html',
                  context={
                      'post': post,
                      'form': form,
                      'comment_dict': comment_dict
                  })
Пример #11
0
def detail(request, pk):
    post = get_object_or_404(Article, pk=pk)
    post.increase_views()
    post.body = markdown.markdown(post.body,
                                  extensions=[
                                      'markdown.extensions.extra',
                                      'markdown.extensions.codehilite',
                                      'markdown.extensions.toc',
                                  ])
    form = CommentsForm()
    comment_list = post.comments_set.all()
    context = {'post': post, 'form': form, 'comment_list': comment_list}

    return render(request, 'blog/detail.html', context=context)
Пример #12
0
def detail(request, id):
    # try:
    #     article = Article.objects.get(pk=id)
    # except Article.DoesNotExist:
    #     raise Http404

    article = get_object_or_404(Article, pk=id)
    article.views += 1
    article.save()
    commentsForm = CommentsForm()
    return render(request,
                  'home/article.html',
                  context={
                      'article': article,
                      'commentsForm': commentsForm
                  })
Пример #13
0
def view_single_incident(request, incident_id):
    if request.user.perms.access_level > 4:
        return render_to_response('unauthorized.html', context_instance=RequestContext(request))
    incident = get_object_or_404(Incident, pk=incident_id)
    if settings.JIRA_ENABLED:
        jira_enabled = True
        jira_url = '%s/browse/' % settings.JIRA_OPTS.get('URL')
        try:
            jira = JIRATickets.objects.get(incident=incident)
        except JIRATickets.DoesNotExist:
            pass
    incidentlogs = IncidentLog.objects.filter(incident=incident).order_by('timestamp')
    eventactionlogs = EventActionLog.objects.filter(incident=incident).order_by('dateAdded')
    comments = Comments.objects.filter(incident=incident).order_by('date_added')
    comments_form = CommentsForm()
    redirect_to = '/incidents/view/%s/' % incident.id
    return render_to_response('view_incident.html', locals(), context_instance=RequestContext(request))
Пример #14
0
def detail(request, pk):
    #作用是当传入的 pk对应的 Post在数据库存在时返回post; 如果不存在,返回404
    post = get_object_or_404(Post, pk=pk)

    #导入markdown这样我们在模板中展示 {{ post.body }} 就是渲染过后的 HTML 文本
    #传递的额外参数 extensions是对 Markdown 语法的拓展
    #extra 本身包含很多拓展、 codehilite 是语法高亮拓展、toc 允许我们自动生成目录
    post.body = markdown.markdown(post.body,
                                  extensions=[
                                      'markdown.extensions.extra',
                                      'markdown.extensions.codehilite',
                                      'markdown.extensions.toc',
                                  ])
    form = CommentsForm()
    comments_list = post.comments_set.all()
    context = {'post': post, 'form': form, 'comments_list': comments_list}
    return render(request, 'blog/detail.html', context)
Пример #15
0
def detail(request,pk):
    post = get_object_or_404(Post,pk=pk)
    post.body = markdown.markdown(post.body,extensions=[
        'markdown.extensions.extra',
        'markdown.extensions.codehilite',
        'markdown.extensions.toc',
    ])
    form = CommentsForm()
    # 获取这篇post下的全部评论
    comment_list = post.comments_set.all()

    # 将文章,表单,以及文章下的评论列表作为作为模板变量传给 detail.html 模板,以便渲染相应数据
    context = {
        'post':post,
        'form':form,
        'comment_list':comment_list
    }

    return render(request,'blog/detail.html',context=context)
Пример #16
0
def view_comments(request, application, model, record_id):
    '''
    добавление на страницу списка добавленных комментариев
    '''
    all_comments = Comments.objects.filter(applications=application,
                                           model=model,
                                           record_id=record_id,
                                           moderation=True)
    data = {'comment_form': False, 'all_comments': all_comments}
    if is_login(request):
        comment_form = CommentsForm(
            initial={
                'applications': application,
                'model': model,
                'record_id': record_id,
                'user': user_id(request)
            })
        data['comment_form'] = comment_form
    data.update(csrf(request))
    return render_to_string('comments.html', data)
Пример #17
0
    def post(self, request, article_id):
        comment_form = CommentsForm(request.POST)
        article = get_object_or_404(Article, pk=article_id)

        if comment_form.is_valid() and article:

            parent_id = request.POST.get("parent_id", "")
            nick_name = request.POST.get("nick_name", "")
            email = request.POST.get("email", "")
            url = request.POST.get("url", "")
            text = request.POST.get("text", "")
            comment = Comments()
            comment.nick_name = nick_name
            comment.parent_id = parent_id
            comment.email = email
            comment.url = url
            comment.text = text
            comment.article = article
            comment.save()
        return redirect(article.get_absolute_url() + "#comments")
Пример #18
0
def blog_detail(request, blog_id):
    blog = get_object_or_404(Blog, id=blog_id)
    read_cookie_key = read_statistics_once_read(request, blog)
    previous_blog = Blog.objects.filter(
        create_time__gt=blog.create_time).last()
    next_blog = Blog.objects.filter(create_time__lt=blog.create_time).first()
    content_type = ContentType.objects.get_for_model(blog)
    comments = Comments.objects.filter(content_type=content_type,
                                       object_id=blog.id)[:10]
    comment_form = CommentsForm(initial={
        'object_id': blog.id,
        'content_type': content_type.model
    })
    content = {
        'blog': blog,
        'previous_blog': previous_blog,
        'next_blog': next_blog,
        'comments': comments,
        'comment_form': comment_form
    }
    response = render(request, 'blog_detail.html', content)
    response.set_cookie(read_cookie_key, 'true')  # 阅读cookie标记
    return response
Пример #19
0
    def post(self, request):
        # 接受参数
        data = request.POST
        data = dict(data)
        # 获取当前用户的id
        user_id = request.session.get('id')
        # 判断该用户是否具备评价资格
        if UserModel.objects.get(pk=user_id).user_status != 1:
            return JsonResponse(json_msg(2, "您已经被禁言!7天后解除限制!"))

        # 判断评论id是否存在
        parent_id = int(data.get("parent_id")[0])
        try:
            BoardModel.objects.get(pk=parent_id)
        except BoardModel.DoesNotExist:
            return JsonResponse(json_msg(3, "回复的留言不存在!"))

        # 主要是对评论内容进行判断合法性
        content = data.get("reply")
        content = ''.join(content)
        # 判断评论的长度够不够
        # 创建form对象, 验证评论内容的合法性
        data["content"] = content
        form = CommentsForm(data)
        # 判断数据的合法性
        if form.is_valid():
            # 数据合法的情况下
            # 将数据保存到数据库中
            BoardModel.objects.create(
                content=content,
                parent_id=parent_id,
                user_id=user_id,
            )
            return JsonResponse(json_msg(0, "回复成功!"))
        else:
            return JsonResponse(json_msg(4, "回复不能为空!"))
Пример #20
0
 def get_context_data(self, **kwargs):
     kwargs['comment_form'] = CommentsForm()
     kwargs['comments'] = Comments.objects.filter(
         movie__id=kwargs['object'].pk).order_by('-comment_time')
     return super().get_context_data(**kwargs)