Skip to content

Commit abab226

Browse files
committed
Use DataBlockResponse model to validate block db r/w
1 parent 705ecf8 commit abab226

File tree

6 files changed

+60
-13
lines changed

6 files changed

+60
-13
lines changed

pydatalab/schemas/cell.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,18 @@
223223
},
224224
"bokeh_plot_data": {
225225
"title": "Bokeh Plot Data",
226-
"type": "string"
226+
"anyOf": [
227+
{
228+
"type": "string"
229+
},
230+
{
231+
"type": "object"
232+
}
233+
]
234+
},
235+
"processed_data": {
236+
"title": "Processed Data",
237+
"type": "object"
227238
}
228239
},
229240
"required": [

pydatalab/schemas/equipment.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,18 @@
187187
},
188188
"bokeh_plot_data": {
189189
"title": "Bokeh Plot Data",
190-
"type": "string"
190+
"anyOf": [
191+
{
192+
"type": "string"
193+
},
194+
{
195+
"type": "object"
196+
}
197+
]
198+
},
199+
"processed_data": {
200+
"title": "Processed Data",
201+
"type": "object"
191202
}
192203
},
193204
"required": [

pydatalab/schemas/sample.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,18 @@
276276
},
277277
"bokeh_plot_data": {
278278
"title": "Bokeh Plot Data",
279-
"type": "string"
279+
"anyOf": [
280+
{
281+
"type": "string"
282+
},
283+
{
284+
"type": "object"
285+
}
286+
]
287+
},
288+
"processed_data": {
289+
"title": "Processed Data",
290+
"type": "object"
280291
}
281292
},
282293
"required": [

pydatalab/schemas/startingmaterial.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,18 @@
329329
},
330330
"bokeh_plot_data": {
331331
"title": "Bokeh Plot Data",
332-
"type": "string"
332+
"anyOf": [
333+
{
334+
"type": "string"
335+
},
336+
{
337+
"type": "object"
338+
}
339+
]
340+
},
341+
"processed_data": {
342+
"title": "Processed Data",
343+
"type": "object"
333344
}
334345
},
335346
"required": [

pydatalab/src/pydatalab/blocks/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from pydatalab import __version__
88
from pydatalab.logger import LOGGER
9+
from pydatalab.models.blocks import DataBlockResponse
910

1011
__all__ = ("generate_random_id", "DataBlock", "generate_js_callback_single_float_parameter")
1112

@@ -100,6 +101,8 @@ def generate_random_id():
100101
class DataBlock:
101102
"""Base class for a data block."""
102103

104+
block_db_model = DataBlockResponse
105+
103106
name: str = "base"
104107
"""The human-readable block name specifying which technique
105108
or file format it pertains to.
@@ -194,12 +197,9 @@ def __init__(
194197
def to_db(self):
195198
"""returns a dictionary with the data for this
196199
block, ready to be input into mongodb"""
197-
from bson import ObjectId
198200

199201
LOGGER.debug("Casting block %s to database object.", self.__class__.__name__)
200-
dct_for_db = {k: v for k, v in self.data.items() if k != "bokeh_plot_data"}
201-
dct_for_db["file_id"] = ObjectId(dct_for_db["file_id"]) if "file_id" in dct_for_db else None
202-
return dct_for_db
202+
return self.block_db_model(**self.data).dict(exclude_unset=True)
203203

204204
@classmethod
205205
def from_db(cls, block: dict):
@@ -339,6 +339,6 @@ def update_from_web(self, data: dict):
339339
"Updating block %s from web request",
340340
self.__class__.__name__,
341341
)
342-
self.data.update(data)
342+
self.data.update(self.block_db_model(**data).dict(exclude={}, exclude_unset=True))
343343

344344
return self

pydatalab/src/pydatalab/models/blocks.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pydantic import BaseModel
1+
from pydantic import BaseModel, Field
22

33
from pydatalab.models.utils import JSON_ENCODERS, PyObjectId
44

@@ -32,13 +32,16 @@ class DataBlockResponse(BaseModel):
3232
file_ids: list[PyObjectId] | None = None
3333
"""A list of file IDs associated with the block, if any."""
3434

35-
b64_encoded_image: dict[PyObjectId, str] | None = None
35+
b64_encoded_image: dict[PyObjectId, str] | None = Field(exclude=True)
3636
"""Any base64-encoded image data associated with the block, keyed by file_id, if any."""
3737

38-
bokeh_plot_data: str | None = None
38+
bokeh_plot_data: str | dict | None = Field(exclude=True)
3939
"""A JSON-encoded string containing the Bokeh plot data, if any."""
4040

41+
processed_data: dict | None = None
42+
"""Any processed data associated with the block, small enough to store."""
43+
4144
class Config:
4245
allow_population_by_field_name = True
4346
json_encoders = JSON_ENCODERS
44-
extra = "allow"
47+
extra = "ignore"

0 commit comments

Comments
 (0)