Beispiel #1
0
    def post(self, request, slug):
        article = self.get_object(slug=slug)
        user_agent = request.META.get('HTTP_USER_AGENT', None)
        ip_address = request.META.get('REMOTE_ADDR', None)
        form = CommentForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            content = form.cleaned_data['content']
            Comment.objects.create(
                article=article,
                name=name,
                email=email,
                content=content,
                user_agent=user_agent,
                ip_address=ip_address,
            )
            # Reload form to remove previous values
            form = CommentForm()
            messages.add_message(request, messages.SUCCESS, 'Your comment has been submitted for verification.')
        else:
            messages.add_message(request, messages.ERROR, 'There are errors. Please correct.')

        context = {
            'article': article,
            'comments': self.get_comments(article),
            'form': form,
        }
        return render(request, 'articles/comment-list.html', context)
Beispiel #2
0
 def post(self, request, pk):
     form = CommentForm(request.POST)
     if form.is_valid():
         comment = form.save(commit=False)
         comment.article_id = pk
         comment.author = request.user
         comment.save()
     return redirect('articleDetail', pk=pk)
Beispiel #3
0
def comments_create(request, pk):
    article = get_object_or_404(Article, pk=pk)
    form = CommentForm(request.POST)
    if form.is_valid():
        comment = form.save(commit=False)
        comment.article = article
        comment.save()
        return redirect('articles:detail', article.pk)
Beispiel #4
0
def create_comment_form(request: HttpRequest,
                        comment_form: CommentForm,
                        article_id: int) -> Comment:
    """
    Create comment form using a article's id
    """
    if comment_form.is_valid():
        new_comment = comment_form.save(commit=False)
        new_comment.article_id = article_id
        new_comment.name = request.user
        return new_comment
Beispiel #5
0
def article_comment_create(request, id):
    article = get_object_or_404(Article, id=id)
    form = CommentForm(request.POST, None)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.article = article
        obj.author = request.user
        obj.save()
        messages.success(request, 'Yorum eklendi.')

    return redirect(reverse('articles:detail', args=[article.slug]))
Beispiel #6
0
 def post(self, request, *args, **kwargs):
     comment_form = CommentForm(request.POST)
     if comment_form.is_valid():
         comment = comment_form.save(commit=False)
         # 按理说这一应该是comment.artilce = article_object的
         # 但是已经获取到了article_id,再去数据库查询一次article显得多此一举
         comment.article_id = kwargs.get('article_id')
         comment.save()
         return redirect('detail', article_id=kwargs.get('article_id'))
     return render(request, 'detail.html', {
         'comment_form': comment_form,
     })
Beispiel #7
0
def submit_comment(request, article_id):
    url = reverse('articles:detail', args=[article_id])
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            article = get_object_or_404(Article, id=article_id)
            comment = form.save(commit=False)
            comment.article = article
            comment.author = request.user.member
            comment.save()
            return redirect(url)
    return redirect(url)
Beispiel #8
0
def add_comment(request, id):
    post = get_object_or_404(Post, id=id)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('post_detail', id=id)
    else:
        form = CommentForm()
    return render(request, 'post/comment_form.html', {'form': form})
Beispiel #9
0
def create_reply_form(request: HttpRequest,
                      comment_form: CommentForm,
                      parent_comment: Comment) -> CommentForm:
    """
    Create reply form using a parent comment
    """
    if comment_form.is_valid():
        new_comment = comment_form.save(commit=False)
        new_comment.article_id = parent_comment.article_id
        new_comment.name = request.user
        new_comment.path.extend(parent_comment.path)
        new_comment.reply_to_id = parent_comment.name_id
        return new_comment
Beispiel #10
0
def article(request, id):
    article = Article.objects.get(id=id)
    form = CommentForm()
    if request.POST:
        data = request.POST.copy()
        data['user'] = request.user.id
        data['article'] = article.id
        data['published'] = datetime.datetime.now()
        form = CommentForm(data)
        if form.is_valid():
            form.save()
            form = CommentForm()

    data = {}
    data['title'] = article.title
    data['article'] = article
    data['form'] = form
    return render(request, 'article.html', data)
Beispiel #11
0
def addcomment(request, a_id):
    article_ = get_object_or_404(Articles, id=a_id)
    if request.method == 'POST':
        print("sprawdzenie")
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.user = request.user
            comment.published = timezone.now()
            comment.article = article_
            comment.save()
            response = article(request, article_id=a_id, comesFromComment=True)
        return response
    else:
        form = CommentForm()
        context = {'form': form, 'user': request.user, 'article_id': a_id}

    return render(request, 'addcomment.html', context)
Beispiel #12
0
def article(request, article_id):
    article = get_object_or_404(Article, pk=article_id)
    comments = article.comment_set.order_by('pk')
    
    if request.method == 'POST':
        form = CommentForm(request.POST)
        
        if form.is_valid():
            comment = form.save(commit = False)
            comment.article = article
            comment.save()
            
            return HttpResponseRedirect(reverse('articles:article', kwargs = {'article_id' : article_id}) + '#comment-' + str(comment.id))
    else:
        form = CommentForm()
        
    return render(request, 'articles/article.html', {
        'article'  : article,
        'comments' : comments,
        'form'     : form
    })
Beispiel #13
0
def article_detail(request,id,slug):
    article = get_object_or_404(ArticlePost,id = id,slug = slug)
    total_views = r.incr("article:{}:views".format(article.id))
    r.zincrby('article_ranking',article.id,1)
    article_ranking = r.zrange('article_ranking',0,-1,desc=True)[:10]
    article_ranking_id = [int(id) for id in article_ranking]
    most_viewed = list(ArticlePost.objects.filter(id__in=article_ranking_id))
    most_viewed.sort(key=lambda x:article_ranking_id.index(x.id))
    # 最新文章的排序
    latest_articles = ArticlePost.objects.order_by("-creat")[:5]
    most_comment_articles = ArticlePost.objects.annotate(total_comment=Count('comments')).order_by("-total_comment")[:5]

    # 执行插入流水操作
    # db = MySQLdb.connect("localhost", "root", "wjl984296155@#$", "laoqidjangobook", charset='utf8')
    # cursor = db.cursor()
    # article_id = id

    # print(request.user.id)
    # sql = "INSERT INTO account_view_count(user_id,article_id) VALUES ('%d','%d' )" % (int(request.user.id),int(article_id))
    # cursor.execute(sql)
    # db.commit()
    if request.method=="POST":
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
                new_comment = comment_form.save(commit=False)
                new_comment.article = article
                new_comment.save()
    else:
        comment_form = CommentForm()
    article_tags_ids = article.article_tag.values_list("id", flat=True)
    similar_articles = ArticlePost.objects.filter(article_tag__in=article_tags_ids).exclude(id=article.id)
    similar_articles = similar_articles.annotate(same_tags=Count("article_tag")).order_by('-same_tags', '-creat')[:4]
    # 传入userid
    top_five_article_id = data_handle.recommend(request.user.id)
    # 输出recommend,article_id   list
    top_five_articles = []
    for i in range(len(top_five_article_id)):
        top_five_article = get_object_or_404(ArticlePost,id = top_five_article_id[i])
        top_five_articles.append(top_five_article)
    return render(request,'article/list/article_detail.html',{'article':article,'total_views':total_views,'most_viewed':most_viewed,'comment_form':comment_form,'latest_articles':latest_articles,'most_comment_articles':most_comment_articles,"similar_articles":similar_articles,"top_five_articles":top_five_articles})
Beispiel #14
0
def add_comment(request, article_id):
    art = Article.objects.get(id=article_id)

    if request.method == "POST":
        cf = CommentForm(request.POST)
        if cf.is_valid():
            comment = cf.save(commit=False)
            comment.published = timezone.now()
            comment.article = art
            comment.save()

            return HttpResponseRedirect('/article/%s' % article_id)

    else:
        cf = CommentForm()

    args = {}
    args.update(csrf(request))
    args['article'] = art
    args['form'] = cf

    return render(request, 'add_comment.html', args)
Beispiel #15
0
def detail(request, slug=None, pk=None):
    if slug:
        a = get_object_or_404(Article, slug__exact=slug)
    elif pk:
        a = get_object_or_404(Article, pk=pk)
    else:
        raise Http404

    liked = False
    if request.user.is_authenticated():
        liked = (a.like_set.all() and a.like_set.filter(user=request.user))

    if request.method == 'POST':
        if not request.user.is_authenticated():
            return HttpResponseRedirect(reverse(login))
        form = CommentForm(request.POST)
        if form.is_valid():
            c = Comment(article=a, user=request.user, text=form.cleaned_data['comment_text'])
            c.save()
            return HttpResponseRedirect(a.get_absolute_url())
    else:
        form = CommentForm()
    return render_to_response('articles/detail.html', {'article': a, 'comment_form': form, 'liked': liked }, context_instance = RequestContext(request))
Beispiel #16
0
def article_detail(request,pk,slug):
    article=get_object_or_404(Article,id=pk,slug=slug)

    comments=Comment.objects.filter(article=article,reply=None).order_by('-timestamp')

    is_liked=False
    if article.liked.filter(id=request.user.id).exists():
        is_liked=True

    is_favourite=False
    if article.favourite.filter(id=request.user.id).exists():
        is_favourite=True

    if request.method=="POST":
        comment_form=CommentForm(request.POST or None)

        if comment_form.is_valid():
            comment=request.POST.get('comment')
            reply_id=request.POST.get('object_id')
            comment_qs=None

            if reply_id:
                comment_qs=Comment.objects.get(id=reply_id)

            obj=Comment.objects.create(article=article,user=request.user,comment=comment,reply=comment_qs)
            obj.save()
            return redirect(article.get_absolute_url())
    else:
        comment_form=CommentForm()

    context={'object':article,
             'is_liked':is_liked,
             'is_favourite':is_favourite,
             'comments':comments,
             'form':comment_form,}
    return render(request,'articles/article_detail.html',context)
Beispiel #17
0
 def test_comment_form_valid_data(self):
     form = CommentForm(data={'content': 'Yo wassup'})
     self.assertTrue(form.is_valid())
Beispiel #18
0
 def test_comment_form_no_data(self):
     form = CommentForm(data={})
     self.assertFalse(form.is_valid())
     self.assertEquals(len(form.errors), 1)