1 from manifold.core.query import Query
3 from django.views.generic.base import TemplateView
4 from django.shortcuts import render_to_response
6 from unfold.loginrequired import LoginRequiredView
7 from django.http import HttpResponse
9 from manifold.core.query import Query, AnalyzedQuery
10 from manifoldapi.manifoldapi import execute_query
12 from string import join
17 # handles serialization of datetime in json
18 DateEncoder = lambda obj: obj.strftime("%B %d, %Y %H:%M:%S") if isinstance(obj, datetime.datetime) else None
20 # support converting decimal in json
21 json.encoder.FLOAT_REPR = lambda o: format(o, '.2f')
23 # handles decimal numbers serialization in json
24 class DecimalEncoder(json.JSONEncoder):
25 def _iterencode(self, o, markers=None):
26 if isinstance(o, decimal.Decimal):
27 return (str(o) for o in [o])
28 return super(DecimalEncoder, self)._iterencode(o, markers)
30 def dispatch(request, object_type, object_name):
32 object_properties = None
36 'platform' : platform,
38 'resource' : resource,
40 'authority' : authority,
43 if request.method == 'POST':
44 req_items = request.POST.items()
45 elif request.method == 'GET':
46 req_items = request.GET.items()
49 if el[0].startswith('filters'):
50 object_filters[el[0][8:-1]] = el[1]
51 elif el[0].startswith('columns'):
52 object_properties = request.POST.getlist('columns[]')
55 if ((object_type == 'platform') or (object_type == 'testbed')) :
56 object_type = 'local:platform'
57 if object_properties == None :
58 object_properties = ['platform', 'platform_longname', 'platform_url', 'platform_description','gateway_type'];
59 return switch.get('platform', error)(request, object_name, object_properties, object_filters)
61 if object_properties == None :
62 query = Query.get('local:object').filter_by('table', '==', object_type).select('column.name')
63 results = execute_query(request, query)
65 object_properties = []
66 for r in results[0]['column'] :
67 object_properties.append(r['name'])
69 return error(request, object_name, {})
70 return switch.get(object_type, error)(request, object_name, object_properties, object_filters)
72 def platform(request, object_name, object_properties, object_filters = None):
73 query = Query().get('local:platform').filter_by('disabled', '==', '0').filter_by('gateway_type', '==', 'sfa').filter_by('platform', '!=', 'myslice')
75 for k, f in object_filters.iteritems() :
76 query.filter_by(k, '==', f)
77 query.select(object_properties)
78 return send(request, execute_query(request, query), object_properties)
80 # Add different filters possibilities [['user.user_hrn','==','$user_hrn'],['parent_authority','==','ple.upmc']]
81 def slice(request, object_name, object_properties, object_filters = None):
82 query = Query().get('slice')#.filter_by('user.user_hrn', '==', '$user_hrn')
84 for k, f in object_filters.iteritems() :
85 query.filter_by(k, '==', f)
86 query.select(object_properties)
87 return send(request, execute_query(request, query), object_properties)
89 def resource(request, object_name, object_properties, object_filters = None):
90 query = Query().get('resource')
92 for k, f in object_filters.iteritems() :
93 query.filter_by(k, '==', f)
94 query.select(object_properties)
95 return send(request, execute_query(request, query), object_properties)
97 def user(request, object_name, object_properties, object_filters = None):
98 query = Query().get('user')#.filter_by('user_hrn', '==', '$user_hrn')
100 for k, f in object_filters.iteritems() :
101 query.filter_by(k, '==', f)
102 query.select(object_properties)
103 return send(request, execute_query(request, query), object_properties)
105 def authority(request, object_name, object_properties, object_filters = None):
106 query = Query().get('authority')#.filter_by('user_hrn', '==', '$user_hrn')
108 for k, f in object_filters.iteritems() :
109 query.filter_by(k, '==', f)
110 query.select(object_properties)
111 return send(request, execute_query(request, query), object_properties)
113 def send(request, response, object_properties):
114 if request.path.split('/')[1] == 'rest' :
115 response_data = response
116 return HttpResponse(json.dumps(response_data, cls=DecimalEncoder, default=DateEncoder), content_type="application/json")
117 elif request.path.split('/')[1] == 'table' :
118 return render_to_response('table-default.html', {'data' : response, 'properties' : object_properties})
119 elif request.path.split('/')[1] == 'datatable' :
121 response_data['columns'] = object_properties
122 response_data['labels'] = object_properties
123 response_data['data'] = []
124 response_data['total'] = len(response)
127 for p in object_properties :
131 response_data['data'].append(d)
133 return HttpResponse(json.dumps(response_data, cls=DecimalEncoder, default=DateEncoder), content_type="application/json")
135 def error(request, object_name, object_properties):
136 return HttpResponse(json.dumps({'error' : 'an error has occurred'}), content_type="application/json")