Beispiel #1
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 #2
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 #3
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 #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 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 #11
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 #12
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 #13
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)