views.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from django.shortcuts import render, redirect
  2. from django.views import View
  3. from django.views.generic import ListView
  4. from django.contrib.auth.mixins import LoginRequiredMixin
  5. from article.models import ArticlePost
  6. class CommentNoticeListView(LoginRequiredMixin, ListView):
  7. # 通知列表
  8. # 上下文的名称
  9. context_object_name = 'notices'
  10. # 模板位置
  11. template_name = 'notice/list.html'
  12. # 登录重定向
  13. login_url = '/user/login/'
  14. # 未读通知的查询集
  15. def get_queryset(self):
  16. return self.request.user.notifications.unread()
  17. class CommentNoticeUpdateView(View):
  18. # 更新通知状态
  19. # 处理get请求
  20. def get(self, request):
  21. # 获取未读消息
  22. notice_id = request.GET.get('notice_id')
  23. # 更新单条通知
  24. if notice_id:
  25. article = ArticlePost.objects.get(id=request.GET.get('article_id'))
  26. request.user.notifications.get(id=notice_id).mark_as_read()
  27. return redirect(article)
  28. else:
  29. request.user.notifications.mark_all_as_read()
  30. return redirect('notice:list')