Exemple #1
0
 def test_notification(self):
     """单个通知"""
     Notification.objects.mark_all_as_read()
     obj = News.objects.create(user=self.user, content="内容示例", reply=True)
     notification_handler(self.other_user, self.user, "C",
                          obj)  # other_user评论了user
     assert Notification.objects.unread().count() == 1
Exemple #2
0
 def switch_like(self, user):
     """点赞或取消点赞"""
     # 如点赞过,则取消
     if user in self.liked.all():
         self.liked.remove(user)
     else:
         self.liked.add(user)
         # 通知楼主
         notification_handler(user, self.user, 'L', self, key="social_update", id_value=str(self.uuid))
Exemple #3
0
 def reply_this(self, user, text):
     """
     回复首页的动态
     :param user: 登录的用户
     :param text: 回复的内容
     :return: None
     """
     parent = self.get_parent()
     News.objects.create(
         user=user,
         content=text,
         reply=True,
         parent=parent
     )
     notification_handler(user, parent.user, 'R', parent, id_value=str(parent.uuid), key="social_update")
Exemple #4
0
def accept_answer(request):
    """
    认可回答 AJAX POST请求
    已经被接受的回答不能被取消
    """
    answer_id = request.POST.get('answer')
    answer = Answer.objects.get(pk=answer_id)
    # 如果当前登录用户不是提问者,抛出权限拒绝错误,前端也进行过一次检验,可能没有必要做这一步
    if answer.question.user.username != request.user.username:
        raise PermissionDenied
    answer.accept_answer()

    # 发送通知给回答者
    notification_handler(request.user, answer.user, 'W', answer)

    return JsonResponse({"status": "true"})
Exemple #5
0
def post_comment(request, article_id, parent_comment_id=None):
    """发表评论"""
    article = get_object_or_404(Article, id=article_id)

    if request.method == 'POST':
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.article = article
            new_comment.user = request.user

            # 二级及以下评论
            if parent_comment_id:
                parent_comment = Comment.objects.get(id=parent_comment_id)
                # 若回复层数超过二级,则转化为二级
                new_comment.parent_id = parent_comment.get_root().id
                new_comment.reply_to = parent_comment.user
                new_comment.save()
                # 二级及以下评论的消息通知
                notification_handler(request.user, parent_comment.user, 'R', parent_comment)
                return JsonResponse({'code': '200 OK'})

            # 保存一级评论
            new_comment.save()
            # 一级评论的消息通知
            notification_handler(request.user, article.user, 'C', article)
            return redirect('articles:article', slug=article.slug)
        else:
            return HttpResponse('表单内容有误,请重新填写!')
    # GET请求用于生成回复栏
    elif request.method == 'GET':
        comment_form = CommentForm()
        context = {
            'comment_form': comment_form,
            'article_id': article_id,
            'parent_comment_id': parent_comment_id
        }
        return render(request, 'articles/reply_comment.html', context)
    else:
        return HttpResponse("仅接受GET/POST请求")
Exemple #6
0
 def save(self, force_insert=False, force_update=False, using=None,
          update_fields=None):
     super().save()
     from yunjian.notifications.views import notification_handler
     notification_handler(self.user, self.question.user, "A", self.question)