4 from manifold.manifoldresult import ManifoldResult
5 from manifold.manifoldapi import ManifoldAPI
7 from django.contrib import messages
12 # turn this on if you want to work offline
18 def __init__ (self, auth):
20 self.hash_by_object={}
22 def fetch (self, request):
23 offline_filename="%s/../offline-metadata.json"%os.path.dirname(__file__)
26 with file(offline_metadata) as f:
27 self.hash_by_object=json.loads(f.read())
30 print "metadata.work_offline: failed to decode %s"%offline_filename
31 manifold_api = ManifoldAPI(self.auth)
32 fields = ['table', 'column.name', 'column.qualifier', 'column.type',
33 'column.is_array', 'column.description', 'column.default', 'key', 'capability']
34 #fields = ['table', 'column.column',
35 # 'column.description','column.header', 'column.title',
36 # 'column.unit', 'column.info_type',
37 # 'column.resource_type', 'column.value_type',
38 # 'column.allowed_values', 'column.platforms.platform',
39 # 'column.platforms.platform_url']
40 request={ 'action': 'get',
41 'object': 'local:object', # proposed to replace metadata:table
44 result = manifold_api.forward(request)
46 # xxx need a way to export error messages to the UI
47 if result['code'] == 1: # warning
48 # messages.warning(request, result['description'])
49 print ("METADATA WARNING -",request,result['description'])
50 elif result['code'] == 2:
51 # messages.error(request, result['description'])
52 print ("METADATA ERROR -",request,result['description'])
56 rows = result.ok_value()
57 # API errors will be handled by the outer logic
59 # print "Failed to retrieve metadata",rows_result.error()
61 self.hash_by_object = dict ( [ (row['table'], row) for row in rows ] )
62 # save for next time we use offline mode
64 with file(offline_filename,'w') as f:
65 f.write(json.dumps(self.hash_by_object))
68 return json.dumps(self.hash_by_object)
70 def details_by_object (self, object):
71 return self.hash_by_object[object]
73 def sorted_fields_by_object (self, object):
74 return self.hash_by_object[object]['column'].sort()
76 def get_field_type(self, object, field):
77 print "Temp fix for metadata::get_field_type() -> consider moving to manifold.core.metadata soon"