Skip to content

Commit 87ec238

Browse files
committed
fix creation of new policies
1 parent 3144a61 commit 87ec238

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/cbapi/defense/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
class DefenseMutableModel(MutableBaseModel):
1313
_change_object_http_method = "PATCH"
14+
_change_object_key_name = None
15+
16+
def __init__(self, cb, model_unique_id=None, initial_data=None, force_init=False, full_doc=False):
17+
super(DefenseMutableModel, self).__init__(cb, model_unique_id=model_unique_id, initial_data=initial_data,
18+
force_init=force_init, full_doc=full_doc)
19+
if not self._change_object_key_name:
20+
self._change_object_key_name = self.primary_key
1421

1522
def _parse(self, obj):
1623
if type(obj) == dict and self.info_key in obj:
@@ -79,6 +86,12 @@ def _refresh_if_needed(self, request_ret):
7986
self._info = message.get(self.info_key)
8087
self._full_init = True
8188
refresh_required = False
89+
else:
90+
if self._change_object_key_name in message.keys():
91+
# if all we got back was an ID, try refreshing to get the entire record.
92+
log.debug("Only received an ID back from the server, forcing a refresh")
93+
self._info[self.primary_key] = message[self._change_object_key_name]
94+
refresh_required = True
8295
else:
8396
# "success" is False
8497
raise ServerError(request_ret.status_code, message.get("message", ""),
@@ -131,5 +144,6 @@ class Policy(DefenseMutableModel, CreatableModelMixin):
131144
info_key = "policyInfo"
132145
swagger_meta_file = "defense/models/policyInfo.yaml"
133146
_change_object_http_method = "PUT"
147+
_change_object_key_name = "policyId"
134148

135149

src/cbapi/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def __get__(self, instance, instance_type=None):
116116
class ObjectFieldDescriptor(FieldDescriptor):
117117
def __get__(self, instance, instance_type=None):
118118
ret = super(ObjectFieldDescriptor, self).__get__(instance, instance_type)
119-
return json.loads(ret) or {}
119+
return ret or {}
120120

121121

122122
class DateTimeFieldDescriptor(FieldDescriptor):

0 commit comments

Comments
 (0)