Source code for nkdsu.apps.vote.views.api

import json
from abc import ABC, abstractmethod
from typing import TypeVar

from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpRequest, HttpResponse
from django.views.generic import View
from django.views.generic.detail import SingleObjectMixin

from ..api_utils import JsonDict, JsonEncodable, JsonList, Serializable
from ..mixins import ShowDetailMixin, ThisShowDetailMixin, TwitterUserDetailMixin
from ..models import Track
from ..views import Search


S = TypeVar("S", bound=Serializable)


[docs] class APIView(View, ABC):
[docs] @abstractmethod def get_api_stuff(self) -> JsonEncodable: raise NotImplementedError()
[docs] def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: resp = HttpResponse( json.dumps(self.get_api_stuff(), cls=DjangoJSONEncoder, indent=2), content_type='application/json', ) resp['Access-Control-Allow-Origin'] = '*' return resp
[docs] class DetailAPIView(APIView, SingleObjectMixin[S]):
[docs] def get_api_stuff(self) -> JsonDict: return self.get_object().api_dict(verbose=True)
[docs] class ShowAPI(ThisShowDetailMixin, DetailAPIView): pass
[docs] class PrevShowAPI(ShowDetailMixin, DetailAPIView): view_name = 'vote:api:show'
[docs] class TrackAPI(DetailAPIView): model = Track
[docs] class SearchAPI(APIView, Search):
[docs] def get_api_stuff(self, *a, **k) -> JsonList: return [t.api_dict() for t in self.get_queryset()]
[docs] class TwitterUserAPI(TwitterUserDetailMixin, DetailAPIView): pass