fixed hazelnut checkbox management
[myslice.git] / manifold / metadata.py
1 import json 
2
3 from manifold.manifoldresult import ManifoldResult
4 from manifold.manifoldapi import ManifoldAPI
5
6 debug=False
7 debug=True
8
9 # turn this on if you want to work offline
10 work_offline=False
11 #work_offline=True
12
13 class MetaData:
14
15     def __init__ (self, auth):
16         self.auth=auth
17         self.hash_by_object={}
18
19     def fetch (self):
20         offline_filename="offline-metadata.json"
21         if work_offline:
22             try:
23                 with file(offline_metadata) as f:
24                     self.hash_by_object=json.loads(f.read())
25                 return
26             except:
27                 print "metadata.work_offline: failed to decode %s"%offline_filename
28         manifold_api = ManifoldAPI(self.auth)
29         fields = ['table', 'column.column',
30                   'column.description','column.header', 'column.title',
31                   'column.unit', 'column.info_type',
32                   'column.resource_type', 'column.value_type',
33                   'column.allowed_values', 'column.platforms.platform',
34                   'column.platforms.platform_url']
35         rows_result = manifold_api.Get({
36             'object': 'local:object', # proposed to replace metadata:table
37             'fields':     fields 
38         })
39
40         if row_results['code'] == 1: # warning
41             messages.warning(request, result['description'])
42         elif row_results['code'] == 2:
43             messages.error(request, result['description'])
44             # XXX FAIL HERE XXX
45
46         rows = rows_result.ok_value()
47 # API errors will be handled by the outer logic
48 #        if not rows:
49 #            print "Failed to retrieve metadata",rows_result.error()
50 #            rows=[]
51         self.hash_by_object = dict ( [ (row['table'], row) for row in rows ] )
52         # save for next time we use offline mode
53         if debug and rows:
54             with file(offline_filename,'w') as f:
55                 f.write(json.dumps(self.hash_by_object))
56
57     def to_json(self):
58         return json.dumps(self.hash_by_object)
59
60     def details_by_object (self, object):
61         return self.hash_by_object[object]
62
63     def sorted_fields_by_object (self, object):
64         return self.hash_by_object[object]['column'].sort()
65
66     def get_field_type(self, object, field):
67         print "Temp fix for metadata::get_field_type() -> consider moving to manifold.core.metadata soon"
68         return field