예제 #1
0
파일: rest.py 프로젝트: weiiv/TheOrgBook
class CredentialViewSet(ReadOnlyModelViewSet):
    serializer_class = CredentialSerializer
    queryset = Credential.objects.all()

    @detail_route(url_path="formatted", methods=["get"])
    def retrieve_formatted(self, request, pk=None):
        item = self.get_object()
        serializer = ExpandedCredentialSerializer(item)
        return Response(serializer.data)

    @swagger_auto_schema(responses={200: CredentialSerializer(many=True)})
    @list_route(url_path="active", methods=["get"])
    def list_active(self, request, pk=None):
        queryset = self.queryset.filter(revoked=False,
                                        inactive=False,
                                        latest=True)
        serializer = CredentialSerializer(queryset, many=True)
        return Response(serializer.data)

    @swagger_auto_schema(responses={200: CredentialSerializer(many=True)})
    @list_route(url_path="historical", methods=["get"])
    def list_historical(self, request, pk=None):
        queryset = self.queryset.filter(Q(revoked=True) | Q(inactive=True))
        serializer = CredentialSerializer(queryset, many=True)
        return Response(serializer.data)

    @detail_route(url_path="latest", methods=["get"])
    def get_latest(self, request, pk=None):
        item = self.get_object()
        latest = None
        if item.credential_set:
            latest = item.credential_set.latest_credential
        if not latest:
            latest = item
        serializer = CredentialSerializer(latest)
        return Response(serializer.data)

    def get_object(self):
        pk = self.kwargs.get("pk")
        if not pk:
            raise Http404()
        filter = {"wallet_id": pk}
        try:
            filter = {"pk": int(pk)}
        except (ValueError, TypeError):
            pass

        queryset = self.filter_queryset(self.get_queryset())
        obj = get_object_or_404(queryset, **filter)

        # May raise a permission denied
        self.check_object_permissions(self.request, obj)
        return obj
예제 #2
0
 def get_latest(self, request, pk=None):
     item = self.get_object()
     latest = None
     if item.credential_set:
         latest = item.credential_set.latest_credential
     if not latest:
         latest = item
     serializer = CredentialSerializer(latest)
     return Response(serializer.data)
예제 #3
0
 def retrieve(self, request, pk=None):
     queryset = Credential.objects.all()
     try:
         # Overload to allow querying by wallet_id
         item = Credential.objects.get(wallet_id=pk)
     except Credential.DoesNotExist:
         try:
             item = get_object_or_404(queryset, pk=pk)
         except ValueError:
             raise NotFound()
     serializer = CredentialSerializer(item)
     return Response(serializer.data)
예제 #4
0
 def list_historical(self, request, pk=None):
     queryset = Credential.objects.filter(~Q(revoked=False))
     serializer = CredentialSerializer(queryset, many=True)
     return Response(serializer.data)
예제 #5
0
 def list(self, request):
     queryset = Credential.objects.all()
     serializer = CredentialSerializer(queryset, many=True)
     return Response(serializer.data)
예제 #6
0
 def list_historical(self, request, pk=None):
     queryset = self.queryset.filter(Q(revoked=True) | Q(inactive=True))
     serializer = CredentialSerializer(queryset, many=True)
     return Response(serializer.data)
예제 #7
0
 def list_active(self, request, pk=None):
     queryset = self.queryset.filter(revoked=False,
                                     inactive=False,
                                     latest=True)
     serializer = CredentialSerializer(queryset, many=True)
     return Response(serializer.data)