@@ -892,152 +892,6 @@ def purge_async(self, trans, dataset_id, filename):
892892 else :
893893 raise Exception (message )
894894
895- @web .expose
896- def copy_datasets (
897- self ,
898- trans ,
899- source_history = None ,
900- source_content_ids = "" ,
901- target_history_id = None ,
902- target_history_ids = "" ,
903- new_history_name = "" ,
904- do_copy = False ,
905- ** kwd ,
906- ):
907- user = trans .get_user ()
908- if source_history is not None :
909- decoded_source_history_id = self .decode_id (source_history )
910- history = self .history_manager .get_owned (
911- decoded_source_history_id , trans .user , current_history = trans .history
912- )
913- current_history = trans .get_history ()
914- else :
915- history = current_history = trans .get_history ()
916- refresh_frames = []
917- if source_content_ids :
918- if not isinstance (source_content_ids , list ):
919- source_content_ids = source_content_ids .split ("," )
920- encoded_dataset_collection_ids = [
921- s [len ("dataset_collection|" ) :] for s in source_content_ids if s .startswith ("dataset_collection|" )
922- ]
923- encoded_dataset_ids = [s [len ("dataset|" ) :] for s in source_content_ids if s .startswith ("dataset|" )]
924- decoded_dataset_collection_ids = set (map (self .decode_id , encoded_dataset_collection_ids ))
925- decoded_dataset_ids = set (map (self .decode_id , encoded_dataset_ids ))
926- else :
927- decoded_dataset_collection_ids = []
928- decoded_dataset_ids = []
929- if new_history_name :
930- target_history_ids = []
931- else :
932- if target_history_id :
933- target_history_ids = [self .decode_id (target_history_id )]
934- elif target_history_ids :
935- if not isinstance (target_history_ids , list ):
936- target_history_ids = target_history_ids .split ("," )
937- target_history_ids = list ({self .decode_id (h ) for h in target_history_ids if h })
938- else :
939- target_history_ids = []
940- done_msg = error_msg = ""
941- new_history = None
942- if do_copy :
943- invalid_contents = 0
944- if not (decoded_dataset_ids or decoded_dataset_collection_ids ) or not (
945- target_history_ids or new_history_name
946- ):
947- error_msg = "You must provide both source datasets and target histories. "
948- else :
949- if new_history_name :
950- new_history = trans .app .model .History ()
951- new_history .name = new_history_name
952- new_history .user = user
953- trans .sa_session .add (new_history )
954- with transaction (trans .sa_session ):
955- trans .sa_session .commit ()
956- target_history_ids .append (new_history .id )
957- if user :
958- target_histories = [
959- hist
960- for hist in map (trans .sa_session .query (trans .app .model .History ).get , target_history_ids )
961- if hist is not None and hist .user == user
962- ]
963- else :
964- target_histories = [history ]
965- if len (target_histories ) != len (target_history_ids ):
966- error_msg = (
967- error_msg
968- + "You do not have permission to add datasets to %i requested histories. "
969- % (len (target_history_ids ) - len (target_histories ))
970- )
971- source_contents = list (
972- map (trans .sa_session .query (trans .app .model .HistoryDatasetAssociation ).get , decoded_dataset_ids )
973- )
974- source_contents .extend (
975- map (
976- trans .sa_session .query (trans .app .model .HistoryDatasetCollectionAssociation ).get ,
977- decoded_dataset_collection_ids ,
978- )
979- )
980- source_contents .sort (key = lambda content : content .hid )
981- for content in source_contents :
982- if content is None :
983- error_msg = f"{ error_msg } You tried to copy a dataset that does not exist. "
984- invalid_contents += 1
985- elif content .history != history :
986- error_msg = f"{ error_msg } You tried to copy a dataset which is not in your current history. "
987- invalid_contents += 1
988- else :
989- for hist in target_histories :
990- if content .history_content_type == "dataset" :
991- copy = content .copy (flush = False )
992- hist .stage_addition (copy )
993- else :
994- copy = content .copy (element_destination = hist )
995- if user :
996- copy .copy_tags_from (user , content )
997- for hist in target_histories :
998- hist .add_pending_items ()
999- with transaction (trans .sa_session ):
1000- trans .sa_session .commit ()
1001- if current_history in target_histories :
1002- refresh_frames = ["history" ]
1003- hist_names_str = ", " .join (
1004- '<a href="{}" target="_top">{}</a>' .format (
1005- url_for (
1006- controller = "history" , action = "switch_to_history" , hist_id = trans .security .encode_id (hist .id )
1007- ),
1008- escape (hist .name ),
1009- )
1010- for hist in target_histories
1011- )
1012- num_source = len (source_content_ids ) - invalid_contents
1013- num_target = len (target_histories )
1014- done_msg = "%i %s copied to %i %s: %s." % (
1015- num_source ,
1016- inflector .cond_plural (num_source , "dataset" ),
1017- num_target ,
1018- inflector .cond_plural (num_target , "history" ),
1019- hist_names_str ,
1020- )
1021- trans .sa_session .refresh (history )
1022- source_contents = history .active_contents
1023- target_histories = [history ]
1024- if user :
1025- target_histories = user .active_histories
1026- return trans .fill_template (
1027- "/dataset/copy_view.mako" ,
1028- source_history = history ,
1029- current_history = current_history ,
1030- source_content_ids = source_content_ids ,
1031- target_history_id = target_history_id ,
1032- target_history_ids = target_history_ids ,
1033- source_contents = source_contents ,
1034- target_histories = target_histories ,
1035- new_history_name = new_history_name ,
1036- done_msg = done_msg ,
1037- error_msg = error_msg ,
1038- refresh_frames = refresh_frames ,
1039- )
1040-
1041895 def _copy_datasets (self , trans , dataset_ids , target_histories , imported = False ):
1042896 """Helper method for copying datasets."""
1043897 user = trans .get_user ()
0 commit comments