Skip to content

Commit

Permalink
Save manually created resource in the config
Browse files Browse the repository at this point in the history
  • Loading branch information
aidazolic committed Nov 20, 2023
1 parent 475998a commit 63e9eee
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
22 changes: 22 additions & 0 deletions editor/core/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .names import find_unique_name
from .state import FileObject
from .state import FileSet


@dataclasses.dataclass
Expand Down Expand Up @@ -124,3 +125,24 @@ def file_from_upload(
sha256=sha256,
df=df,
)


def file_from_form(
file_type: FileType, type: str, name, description, sha256: str, names: set[str]
) -> FileObject | FileSet:
"""Creates a file based on manually added fields."""
if type == "File object":
return FileObject(
name=find_unique_name(names, name),
description=description,
content_url="",
encoding_format=file_type.encoding_format,
sha256=sha256,
df=None,
)
else:
return FileSet(
name=find_unique_name(names, name),
description=description,
encoding_format=file_type.encoding_format,
)
61 changes: 41 additions & 20 deletions editor/views/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import streamlit as st

from components.tree import render_tree
from core.files import file_from_form
from core.files import file_from_upload
from core.files import file_from_url
from core.files import FILE_TYPES
Expand All @@ -15,8 +16,16 @@

Resource = FileObject | FileSet

FILE_OBJECT = "File object"
FILE_SET = "File set"
RESOURCE_TYPES = [FILE_OBJECT, FILE_SET]

_DISTANT_URL_KEY = "import_from_url"
_LOCAL_FILE_KEY = "import_from_local_file"
_MANUAL_RESOURCE_TYPE_KEY = "create_manually_type"
_MANUAL_NAME_KEY = "manual_object_name"
_MANUAL_DESCRIPTION_KEY = "manual_object_description"
_MANUAL_SHA256_KEY = "manual_object_sha256"


def render_files():
Expand Down Expand Up @@ -77,28 +86,24 @@ def _render_upload_panel():
st.text_input("URL:", key=_DISTANT_URL_KEY)

with tab3:
resource_type = st.selectbox("Type", options=["FileObject", "FileSet"])

file = FileObject() if resource_type == "FileObject" else FileSet()

name = st.text_input(
needed_field("File name"), value=file.name, key="manual_name"
resource_type = st.selectbox(
"Type", options=RESOURCE_TYPES, key=_MANUAL_RESOURCE_TYPE_KEY
)
description = st.text_area(
st.text_input(
needed_field("File name"),
key=_MANUAL_NAME_KEY,
)
st.text_area(
"File description",
value=file.description,
placeholder="Provide a clear description of the file.",
key="manual_description",
key=_MANUAL_DESCRIPTION_KEY,
)
sha256 = st.text_input(
needed_field("SHA256"),
value=file.sha256,
disabled=True,
key="manual_sha256",
st.text_input(
"SHA256",
key=_MANUAL_SHA256_KEY,
)
parent = st.text_input(
needed_field("Parent"),
value=file.encoding_format,
st.text_input(
"Parent",
key="manual_parent",
)

Expand All @@ -115,10 +120,26 @@ def handle_on_click():
file = file_from_url(file_type, url, names)
elif uploaded_file:
file = file_from_upload(file_type, uploaded_file, names)
# todo: handle manually created resource.
else:
st.toast("Please, import either a local file or a URL.", icon="❌")
return
resource_type = st.session_state[_MANUAL_RESOURCE_TYPE_KEY]
needs_sha256 = resource_type == FILE_OBJECT

name = st.session_state[_MANUAL_NAME_KEY]
description = st.session_state[_MANUAL_DESCRIPTION_KEY]
sha256 = st.session_state[_MANUAL_SHA256_KEY] if needs_sha256 else None

if not name or (needs_sha256 and not sha256):
# Some required fields are empty.
st.toast(
"Please, import either a local file, provide a download URL, or"
" create a resource manually.",
icon="❌",
)
return
file = file_from_form(
file_type, resource_type, name, description, sha256, names
)

st.session_state[Metadata].add_distribution(file)
record_sets = infer_record_sets(file, names)
for record_set in record_sets:
Expand Down

0 comments on commit 63e9eee

Please sign in to comment.