def list_historical_credentials(self, request, pk=None): item = self.get_object() queryset = item.credentials.filter(Q(revoked=True) | Q(inactive=True)) item_count = queryset.count() serializer = ExpandedCredentialSerializer(queryset, many=True) response = Response(serializer.data) response["item_count"] = item_count return response
def list_credentials(self, request, pk=None): item = self.get_object() queryset = item.credentials item_count = queryset.count() serializer = ExpandedCredentialSerializer(queryset, many=True) response = Response(serializer.data) response["item_count"] = item_count return response
def retrieve_formatted(self, request, pk=None): item = self.get_object() serializer = ExpandedCredentialSerializer(item) return Response(serializer.data)
def list_active_credentials(self, request, pk=None): item = self.get_object() queryset = item.credentials.filter(revoked=False, inactive=False) serializer = ExpandedCredentialSerializer(queryset, many=True) return Response(serializer.data)
def list_credentials(self, request, pk=None): item = self.get_object() queryset = item.credentials serializer = ExpandedCredentialSerializer(queryset, many=True) return Response(serializer.data)
class TopicViewSet(ReadOnlyModelViewSet): serializer_class = TopicSerializer queryset = Topic.objects.all() @action(detail=True, url_path="formatted", methods=["get"]) def retrieve_formatted(self, request, pk=None): item = self.get_object() serializer = CustomTopicSerializer(item) return Response(serializer.data) @swagger_auto_schema( responses={200: ExpandedCredentialSerializer(many=True)}) @action(detail=True, url_path="credential", methods=["get"]) def list_credentials(self, request, pk=None): item = self.get_object() queryset = item.credentials serializer = ExpandedCredentialSerializer(queryset, many=True) return Response(serializer.data) @swagger_auto_schema( responses={200: ExpandedCredentialSerializer(many=True)}) @action(detail=True, url_path="credential/active", methods=["get"]) def list_active_credentials(self, request, pk=None): item = self.get_object() queryset = item.credentials.filter(revoked=False, inactive=False) serializer = ExpandedCredentialSerializer(queryset, many=True) return Response(serializer.data) @swagger_auto_schema( responses={200: ExpandedCredentialSerializer(many=True)}) @action(detail=True, url_path="credential/historical", methods=["get"]) def list_historical_credentials(self, request, pk=None): item = self.get_object() queryset = item.credentials.filter(Q(revoked=True) | Q(inactive=True)) serializer = ExpandedCredentialSerializer(queryset, many=True) return Response(serializer.data) @swagger_auto_schema(responses={200: TopicSerializer(many=False)}) @action( detail=False, methods=["get"], url_path="ident/(?P<type>[^/]+)/(?P<source_id>[^/.]+)", ) def retrieve_by_type(self, request, type=None, source_id=None): return self.retrieve(request) @swagger_auto_schema(responses={200: CustomTopicSerializer(many=False)}) @action( detail=False, methods=["get"], url_path="ident/(?P<type>[^/]+)/(?P<source_id>[^/.]+)/formatted", ) def retrieve_by_type_formatted(self, request, type=None, source_id=None): return self.retrieve_formatted(request) @swagger_auto_schema( responses={200: ExpandedCredentialSetSerializer(many=True)}) @action(detail=True, url_path="credentialset", methods=["get"]) def list_credential_sets(self, request, pk=None): item = self.get_object() credential_sets = ( item.credential_sets # .select_related("credential_type", "topic") .prefetch_related( # "credentials__addresses", "credentials__related_topics", "credentials__credential_type", "credentials__topic", ).order_by("first_effective_date").all()) def get_credential_local_name(credential): if not credential.get_local_name(): return get_topic_local_name(credential.topic) else: return { "id": credential.get_local_name().id, "text": credential.get_local_name().text or None, "language": credential.get_local_name().language or None, "credential_id": credential.get_local_name().credential_id or None, "type": credential.get_local_name().type or None, } def get_credential_remote_name(credential): if not credential.get_remote_name(): return get_topic_remote_name(credential.topic) else: return { "id": credential.get_remote_name().id, "text": credential.get_remote_name().text or None, "language": credential.get_remote_name().language or None, "credential_id": credential.get_remote_name().credential_id or None, "type": credential.get_remote_name().type or None, } def get_topic_local_name(topic): if not topic.get_local_name(): return {} else: return { "id": topic.get_local_name().id, "text": topic.get_local_name().text or None, "language": topic.get_local_name().language or None, "credential_id": topic.get_local_name().credential_id or None, "type": topic.get_local_name().type or None, } def get_topic_remote_name(topic): if not topic.get_remote_name(): return {} else: return { "id": topic.get_remote_name().id, "text": topic.get_remote_name().text or None, "language": topic.get_remote_name().language or None, "credential_id": topic.get_remote_name().credential_id or None, "type": topic.get_remote_name().type or None, } data = [{ "id": credential_set.id, "create_timestamp": credential_set.create_timestamp.isoformat() if credential_set.create_timestamp is not None else None, "update_timestamp": credential_set.update_timestamp.isoformat() if credential_set.update_timestamp is not None else None, "latest_credential_id": credential_set.latest_credential_id, "topic_id": credential_set.topic_id, "first_effective_date": credential_set.first_effective_date.isoformat() if credential_set.first_effective_date is not None else None, "last_effective_date": credential_set.last_effective_date.isoformat() if credential_set.last_effective_date is not None else None, "credentials": [{ "id": credential.id, "create_timestamp": credential.create_timestamp.isoformat() if credential.create_timestamp else None, "effective_date": credential.effective_date.isoformat() if credential.effective_date else None, "inactive": credential.inactive, "latest": credential.latest, "revoked": credential.revoked, "revoked_date": credential.revoked_date.isoformat() if credential.revoked_date else None, "credential_id": credential.credential_id, "names": [{ "id": name.id, "text": name.text or None, "language": name.language or None, "credential_id": name.credential_id, "type": name.type, } if name else {} for name in credential.all_names], "local_name": get_credential_local_name(credential), "remote_name": get_credential_remote_name(credential), "topic": { "id": credential.topic.id, "source_id": credential.topic.source_id, "type": credential.topic.type, "local_name": get_topic_local_name(credential.topic), "remote_name": get_topic_remote_name(credential.topic) }, "related_topics": [ { "id": related_topic.id, "source_id": related_topic.source_id, "type": related_topic.type, "names": [{ "id": name.id, "text": name.text or None, "language": name.language or None, "credential_id": name.credential_id, "type": name.type, } if name else {} for name in related_topic.get_active_names() ], "local_name": get_topic_local_name(related_topic), "remote_name": get_topic_remote_name(related_topic) } if related_topic else {} for related_topic in credential.related_topics.all() ], "credential_type": { "id": credential.credential_type.id, "description": credential.credential_type.description, }, } for credential in credential_set.credentials.all()], } for credential_set in credential_sets] return Response(data) def get_object(self): if self.kwargs.get("pk"): return super(TopicViewSet, self).get_object() type = self.kwargs.get("type") source_id = self.kwargs.get("source_id") if not type or not source_id: raise Http404() # map type to a schema name, if an "old style" type is used if settings.CRED_TYPE_SYNONYMS and type.lower( ) in settings.CRED_TYPE_SYNONYMS: type = settings.CRED_TYPE_SYNONYMS[type.lower()] queryset = self.filter_queryset(self.get_queryset()) obj = get_object_or_404(queryset, type=type, source_id=source_id) # May raise a permission denied self.check_object_permissions(self.request, obj) return obj