import datetime
import decimal
import logging

import elasticsearch

from nefertari.renderers import _JSONEncoder


log = logging.getLogger(__name__)


class JSONEncoder(_JSONEncoder):
    def default(self, obj):
        if (isinstance(obj, datetime.date) and
                not isinstance(obj, datetime.datetime)):
            return obj.strftime('%Y-%m-%d')
        if isinstance(obj, datetime.timedelta):
            return obj.seconds
        if isinstance(obj, decimal.Decimal):
            return float(obj)

        if hasattr(obj, 'to_dict'):
            # If it got to this point, it means its a nested object.
            # Outter objects would have been handled with DataProxy.
            return obj.to_dict(__nested=True)

        return super(JSONEncoder, self).default(obj)


class ESJSONSerializer(elasticsearch.serializer.JSONSerializer):
    def default(self, data):
        if (isinstance(data, datetime.date) and
                not isinstance(data, datetime.datetime)):
            return data.strftime('%Y-%m-%d')
        if isinstance(data, datetime.time):
            return data.strftime('%H:%M:%S')
        if isinstance(data, datetime.timedelta):
            return data.seconds
        if isinstance(data, decimal.Decimal):
            return float(data)
        try:
            return super(ESJSONSerializer, self).default(data)
        except:
            import traceback
            log.error(traceback.format_exc())

@@ -209,33 +209,32 @@
 bj):%0A        if 
-(
 isinstance(obj, 
@@ -225,32 +225,33 @@
 isinstance(obj, 
+(
 datetime.date) a
@@ -251,33 +251,102 @@
 date
-) and%0A                not
+time, datetime.date)):%0A            return obj.strftime(%22%25Y-%25m-%25dT%25H:%25M:%25SZ%22)  # iso%0A        if
  isi
@@ -363,34 +363,29 @@
 j, datetime.
-date
 time)
-)
 :%0A          
@@ -400,39 +400,39 @@
  obj.strftime('%25
-Y-%25m-%25d
+H:%25M:%25S
 ')%0A        if is
@@ -936,20 +936,19 @@
 t(self, 
-data
+obj
 ):%0A     
@@ -953,17 +953,16 @@
      if 
-(
 isinstan
@@ -956,38 +956,38 @@
   if isinstance(
-data
+obj
 , 
+(
 datetime.date) a
@@ -987,49 +987,12 @@
 date
-) and%0A                not isinstance(data
+time
 , da
@@ -1002,20 +1002,16 @@
 ime.date
-time
 )):%0A    
@@ -1017,36 +1017,35 @@
          return 
-data
+obj
 .strftime('%25Y-%25m
@@ -1042,17 +1042,17 @@
 ime(
-'
+%22
 %25Y-%25m-%25d
 ')%0A 
@@ -1047,18 +1047,35 @@
 %25Y-%25m-%25d
-')
+T%25H:%25M:%25SZ%22)  # iso
 %0A       
@@ -1081,36 +1081,35 @@
   if isinstance(
-data
+obj
 , datetime.time)
@@ -1129,20 +1129,19 @@
  return 
-data
+obj
 .strftim
@@ -1176,20 +1176,19 @@
 nstance(
-data
+obj
 , dateti
@@ -1221,20 +1221,19 @@
  return 
-data
+obj
 .seconds
@@ -1255,20 +1255,19 @@
 nstance(
-data
+obj
 , decima
@@ -1303,20 +1303,19 @@
 n float(
-data
+obj
 )%0A      
@@ -1382,12 +1382,11 @@
 ult(
-data
+obj
 )%0A  

Encode date, datetime as ISO. Timedelta ni seconds