1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 '''
21 JSON based rendering.
22
23 Created on 01.02.2012
24
25 @author: tmetsch
26 '''
27
28
29
30
31
32
33
34 from occi.core_model import Resource
35 from occi.handlers import CONTENT_TYPE
36 from occi.protocol.rendering import Rendering
37 import json
38
39
41 '''
42 Create a JSON struct for a category.
43 '''
44 data = {'term': category.term, 'scheme': category.scheme}
45 if hasattr(category, 'title') and category.title is not '':
46 data['title'] = category.title
47 if hasattr(category, 'related') and len(category.related) > 0:
48 rel_list = []
49 for item in category.related:
50 rel_list.append(str(item))
51 data['related'] = rel_list
52 if hasattr(category, 'location') and category.location is not None:
53 data['location'] = category.location
54 if hasattr(category, 'attributes') and len(category.attributes) >= 1:
55 attr_list = {}
56 for item in category.attributes:
57 if category.attributes[item] == 'required':
58 attr_list[item] = 'required'
59 elif category.attributes[item] == 'immutable':
60 attr_list[item] = 'immutable'
61 else:
62 attr_list[item] = 'muttable'
63 data['attributes'] = attr_list
64 if hasattr(category, 'actions') and len(category.actions) > 0:
65 action_list = []
66 for item in category.actions:
67 action_list.append(str(item))
68 data['actions'] = action_list
69 return data
70
71
73 '''
74 Create a JSON struct for an entity.
75 '''
76 data = {'kind': _from_category(entity.kind)}
77
78
79
80 mixins = []
81 for mixin in entity.mixins:
82 tmp = _from_category(mixin)
83 mixins.append(tmp)
84 data['mixins'] = mixins
85
86
87 actions = []
88 for action in entity.actions:
89 tmp = {'kind': _from_category(action), 'link': entity.identifier +
90 '?action=' + action.term}
91 actions.append(tmp)
92 data['actions'] = actions
93
94
95 if isinstance(entity, Resource):
96 links = []
97 for link in entity.links:
98 tmp = _from_entity(link)
99 tmp['source'] = link.source.identifier
100 tmp['target'] = link.target.identifier
101 links.append(tmp)
102 data['links'] = links
103
104
105 attr = {}
106 for attribute in entity.attributes:
107 attr[attribute] = entity.attributes[attribute]
108 data['attributes'] = attr
109
110 return data
111
112
114 '''
115 This is a rendering which will use the HTTP header to place the information
116 in an syntax and semantics as defined in the OCCI specification.
117 '''
118
119 mime_type = 'application/occi+json'
120
126
128 data = []
129 for item in entities:
130 data.append(_from_entity(item))
131
132 body = json.dumps(data, sort_keys=True, indent=2)
133 return {CONTENT_TYPE: self.mime_type}, body
134
136 data = []
137 for item in categories:
138 data.append(_from_category(item))
139
140 body = json.dumps(data, sort_keys=True, indent=2)
141 return {CONTENT_TYPE: self.mime_type}, body
142