# CCS CS Lab Main JSON # # Douglas Thrift # # $Id$ from ccscslab.main.models import Request from datetime import datetime from django.db.models.query import QuerySet import pytz try: import simplejson as json except ImportError: import json class RequestJSONEncoder(json.JSONEncoder): def default(self, object): if isinstance(object, QuerySet): return tuple(object) elif isinstance(object, Request): return { 'n': object.name, 'f': object.full_name, 't': object.temporary_password, 'r': object.requested.strftime('%Y-%m-%dT%H:%M:%S.%fZ'), 'i': object.ip_address, 'd': object.dns_name, 'e': object.email, } else: return super(RequestJSONEncoder, self).default(object) def request_object_hook(object): if 'n' in object and 'f' in object and 't' in object and 'r' in object and 'i' in object and 'e' in object: return Request.objects.get_or_create(name = object['n'], defaults = {'full_name': object['f'], 'temporary_password': object['t'], 'requested': pytz.UTC.localize(datetime.strptime(object['r'], '%Y-%m-%dT%H:%M:%S.%fZ')), 'ip_address': object['i'], 'dns_name': object.get('d', ''), 'email': object['e']})[0] return object