Skip to content

Instantly share code, notes, and snippets.

@hemanth-sp
Created February 26, 2020 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hemanth-sp/7fc49c78e3022fb265d30255037644ce to your computer and use it in GitHub Desktop.
Save hemanth-sp/7fc49c78e3022fb265d30255037644ce to your computer and use it in GitHub Desktop.
views.py
from django.contrib.auth.models import User
from rest_framework import generics
from rest_framework.permissions import IsAdminUser, AllowAny
from .serializers import *
from .models import *
from rest_framework.renderers import JSONRenderer
from rest_framework.utils import encoders, json
"""
Helper classes for parsers.
"""
import datetime
import decimal
import json # noqa
import uuid
from django.db.models.query import QuerySet
from django.utils import timezone
from django.utils.encoding import force_str
from django.utils.functional import Promise
from rest_framework.compat import coreapi
class JSONEncoder(json.JSONEncoder):
"""
JSONEncoder subclass that knows how to encode date/time/timedelta,
decimal types, generators and other basic python objects.
"""
def default(self, obj):
# For Date Time string spec, see ECMA 262
# https://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
if isinstance(obj, Promise):
return force_str(obj)
elif isinstance(obj, datetime.datetime):
representation = obj.isoformat()
if representation.endswith('+00:00'):
representation = representation[:-6] + 'Z'
return representation
elif isinstance(obj, datetime.date):
return obj.isoformat()
elif isinstance(obj, datetime.time):
if timezone and timezone.is_aware(obj):
# raise ValueError("JSON can't represent timezone-aware times.")
pass
representation = obj.isoformat()
return representation
elif isinstance(obj, datetime.timedelta):
return str(obj.total_seconds())
elif isinstance(obj, decimal.Decimal):
# Serializers will coerce decimals to strings by default.
return float(obj)
elif isinstance(obj, uuid.UUID):
return str(obj)
elif isinstance(obj, QuerySet):
return tuple(obj)
elif isinstance(obj, bytes):
# Best-effort for binary blobs. See #4187.
return obj.decode()
elif hasattr(obj, 'tolist'):
# Numpy arrays and array scalars.
return obj.tolist()
elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)):
raise RuntimeError(
'Cannot return a coreapi object from a JSON view. '
'You should be using a schema renderer instead for this view.'
)
elif hasattr(obj, '__getitem__'):
cls = (list if isinstance(obj, (list, tuple)) else dict)
try:
return cls(obj)
except Exception:
pass
elif hasattr(obj, '__iter__'):
return tuple(item for item in obj)
return super().default(obj)
class JSONRenderer1(JSONRenderer):
"""
Renderer which serializes to JSON.
"""
encoder_class = JSONEncoder
class HemDetail(generics.ListAPIView):
renderer_classes = [JSONRenderer1]
queryset = Hem.objects.all()
serializer_class = HemSerializer
permission_classes = [AllowAny]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment