Skip to content

Commit 7c77647

Browse files
committed
Remove defunct code for from_db and dereference_files
1 parent 54f34d7 commit 7c77647

File tree

3 files changed

+2
-86
lines changed

3 files changed

+2
-86
lines changed

pydatalab/src/pydatalab/blocks/base.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,6 @@ def to_db(self) -> dict:
202202
LOGGER.debug("Casting block %s to database object.", self.__class__.__name__)
203203
return self.block_db_model(**self.data).dict(exclude_unset=True)
204204

205-
@classmethod
206-
def from_db(cls, block: dict):
207-
"""Create a block from data stored in the database."""
208-
LOGGER.debug("Loading block %s from database object.", cls.__class__.__name__)
209-
new_block = cls(
210-
item_id=block.get("item_id"),
211-
collection_id=block.get("collection_id"),
212-
init_data=block,
213-
)
214-
if "file_id" in new_block.data:
215-
new_block.data["file_id"] = str(new_block.data["file_id"])
216-
217-
if new_block.data.get("title", "") == new_block.description:
218-
new_block.data["title"] = new_block.name
219-
220-
return new_block
221-
222205
def to_web(self) -> dict[str, Any]:
223206
"""Returns a JSON serializable dictionary to render the data block on the web."""
224207
block_errors = []

pydatalab/src/pydatalab/routes/v0_1/blocks.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ def add_data_block():
3232

3333
block = BLOCK_TYPES[block_type](item_id=item_id)
3434

35-
data = block.to_db()
36-
37-
# currently, adding to both blocks and blocks_obj to mantain compatibility with
38-
# the old site. The new site only uses blocks_obj
3935
if insert_index:
4036
display_order_update = {
4137
"$each": [block.block_id],
@@ -47,8 +43,8 @@ def add_data_block():
4743
result = flask_mongo.db.items.update_one(
4844
{"item_id": item_id, **get_default_permissions(user_only=True)},
4945
{
50-
"$push": {"blocks": data, "display_order": display_order_update},
51-
"$set": {f"blocks_obj.{block.block_id}": data},
46+
"$push": {"display_order": display_order_update},
47+
"$set": {f"blocks_obj.{block.block_id}": block.to_db()},
5248
},
5349
)
5450

pydatalab/src/pydatalab/routes/v0_1/items.py

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -28,61 +28,6 @@
2828
def _(): ...
2929

3030

31-
def reserialize_blocks(display_order: list[str], blocks_obj: dict[str, dict]) -> dict[str, dict]:
32-
"""Create the corresponding Python objects from JSON block data, then
33-
serialize it again as JSON to populate any missing properties.
34-
35-
Parameters:
36-
blocks_obj: A dictionary containing the JSON block data, keyed by block ID.
37-
38-
Returns:
39-
A dictionary with the re-serialized block data.
40-
41-
"""
42-
for block_id in display_order:
43-
try:
44-
block_data = blocks_obj[block_id]
45-
except KeyError:
46-
LOGGER.warning(f"block_id {block_id} found in display order but not in blocks_obj")
47-
continue
48-
blocktype = block_data["blocktype"]
49-
blocks_obj[block_id] = (
50-
BLOCK_TYPES.get(blocktype, BLOCK_TYPES["notsupported"]).from_db(block_data).to_web()
51-
)
52-
53-
return blocks_obj
54-
55-
56-
# Seems to be obselete now?
57-
def dereference_files(file_ids: list[str | ObjectId]) -> dict[str, dict]:
58-
"""For a list of Object IDs (as strings or otherwise), query the files collection
59-
and return a dictionary of the data stored under each ID.
60-
61-
Parameters:
62-
file_ids: The list of IDs of files to return;
63-
64-
Returns:
65-
The dereferenced data as a dictionary with (string) ID keys.
66-
67-
"""
68-
results = {
69-
str(f["_id"]): f
70-
for f in flask_mongo.db.files.find(
71-
{
72-
"_id": {"$in": [ObjectId(_id) for _id in file_ids]},
73-
}
74-
)
75-
}
76-
if len(results) != len(file_ids):
77-
raise RuntimeError(
78-
"Some file IDs did not have corresponding database entries.\n"
79-
f"Returned: {list(results.keys())}\n"
80-
f"Requested: {file_ids}\n"
81-
)
82-
83-
return results
84-
85-
8631
@ITEMS.route("/equipment/", methods=["GET"])
8732
def get_equipment_summary():
8833
_project = {
@@ -837,12 +782,6 @@ def get_item_data(
837782
):
838783
"""Generates a JSON response for the item with the given `item_id`,
839784
or `refcode` additionally resolving relationships to files and other items.
840-
841-
Parameters:
842-
load_blocks: Whether to regenerate any data blocks associated with this
843-
sample (i.e., create the Python object corresponding to the block and
844-
call its render function).
845-
846785
"""
847786
redirect_to_ui = bool(request.args.get("redirect-to-ui", default=False, type=json.loads))
848787
if refcode and redirect_to_ui and CONFIG.APP_URL:
@@ -911,8 +850,6 @@ def get_item_data(
911850
raise KeyError(f"Item {item_id=} has no type field in document.")
912851

913852
doc = ItemModel(**doc)
914-
if load_blocks:
915-
doc.blocks_obj = reserialize_blocks(doc.display_order, doc.blocks_obj)
916853

917854
# find any documents with relationships that mention this document
918855
relationships_query_results = flask_mongo.db.items.find(

0 commit comments

Comments
 (0)