import json

from django.utils.cache import patch_response_headers
from functools import wraps


def long_cache(a_view):
    @wraps(a_view)
    def _wrapped_view(request, *args, **kwargs):
        response = a_view(request, *args, **kwargs)
        patch_response_headers(response, 86400 * 365)
        return response
    return _wrapped_view


def to_json(what):
    return json.dumps(what, indent=1)

import json

from django.utils.cache import patch_response_headers
from functools import wraps
from uuid import UUID


class JsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, UUID):
            return '{}'.format(obj)

        if hasattr(obj, 'serialize'):
            return obj.serialize()

        return json.JSONEncoder.default(self, obj)


def long_cache(a_view):
    @wraps(a_view)
    def _wrapped_view(request, *args, **kwargs):
        response = a_view(request, *args, **kwargs)
        patch_response_headers(response, 86400 * 365)
        return response
    return _wrapped_view


def to_json(what):
    return json.dumps(what, indent=1, cls=JsonEncoder)

Extend JSON serialiser to use serialize() method
Extend JSON serialiser to use serialize() method

If an object is a UUID, return a string representation of it.

If the object still can't be serialised, call its serialize() method.
This is useful when nesting Link models inside dashboards, for
example.

  import json
  
  from django.utils.cache import patch_response_headers
  from functools import wraps
+ from uuid import UUID
+ 
+ 
+ class JsonEncoder(json.JSONEncoder):
+     def default(self, obj):
+         if isinstance(obj, UUID):
+             return '{}'.format(obj)
+ 
+         if hasattr(obj, 'serialize'):
+             return obj.serialize()
+ 
+         return json.JSONEncoder.default(self, obj)
  
  
  def long_cache(a_view):
      @wraps(a_view)
      def _wrapped_view(request, *args, **kwargs):
          response = a_view(request, *args, **kwargs)
          patch_response_headers(response, 86400 * 365)
          return response
      return _wrapped_view
  
  
  def to_json(what):
-     return json.dumps(what, indent=1)
+     return json.dumps(what, indent=1, cls=JsonEncoder)
  
Extend JSON serialiser to use serialize() method
## Code Before:
import json

from django.utils.cache import patch_response_headers
from functools import wraps


def long_cache(a_view):
    @wraps(a_view)
    def _wrapped_view(request, *args, **kwargs):
        response = a_view(request, *args, **kwargs)
        patch_response_headers(response, 86400 * 365)
        return response
    return _wrapped_view


def to_json(what):
    return json.dumps(what, indent=1)

## Instruction:
Extend JSON serialiser to use serialize() method
## Code After:
import json

from django.utils.cache import patch_response_headers
from functools import wraps
from uuid import UUID


class JsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, UUID):
            return '{}'.format(obj)

        if hasattr(obj, 'serialize'):
            return obj.serialize()

        return json.JSONEncoder.default(self, obj)


def long_cache(a_view):
    @wraps(a_view)
    def _wrapped_view(request, *args, **kwargs):
        response = a_view(request, *args, **kwargs)
        patch_response_headers(response, 86400 * 365)
        return response
    return _wrapped_view


def to_json(what):
    return json.dumps(what, indent=1, cls=JsonEncoder)
