1 from manifold.core.query import Query
2 from manifoldapi.manifoldapi import execute_query
4 from django.http import HttpResponse
11 # handles serialization of datetime in json
12 DateEncoder = lambda obj: obj.strftime("%B %d, %Y %H:%M:%S") if isinstance(obj, datetime.datetime) else None
14 # support converting decimal in json
15 json.encoder.FLOAT_REPR = lambda o: format(o, '.2f')
17 # handles decimal numbers serialization in json
18 class DecimalEncoder(json.JSONEncoder):
19 def _iterencode(self, o, markers=None):
20 if isinstance(o, decimal.Decimal):
21 return (str(o) for o in [o])
22 return super(DecimalEncoder, self)._iterencode(o, markers)
24 class ObjectRequest(object):
26 def __init__(self, request, object_type, object_name):
27 self.type = object_type
28 self.name = object_name
33 self.request = request
35 if ((self.type == 'platform') or (self.type == 'testbed')) :
36 self.type = 'local:platform'
38 self.fields = ['platform', 'platform_longname', 'platform_url', 'platform_description','gateway_type'];
39 self.filters['disabled'] = '0'
40 self.filters['gateway_type'] = 'sfa'
41 self.filters['platform'] = '!myslice'
48 # What about key formed of multiple fields???
49 query = Query.get('local:object').filter_by('table', '==', self.type).select('key')
50 results = execute_query(self.request, query)
51 print "key of object = %s" % results
53 for r in results[0]['key'] :
56 raise Exception, 'Manifold db error'
58 def setLocalFields(self):
59 query = Query.get('local:object').filter_by('table', '==', self.type).select('column.name')
60 results = execute_query(self.request, query)
62 for r in results[0]['column'] :
63 self.fields.append(r['name'])
65 raise Exception, 'Manifold db error'
67 def setFields(self, fields):
71 selected_fields.append(p)
72 self.fields = selected_fields
75 def applyFilters(self, query, force_filters = False):
76 if (force_filters and not self.filters) :
77 raise Exception, "Filters required"
79 for k, f in self.filters.iteritems() :
81 query.filter_by(k, '!=', f[1:])
82 elif (f[:2] == ">=") :
83 query.filter_by(k, '>=', f[2:])
85 query.filter_by(k, '>', f[1:])
86 elif (f[:2] == "<=") :
87 query.filter_by(k, '<=', f[2:])
89 query.filter_by(k, '<', f[1:])
91 query.filter_by(k, '==', f)
95 query = Query.get(self.type)
96 if (self.id not in self.fields) :
97 query.select(self.fields + [self.id])
99 query.select(self.fields)
101 query = self.applyFilters(query)
102 return execute_query(self.request, query)
105 query = Query.update(self.type)
106 query = self.applyFilters(query, True)
108 query.set(self.params)
110 raise Exception, "Params are required for update"
111 return execute_query(self.request, query)
114 query = Query.delete(self.type)
115 query = self.applyFilters(query, True)
117 query.set(self.params)
119 raise Exception, "Params are required for update"
120 return execute_query(self.request, query)
123 return HttpResponse(json.dumps(self.get(), cls=DecimalEncoder, default=DateEncoder), content_type="application/json")
126 response = self.get()
128 response_data['fields'] = self.fields
129 response_data['labels'] = self.fields
130 response_data['data'] = []
131 response_data['total'] = len(response)
134 for p in self.fields :
136 response_data['data'].append(d)
138 return HttpResponse(json.dumps(response_data, cls=DecimalEncoder, default=DateEncoder), content_type="application/json")
141 return HttpResponse(json.dumps({'error' : msg}), content_type="application/json")
144 return HttpResponse(json.dumps({'success' : msg}), content_type="application/json")