Skip to content

Commit

Permalink
fix export fso import could work on image and text
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmadHAW committed Dec 3, 2024
1 parent f54b588 commit 218662b
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions backend/src/app/core/data/export/export_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,17 +617,17 @@ def __generate_export_dfs_for_user_data_in_project(
db: Session,
user_id: int,
project_id: int,
) -> Tuple[List[pd.DataFrame], List[pd.DataFrame]]:
) -> Tuple[List[Tuple[str, pd.DataFrame]], List[pd.DataFrame]]:
logger.info(f"Exporting data of User {user_id} in Project {project_id} ...")
user = crud_user.read(db=db, id=user_id)

# all AnnotationDocuments
adocs = user.annotation_documents
exported_adocs: List[pd.DataFrame] = []
exported_adocs: List[Tuple[str, pd.DataFrame]] = []
for adoc in adocs:
if adoc.source_document.project_id == project_id:
export_data = self.__generate_export_df_for_adoc(db=db, adoc_id=adoc.id)
exported_adocs.append(export_data)
exported_adocs.append((adoc.source_document.filename, export_data))

# all Memos
memos = user.memos
Expand Down Expand Up @@ -789,14 +789,13 @@ def _export_user_data_from_proj(

exported_files = []
# one file per adoc
for adoc_df in exported_adocs:
if len(adoc_df) > 0: # for adocs with 0 annos
export_file = self.__write_export_data_to_temp_file(
data=adoc_df,
export_format=export_format,
fn=f"adoc_{adoc_df.iloc[0].adoc_id}",
)
exported_files.append(export_file)
for sdoc_name, adoc_df in exported_adocs:
export_file = self.__write_export_data_to_temp_file(
data=adoc_df,
export_format=export_format,
fn=f"adoc_{sdoc_name}",
)
exported_files.append(export_file)

# one file for all memos
if len(exported_memos) > 0:
Expand Down Expand Up @@ -863,7 +862,7 @@ def _export_all_data_from_proj(
proj = crud_project.read(db=db, id=project_id)
users = proj.users

exported_adocs: Dict[int, List[pd.DataFrame]] = dict()
exported_adocs: Dict[str, List[pd.DataFrame]] = dict()
exported_memos: List[pd.DataFrame] = []
exported_logbooks: List[Tuple[int, str]] = []
exported_files = []
Expand Down Expand Up @@ -910,16 +909,16 @@ def _export_all_data_from_proj(
)

# group the adocs by sdoc name and merge them later
for adoc_df in ex_adocs:
if len(adoc_df) > 0: # for adocs with 0 annos:
sdoc_name = adoc_df.iloc[0].sdoc_name
if sdoc_name not in exported_adocs:
exported_adocs[sdoc_name] = []
exported_adocs[sdoc_name].append(adoc_df)
for sdoc_name, adoc_df in ex_adocs:
if sdoc_name not in exported_adocs:
exported_adocs[sdoc_name] = []
exported_adocs[sdoc_name].append(adoc_df)
# merge adocs
merged_exported_adocs: List[pd.DataFrame] = []
merged_exported_adocs: List[Tuple[str, pd.DataFrame]] = []
for sdoc_name in exported_adocs.keys():
merged_exported_adocs.append(pd.concat(exported_adocs[sdoc_name]))
merged_exported_adocs.append(
(sdoc_name, pd.concat(exported_adocs[sdoc_name]))
)

# write users to files
users_file = self.__write_export_data_to_temp_file(
Expand All @@ -930,11 +929,11 @@ def _export_all_data_from_proj(
exported_files.append(users_file)

# write adocs to files
for adoc_df in merged_exported_adocs:
for sdoc_name, adoc_df in merged_exported_adocs:
export_file = self.__write_export_data_to_temp_file(
data=adoc_df,
export_format=export_format,
fn=adoc_df.iloc[0].sdoc_name,
fn=sdoc_name,
append_suffix=True,
)
exported_files.append(export_file)
Expand Down Expand Up @@ -967,12 +966,23 @@ def _export_all_data_from_proj(
)
if len(exported_tags) > 0:
exported_tag_df = pd.concat(exported_tags)
export_file = self.__write_export_data_to_temp_file(
data=exported_tag_df,
export_format=export_format,
fn=f"project_{project_id}_tags",
else:
exported_tag_df = pd.DataFrame(
columns=[
"tag_name",
"description",
"color",
"created",
"parent_tag_name",
"applied_to_sdoc_filenames",
]
)
exported_files.append(export_file)
export_file = self.__write_export_data_to_temp_file(
data=exported_tag_df,
export_format=export_format,
fn=f"project_{project_id}_tags",
)
exported_files.append(export_file)

# write all sdoc metadata to one file
exported_project_metadata = (
Expand Down

0 comments on commit 218662b

Please sign in to comment.