Reactivated PI get emails
[myslice.git] / manifold / metadata.py
1 import json 
2 import os.path
3
4 from manifold.manifoldresult import ManifoldResult
5 from manifold.manifoldapi import ManifoldAPI
6
7 from django.contrib                     import messages
8
9 debug=False
10 #debug=True
11
12 class MetaData:
13
14     def __init__ (self, auth):
15         self.auth=auth
16         self.hash_by_object={}
17
18     def fetch (self, request):
19         manifold_api = ManifoldAPI(self.auth)
20         fields = ['table', 'column.name', 'column.qualifier', 'column.type', 
21                   'column.is_array', 'column.description', 'column.default', 'key', 'capability']
22         #fields = ['table', 'column.column',
23         #          'column.description','column.header', 'column.title',
24         #          'column.unit', 'column.info_type',
25         #          'column.resource_type', 'column.value_type',
26         #          'column.allowed_values', 'column.platforms.platform',
27         #          'column.platforms.platform_url']
28         request={ 'action': 'get',
29                   'object': 'local:object', # proposed to replace metadata:table
30                   'fields':  fields ,
31                   }
32         result = manifold_api.forward(request)
33
34         # xxx need a way to export error messages to the UI
35         if result['code'] == 1: # warning
36             # messages.warning(request, result['description'])
37             print ("METADATA WARNING -",request,result['description'])
38         elif result['code'] == 2:
39             # messages.error(request, result['description'])
40             print ("METADATA ERROR -",request,result['description'])
41             # XXX FAIL HERE XXX
42             return
43
44         rows = result.ok_value()
45 # API errors will be handled by the outer logic
46 #        if not rows:
47 #            print "Failed to retrieve metadata",rows_result.error()
48 #            rows=[]
49         self.hash_by_object = dict ( [ (row['table'], row) for row in rows ] )
50
51     def to_json(self):
52         return json.dumps(self.hash_by_object)
53
54     def details_by_object (self, object):
55         return self.hash_by_object[object]
56
57     def sorted_fields_by_object (self, object):
58         return self.hash_by_object[object]['column'].sort()
59
60     def get_field_type(self, object, field):
61         if debug: print "Temp fix for metadata::get_field_type() -> consider moving to manifold.core.metadata soon"
62         return field