Skip to content

Commit 5c84165

Browse files
committed
domain.py - use clean_payload instead of strip_none_values
1 parent 18ba273 commit 5c84165

1 file changed

Lines changed: 28 additions & 50 deletions

File tree

labkey/domain.py

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,10 @@
1717
from typing import Dict, List, Union, Tuple, TextIO
1818

1919
from .server_context import ServerContext
20+
from .utils import clean_payload
2021
from labkey.query import QueryFilter
2122

2223

23-
def strip_none_values(data: dict, do_strip: bool = True):
24-
if do_strip:
25-
for k in list(data.keys()):
26-
if data[k] is None:
27-
del data[k]
28-
return data
29-
30-
3124
# modeled on org.labkey.api.gwt.client.model.GWTPropertyDescriptor
3225
class PropertyDescriptor:
3326
def __init__(self, **kwargs):
@@ -115,7 +108,6 @@ def __init__(self, **kwargs):
115108
self.value_expression = kwargs.pop("value_expression", kwargs.pop("valueExpression", None))
116109

117110
def to_json(self, strip_none=True):
118-
# TODO: Likely only want to include those that are not None
119111
data = {
120112
"conceptURI": self.concept_uri,
121113
"container": self.container,
@@ -161,19 +153,11 @@ def to_json(self, strip_none=True):
161153
"typeEditable": self.type_editable,
162154
"url": self.url,
163155
"valueExpression": self.value_expression,
156+
"conditionalFormats": [f.to_json() for f in self.conditional_formats],
157+
"propertyValidators": [p.to_json() for p in self.property_validators],
164158
}
165159

166-
json_formats = []
167-
for f in self.conditional_formats:
168-
json_formats.append(f.to_json())
169-
data["conditionalFormats"] = json_formats
170-
171-
json_validators = []
172-
for p in self.property_validators:
173-
json_validators.append(p.to_json())
174-
data["propertyValidators"] = json_validators
175-
176-
return strip_none_values(data, strip_none)
160+
return clean_payload(data) if strip_none else data
177161

178162

179163
class PropertyValidator:
@@ -199,7 +183,7 @@ def to_json(self, strip_none=True):
199183
"type": self.type,
200184
}
201185

202-
return strip_none_values(data, strip_none)
186+
return clean_payload(data) if strip_none else data
203187

204188

205189
class ConditionalFormat:
@@ -212,7 +196,9 @@ def __init__(self, **kwargs):
212196
self.text_color = kwargs.pop("text_color", kwargs.pop("textColor", None))
213197

214198
def to_json(self):
215-
data = {
199+
# Note: unlike the other to_json methods here, this one keeps its None values, and the keys
200+
# are all lower case rather than camelCase.
201+
return {
216202
"backgroundcolor": self.background_color,
217203
"bold": self.bold,
218204
"filter": self.filter,
@@ -221,8 +207,6 @@ def to_json(self):
221207
"textcolor": self.text_color,
222208
}
223209

224-
return data
225-
226210

227211
# modeled on org.labkey.api.gwt.client.model.GWTDomain
228212
class Domain:
@@ -277,19 +261,11 @@ def to_json(self, strip_none=True):
277261
"queryName": self.query_name,
278262
"schemaName": self.schema_name,
279263
"templateDescription": self.template_description,
264+
"fields": [field.to_json() for field in self.fields],
265+
"indices": [index.to_json() for index in self.indices],
280266
}
281267

282-
json_fields = []
283-
for field in self.fields:
284-
json_fields.append(field.to_json())
285-
data["fields"] = json_fields
286-
287-
json_indices = []
288-
for index in self.indices:
289-
json_indices.append(index.to_json())
290-
data["indices"] = json_indices
291-
292-
return strip_none_values(data, strip_none)
268+
return clean_payload(data) if strip_none else data
293269

294270

295271
# TODO: Determine if this can be used when initializing domain.create
@@ -314,7 +290,7 @@ def __init__(self, **kwargs):
314290
def to_json(self, strip_none=True):
315291
data = {"columnNames": self.column_names, "unique": self.unique}
316292

317-
return strip_none_values(data, strip_none)
293+
return clean_payload(data) if strip_none else data
318294

319295

320296
def conditional_format(
@@ -473,12 +449,14 @@ def get_domain_details(
473449
url = server_context.build_url(
474450
"property", "getDomainDetails.api", container_path=container_path
475451
)
476-
payload = {
477-
"schemaName": schema_name,
478-
"queryName": query_name,
479-
"domainId": domain_id,
480-
"domainKind": domain_kind,
481-
}
452+
payload = clean_payload(
453+
{
454+
"schemaName": schema_name,
455+
"queryName": query_name,
456+
"domainId": domain_id,
457+
"domainKind": domain_kind,
458+
}
459+
)
482460
response = server_context.make_request(url, payload, method="GET")
483461
raw_domain = response.get("domainDesign", None)
484462
domain = None
@@ -531,14 +509,14 @@ def save(
531509
:return:
532510
"""
533511
url = server_context.build_url("property", "saveDomain.api", container_path=container_path)
534-
payload = {
535-
"domainDesign": domain.to_json(),
536-
"queryName": query_name,
537-
"schemaName": schema_name,
538-
}
539-
540-
if options is not None:
541-
payload["options"] = options
512+
payload = clean_payload(
513+
{
514+
"domainDesign": domain.to_json(),
515+
"queryName": query_name,
516+
"schemaName": schema_name,
517+
"options": options,
518+
}
519+
)
542520

543521
return server_context.make_request(url, json=payload)
544522

0 commit comments

Comments
 (0)