<commit_before>import base64

from django.db.models import Q
from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404

from rest_framework.exceptions import NotFound
from rest_framework.decorators import detail_route, list_route
from rest_framework.viewsets import ReadOnlyModelViewSet, ViewSet
from rest_framework.response import Response

from api_v2.serializers.rest import (
    IssuerSerializer,
    SchemaSerializer,
    CredentialTypeSerializer,
    TopicSerializer,
    CredentialSerializer,
    ExpandedCredentialSerializer,
    ExpandedCredentialSetSerializer,
    AddressSerializer,
    AttributeSerializer,
    NameSerializer,
    CredentialTopicExtSerializer,
)

from rest_framework.serializers import SerializerMethodField

from drf_yasg.utils import swagger_auto_schema

from api_v2.serializers.search import CustomTopicSerializer

from api_v2.models.Issuer import Issuer
from api_v2.models.Schema import Schema
from api_v2.models.CredentialType import CredentialType
from api_v2.models.Topic import Topic
from api_v2.models.Credential import Credential
from api_v2.models.Address import Address
from api_v2.models.Attribute import Attribute
from api_v2.models.Name import Name

from api_v2 import utils


class IssuerViewSet(ReadOnlyModelViewSet):
    serializer_class = IssuerSerializer
    queryset = Issuer.objects.all()

    @swagger_auto_schema(method='get')
    @detail_route(url_path="credentialtype", methods=["get"])
    def list_credential_types(self, request, pk=None):
        queryset = CredentialType.objects.filter(issuer__id=pk)
        get_object_or_404(queryset, pk=pk)
        serializer = CredentialTypeSerializer(queryset, many=True)
        return Response(serializer.data)

    @swagger_auto_schema(method='get')
    @detail_route(url_path="logo", methods=["get"])
    def fetch_logo(self, request, pk=None):
        issuer = get_object_or_404(self.queryset, pk=pk)
        logo = None
        if issuer.logo_b64:
            logo = base64.b64decode(issuer.logo_b64)
        if not logo:
            raise Http404()
        # FIXME - need to store the logo mime type
        return HttpResponse(logo, content_type="image/jpg")
<commit_msg>fix issuer credential types query<commit_after>import base64

from django.db.models import Q
from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404

from rest_framework.exceptions import NotFound
from rest_framework.decorators import detail_route, list_route
from rest_framework.viewsets import ReadOnlyModelViewSet, ViewSet
from rest_framework.response import Response

from api_v2.serializers.rest import (
    IssuerSerializer,
    SchemaSerializer,
    CredentialTypeSerializer,
    TopicSerializer,
    CredentialSerializer,
    ExpandedCredentialSerializer,
    ExpandedCredentialSetSerializer,
    AddressSerializer,
    AttributeSerializer,
    NameSerializer,
    CredentialTopicExtSerializer,
)

from rest_framework.serializers import SerializerMethodField

from drf_yasg.utils import swagger_auto_schema

from api_v2.serializers.search import CustomTopicSerializer

from api_v2.models.Issuer import Issuer
from api_v2.models.Schema import Schema
from api_v2.models.CredentialType import CredentialType
from api_v2.models.Topic import Topic
from api_v2.models.Credential import Credential
from api_v2.models.Address import Address
from api_v2.models.Attribute import Attribute
from api_v2.models.Name import Name

from api_v2 import utils


class IssuerViewSet(ReadOnlyModelViewSet):
    serializer_class = IssuerSerializer
    queryset = Issuer.objects.all()

    @swagger_auto_schema(method='get')
    @detail_route(url_path="credentialtype", methods=["get"])
    def list_credential_types(self, request, pk=None):
        item = self.get_object()
        queryset = item.credential_types
        serializer = CredentialTypeSerializer(queryset, many=True)
        return Response(serializer.data)

    @swagger_auto_schema(method='get')
    @detail_route(url_path="logo", methods=["get"])
    def fetch_logo(self, request, pk=None):
        issuer = get_object_or_404(self.queryset, pk=pk)
        logo = None
        if issuer.logo_b64:
            logo = base64.b64decode(issuer.logo_b64)
        if not logo:
            raise Http404()
        # FIXME - need to store the logo mime type
        return HttpResponse(logo, content_type="image/jpg")

{'fraction_non_alphanumeric': 0.06704416761041902, 'fraction_numerical': 0.015628539071347678, 'mean_word_length': 4.575757575757576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3717894', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1293, 'n_words': 302}