from rest_framework import generics
from rest_framework.exceptions import PermissionDenied, NotAuthenticated

from rules import test_rule

from main.models import Problem
from main.serializers import ProblemSerializer


class ProblemList(generics.ListAPIView):
    serializer_class = ProblemSerializer

    def get_queryset(self):
        user = self.request.user
        if test_rule('can_access_unreleased_problems', user):
            return Problem.objects.all()
        else:
            return Problem.objects.filter(released=True)


class ProblemDetail(generics.RetrieveAPIView):
    queryset = Problem.objects.all()
    serializer_class = ProblemSerializer

    def check_object_permissions(self, request, problem):
        if problem.released:
            return
        elif not request.user.is_authenticated():  # 用户未登录
            raise NotAuthenticated
        elif not test_rule('can_access_unreleased_problems', request.user):  # 用户无权限
            raise PermissionDenied

@@ -87,26 +87,8 @@
 nied
-, NotAuthenticated
 %0A%0Afr
@@ -714,152 +714,8 @@
  if 
-problem.released:%0A            return%0A        elif not request.user.is_authenticated():  # %E7%94%A8%E6%88%B7%E6%9C%AA%E7%99%BB%E5%BD%95%0A            raise NotAuthenticated%0A        elif 
 not 
@@ -736,35 +736,23 @@
 _access_
-unreleased_
 problem
-s
 ', reque
@@ -762,19 +762,19 @@
 user
-):  # %E7%94%A8%E6%88%B7%E6%97%A0%E6%9D%83%E9%99%90
+, problem):
 %0A   

Improve code.