-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameon_utils.py
36 lines (30 loc) · 1.03 KB
/
gameon_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import json
import datetime
from time import mktime
from models.models import BaseModel
from google.appengine.ext import ndb
import pickle
class GameOnUtils(object):
debug = os.environ.get('SERVER_SOFTWARE', '').startswith('Development/')
@classmethod
def json_serializer(cls, obj):
"""Default JSON serializer."""
import calendar, datetime
if isinstance(obj, datetime.datetime):
if obj.utcoffset() is not None:
obj = obj - obj.utcoffset()
millis = int(
calendar.timegm(obj.timetuple()) * 1000 +
obj.microsecond / 1000
)
return millis
class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return int(mktime(obj.timetuple()))
if isinstance(obj, BaseModel):
obj.key = None
obj.id = None
return obj.to_dict()
return obj.__dict__ #json.JSONEncoder.default(self, obj.__dict__)