Skip to content

Commit 05f9554

Browse files
committed
testing fix for temp unit not coming through properly.
1 parent 5cc5f93 commit 05f9554

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

opensensor/collection_apis.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,23 +254,30 @@ def create_model_instance(model: Type[BaseModel], data: dict):
254254
nested_fields = get_nested_fields(model)
255255

256256
# Handle flat models (like Pressure, LiquidLevel, pH) that have a single main field
257-
if len(model.__fields__) == 2 and "timestamp" in model.__fields__:
258-
main_field = next(field for field in model.__fields__ if field != "timestamp")
257+
for field_name, _ in model.__fields__.items():
258+
if field_name == "timestamp":
259+
continue
260+
if field_name in nested_fields:
261+
continue
262+
259263
lookup_field = (
260264
model.collection_name() if hasattr(model, "collection_name") else model.__name__
261265
)
262-
mongo_field = new_collections.get(lookup_field, main_field.lower())
266+
mongo_field = new_collections.get(lookup_field, field_name.lower())
263267

264268
# Check if the mongo_field exists in the data
265269
if mongo_field in data:
266-
data[main_field] = data[mongo_field]
267-
elif main_field.lower() in data:
268-
# If the main_field (lowercase) exists in data, use it
269-
data[main_field] = data[main_field.lower()]
270+
data[field_name] = data[mongo_field]
271+
elif field_name in data:
272+
# If the field_name exists in data, use it
273+
data[field_name] = data[field_name]
274+
elif field_name.lower() in data:
275+
# If the field_name (lowercase) exists in data, use it
276+
data[field_name] = data[field_name.lower()]
270277
else:
271-
# If neither the mongo_field nor the main_field exists, log an error
278+
# If neither the mongo_field nor the field_name exists, log an error
272279
logger.error(
273-
f"Field '{mongo_field}' or '{main_field}' not found in data for model {model.__name__}"
280+
f"Field '{mongo_field}' or '{field_name}' not found in data for model {model.__name__}"
274281
)
275282
logger.error(f"Available fields in data: {list(data.keys())}")
276283
# You might want to set a default value or raise an exception here

0 commit comments

Comments
 (0)