Skip to content

Commit

Permalink
Fix collections block API and remove UI
Browse files Browse the repository at this point in the history
- Remove mention of collection blocks in UI until later refactor
  • Loading branch information
ml-evs committed Jul 28, 2023
1 parent 0023ea5 commit 9862dc9
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 107 deletions.
15 changes: 8 additions & 7 deletions pydatalab/pydatalab/blocks/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DataBlock:
cache: Optional[Dict[str, Any]] = None
plot_functions: Optional[Sequence[Callable[[], None]]] = None
# whether this datablock can operate on collection data, or just individual items
__supports_collections: bool = False
_supports_collections: bool = False

def __init__(
self,
Expand All @@ -63,10 +63,11 @@ def __init__(
if dictionary is None:
dictionary = {}

if item_id is None and not self.__supports_collections:
if item_id is None and not self._supports_collections:
breakpoint()
raise RuntimeError(f"Must supply `item_id` to make {self.__class__.__name__}.")

if collection_id is not None and not self.__supports_collections:
if collection_id is not None and not self._supports_collections:
raise RuntimeError(
f"This block ({self.__class__.__name__}) does not support collections."
)
Expand Down Expand Up @@ -174,20 +175,20 @@ def update_from_web(self, data):
class NotSupportedBlock(DataBlock):
blocktype = "notsupported"
description = "Block not supported"
__supports_collections = True
_supports_collections = True


class CommentBlock(DataBlock):
blocktype = "comment"
description = "Comment"
__supports_collections = True
_supports_collections = True


class MediaBlock(DataBlock):
blocktype = "media"
description = "Media"
accepted_file_extensions = (".png", ".jpeg", ".jpg", ".tif", ".tiff", ".mp4", ".mov", ".webm")
__supports_collections = False
_supports_collections = False

@property
def plot_functions(self):
Expand Down Expand Up @@ -216,7 +217,7 @@ class NMRBlock(DataBlock):
description = "Simple NMR Block"
accepted_file_extensions = ".zip"
defaults = {"process number": 1}
__supports_collections = False
_supports_collections = False

@property
def plot_functions(self):
Expand Down
5 changes: 4 additions & 1 deletion pydatalab/pydatalab/routes/v0_1/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def add_collection_data_block():
)

# get the new display_order:
display_order_result = flask_mongo.db.items.find_one(
display_order_result = flask_mongo.db.collections.find_one(
{"collection_id": collection_id, **get_default_permissions(user_only=True)},
{"display_order": 1},
)
Expand All @@ -130,6 +130,9 @@ def add_collection_data_block():
)


add_collection_data_block.methods = ("POST",) # type: ignore


def _save_block_to_db(block: DataBlock) -> bool:
"""Save data for a single block within an item to the database,
overwriting previous data saved there.
Expand Down
19 changes: 0 additions & 19 deletions webapp/src/server_fetch_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,25 +358,6 @@ export function addABlock(item_id, block_type, index = null) {
return block_id_promise;
}

export function addACollectionBlock(collection_id, block_type, index = null) {
console.log("addACollectionBlock called with", collection_id, block_type);
var block_id_promise = fetch_post(`${API_URL}/add-collection-data-block/`, {
collection_id: collection_id,
block_type: block_type,
index: index,
})
.then(function (response_json) {
store.commit("addACollectionBlock", {
collection_id: collection_id,
new_block_obj: response_json.new_block_obj,
new_block_insert_index: response_json.new_block_insert_index,
});
return response_json.new_block_obj.block_id;
})
.catch((error) => console.error("Error in addACollectionBlock:", error));
return block_id_promise;
}

export function saveItem(item_id) {
console.log("saveItem Called!");
var item_data = store.state.all_item_data[item_id];
Expand Down
27 changes: 0 additions & 27 deletions webapp/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,33 +132,6 @@ export default createStore({
state.all_item_data[item_id]["display_order"].push(new_block_id);
}
},
addACollectionBlock(state, { collection_id, new_block_obj, new_block_insert_index }) {
// payload: item_id, new_block_obj, new_display_order

// I should actually throw an error if this fails!
console.assert(
collection_id == new_block_obj.collection_id,
"The block has a different collection_id (%s) than the collection_id provided to addACollectionBlock (%s)",
collection_id,
new_block_obj.collection_id
);
console.log(
`addACollectionBlock called with: ${collection_id}, ${new_block_obj}, ${new_block_insert_index}`
);
let new_block_id = new_block_obj.block_id;
state.all_collection_data[collection_id]["blocks_obj"][new_block_id] = new_block_obj;
if (new_block_insert_index) {
state.all_collection_data[collection_id]["display_order"].splice(
new_block_insert_index,
0,
new_block_id
);
}
// if new_block_insert_index is None, then block is inserted at the end
else {
state.all_collection_data[collection_id]["display_order"].push(new_block_id);
}
},
updateBlockData(state, payload) {
// requires the following fields in payload:
// item_id, block_id, block_data
Expand Down
53 changes: 2 additions & 51 deletions webapp/src/views/CollectionPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,6 @@
</span>
<div class="navbar-nav">
<a class="nav-item nav-link" href="/">Home</a>
<div class="nav-item dropdown">
<a
class="nav-link dropdown-toggle ml-2"
id="navbarDropdown"
role="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
@click="isMenuDropdownVisible = !isMenuDropdownVisible"
>
Add a block
</a>
<div
class="dropdown-menu"
style="display: block"
aria-labelledby="navbarDropdown"
v-show="isMenuDropdownVisible"
>
<template v-for="(blockType, id) in blockTypes" :key="id">
<a
v-if="blockTypes[id].supportsCollections == true"
class="dropdown-item"
@click="newBlock($event, id)"
>
{{ blockType.description }}
</a>
</template>
</div>
</div>
<a class="nav-item nav-link" :href="this.collectionApiUrl" target="_blank">
<font-awesome-icon icon="code" fixed-width /> View JSON
</a>
Expand All @@ -62,19 +33,15 @@
<!-- Item-type header information goes here -->
<div class="editor-body">
<CollectionInformation :collection_id="collection_id" />

<div class="container">
<hr />
</div>
</div>
</template>

<script>
import CollectionInformation from "@/components/CollectionInformation";
import { getCollectionData, saveCollection, addACollectionBlock } from "@/server_fetch_utils";
import { getCollectionData, saveCollection } from "@/server_fetch_utils";
import FormattedCollectionName from "@/components/FormattedCollectionName.vue";
import tinymce from "tinymce/tinymce";
import { blockTypes, itemTypes } from "@/resources.js";
import { itemTypes } from "@/resources.js";
import { API_URL } from "@/resources.js";
import { formatDistance } from "date-fns";

Expand All @@ -88,20 +55,6 @@ export default {
};
},
methods: {
async newBlock(event, blockType, index = null) {
this.isMenuDropdownVisible = false;
this.isLoadingNewBlock = true;
this.$refs.blockLoadingIndicator.scrollIntoView({
behavior: "smooth",
});
var block_id = await addACollectionBlock(this.item_id, blockType, index);
// close the dropdown and scroll to the new block
var new_block_el = document.getElementById(block_id);
this.isLoadingNewBlock = false;
new_block_el.scrollIntoView({
behavior: "smooth",
});
},
saveCollectionData() {
// trigger the mce save so that they update the store with their content
console.log("save clicked!");
Expand Down Expand Up @@ -132,7 +85,6 @@ export default {
return this.$store.state.saved_status_collections[this.collection_id];
},
lastModified() {
// if (!this.item_data.last_modified) { return "" }
let item_date = this.collection_data.last_modified;
const save_date = new Date(item_date);
return formatDistance(save_date, new Date(), { addSuffix: true });
Expand All @@ -147,7 +99,6 @@ export default {
},
beforeMount() {
this.collectionApiUrl = API_URL + "/collections/" + this.collection_id;
this.blockTypes = blockTypes; // bind blockTypes as a NON-REACTIVE object to the this context so that it is accessible by the template.
},
mounted() {
// overwrite ctrl-s and cmd-s to save the page
Expand Down
2 changes: 0 additions & 2 deletions webapp/src/views/EditPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ import TinyMceInline from "@/components/TinyMceInline";
import SelectableFileTree from "@/components/SelectableFileTree";

import FileList from "@/components/FileList";
import Modal from "@/components/Modal";
import FileSelectModal from "@/components/FileSelectModal";
import { getItemData, addABlock, saveItem, updateBlockFromServer } from "@/server_fetch_utils";
import FormattedItemName from "@/components/FormattedItemName";
Expand Down Expand Up @@ -235,7 +234,6 @@ export default {
},
components: {
TinyMceInline,
Modal,
SelectableFileTree,
FileList,
FileSelectModal,
Expand Down

0 comments on commit 9862dc9

Please sign in to comment.