mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-20 14:45:56 +08:00
Merge pull request #7233 from artoscinote/ma_SCI_10257
Add duplicate asset functionality [SCI-10257]
This commit is contained in:
commit
bd3968c4a6
|
@ -17,8 +17,8 @@ class AssetsController < ApplicationController
|
|||
helper_method :wopi_file_edit_button_status
|
||||
|
||||
before_action :load_vars, except: :create_wopi_file
|
||||
before_action :check_read_permission, except: %i(edit destroy create_wopi_file toggle_view_mode)
|
||||
before_action :check_edit_permission, only: %i(edit destroy toggle_view_mode)
|
||||
before_action :check_read_permission, except: %i(edit destroy duplicate create_wopi_file toggle_view_mode)
|
||||
before_action :check_edit_permission, only: %i(edit destroy duplicate toggle_view_mode)
|
||||
|
||||
def file_preview
|
||||
render json: { html: render_to_string(
|
||||
|
@ -311,6 +311,43 @@ class AssetsController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def duplicate
|
||||
ActiveRecord::Base.transaction do
|
||||
case @asset.parent
|
||||
when Step, Result
|
||||
new_asset = @asset.duplicate(
|
||||
new_name:
|
||||
"#{@asset.file.filename.base} (1).#{@asset.file.filename.extension}"
|
||||
)
|
||||
|
||||
@asset.parent.assets << new_asset
|
||||
end
|
||||
|
||||
case @asset.parent
|
||||
when Step
|
||||
message_items = { file: @asset.file_name }
|
||||
message_items[:my_module] = @assoc.protocol.my_module.id if @assoc.protocol.in_module?
|
||||
|
||||
log_step_activity(
|
||||
"#{@assoc.protocol.in_module? ? 'task' : 'protocol'}_step_file_duplicated",
|
||||
@assoc,
|
||||
@assoc.my_module&.project,
|
||||
message_items
|
||||
)
|
||||
when Result
|
||||
log_result_activity(
|
||||
:result_file_duplicated,
|
||||
@assoc,
|
||||
file: @asset.file_name,
|
||||
user: current_user.id,
|
||||
my_module: @assoc.my_module.id
|
||||
)
|
||||
end
|
||||
|
||||
render json: new_asset, serializer: AssetSerializer, user: current_user
|
||||
end
|
||||
end
|
||||
|
||||
def checksum
|
||||
render json: { checksum: @asset.file.blob.checksum }
|
||||
end
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
@open_scinote_editor="openScinoteEditor"
|
||||
@open_locally="openLocally"
|
||||
@delete="deleteModal = true"
|
||||
@duplicate="duplicate"
|
||||
@viewMode="changeViewMode"
|
||||
@move="showMoveModal"
|
||||
@menu-toggle="$emit('menu-toggle', $event)"
|
||||
|
@ -77,6 +78,7 @@ import MoveAssetModal from '../modal/move.vue';
|
|||
import MoveMixin from './mixins/move.js';
|
||||
import OpenLocallyMixin from './mixins/open_locally.js';
|
||||
import MenuDropdown from '../../menu_dropdown.vue';
|
||||
import axios from '../../../../packs/custom_axios.js';
|
||||
|
||||
export default {
|
||||
name: 'contextMenu',
|
||||
|
@ -157,6 +159,12 @@ export default {
|
|||
emit: 'move'
|
||||
});
|
||||
}
|
||||
if (this.attachment.attributes.urls.duplicate) {
|
||||
menu.push({
|
||||
text: this.i18n.t('assets.context_menu.duplicate'),
|
||||
emit: 'duplicate'
|
||||
});
|
||||
}
|
||||
if (this.attachment.attributes.urls.delete) {
|
||||
menu.push({
|
||||
text: this.i18n.t('assets.context_menu.delete'),
|
||||
|
@ -187,6 +195,13 @@ export default {
|
|||
data: { asset: { view_mode: viewMode } }
|
||||
});
|
||||
},
|
||||
duplicate() {
|
||||
axios.post(this.attachment.attributes.urls.duplicate).then(() => {
|
||||
this.reloadAttachments();
|
||||
}).catch((e) => {
|
||||
console.error(e);
|
||||
});
|
||||
},
|
||||
deleteAttachment() {
|
||||
this.deleteModal = false;
|
||||
this.$emit('attachment:delete');
|
||||
|
|
|
@ -209,8 +209,10 @@ class Asset < ApplicationRecord
|
|||
file&.blob&.content_type
|
||||
end
|
||||
|
||||
def duplicate
|
||||
def duplicate(new_name: nil)
|
||||
new_asset = dup
|
||||
file.filename = new_name if new_name
|
||||
|
||||
return unless new_asset.save
|
||||
|
||||
duplicate_file(new_asset)
|
||||
|
@ -223,7 +225,11 @@ class Asset < ApplicationRecord
|
|||
raise ArgumentError, 'Destination asset should be persisted first!' unless to_asset.persisted?
|
||||
|
||||
file.blob.open do |tmp_file|
|
||||
to_blob = ActiveStorage::Blob.create_and_upload!(io: tmp_file, filename: blob.filename)
|
||||
to_blob = ActiveStorage::Blob.create_and_upload!(
|
||||
io: tmp_file,
|
||||
filename: blob.filename,
|
||||
metadata: blob.metadata
|
||||
)
|
||||
to_asset.file.attach(to_blob)
|
||||
end
|
||||
|
||||
|
|
|
@ -144,10 +144,12 @@ class AssetSerializer < ActiveModel::Serializer
|
|||
marvin_js_start_edit: start_editing_marvin_js_asset_path(object),
|
||||
start_edit_image: start_edit_image_path(object),
|
||||
delete: asset_destroy_path(object),
|
||||
duplicate: asset_duplicate_path(object),
|
||||
move_targets: asset_move_tagets_path(object),
|
||||
move: asset_move_path(object)
|
||||
)
|
||||
end
|
||||
|
||||
urls[:open_vector_editor_edit] = edit_gene_sequence_asset_path(object.id) if can_manage_asset?(user, object)
|
||||
|
||||
if can_manage_asset?(user, object) && can_open_asset_locally?(user, object)
|
||||
|
|
|
@ -488,16 +488,19 @@ class Extends
|
|||
inventory_item_relationships_unlinked: 298,
|
||||
edit_task_step_file_locally: 299,
|
||||
edit_protocol_template_file_locally: 300,
|
||||
edit_task_result_file_locally: 301
|
||||
edit_task_result_file_locally: 301,
|
||||
task_step_file_duplicated: 302,
|
||||
result_file_duplicated: 303,
|
||||
protocol_step_file_duplicated: 304
|
||||
}
|
||||
|
||||
ACTIVITY_GROUPS = {
|
||||
projects: [*0..7, 32, 33, 34, 95, 108, 65, 109, *158..162, 241, 242, 243],
|
||||
task_results: [23, 26, 25, 42, 24, 40, 41, 99, 110, 122, 116, 128, *246..248, *257..273, *284..291, 301],
|
||||
task_results: [23, 26, 25, 42, 24, 40, 41, 99, 110, 122, 116, 128, *246..248, *257..273, *284..291, 301, 303],
|
||||
task: [8, 58, 9, 59, *10..14, 35, 36, 37, 53, 54, *60..63, 138, 139, 140, 64, 66, 106, 126, 120, 132,
|
||||
148, 166],
|
||||
task_protocol: [15, 22, 16, 18, 19, 20, 21, 17, 38, 39, 100, 111, 45, 46, 47, 121, 124, 115, 118, 127, 130, 137,
|
||||
184, 185, 188, 189, *192..203, 221, 222, 224, 225, 226, 236, *249..252, *274..278, 299],
|
||||
184, 185, 188, 189, *192..203, 221, 222, 224, 225, 226, 236, *249..252, *274..278, 299, 302],
|
||||
task_inventory: [55, 56, 146, 147, 183],
|
||||
experiment: [*27..31, 57, 141, 165],
|
||||
reports: [48, 50, 49, 163, 164],
|
||||
|
@ -506,7 +509,7 @@ class Extends
|
|||
protocol_repository: [80, 103, 89, 87, 79, 90, 91, 88, 85, 86, 84, 81, 82,
|
||||
83, 101, 112, 123, 125, 117, 119, 129, 131, 187, 186,
|
||||
190, 191, *204..215, 220, 223, 227, 228, 229, *230..235,
|
||||
*237..240, *253..256, *279..283, 300],
|
||||
*237..240, *253..256, *279..283, 300, 304],
|
||||
team: [92, 94, 93, 97, 104, 244, 245],
|
||||
label_templates: [*216..219]
|
||||
}
|
||||
|
|
|
@ -3641,6 +3641,7 @@ en:
|
|||
context_menu:
|
||||
set_view_size: "SET PREVIEW SIZE"
|
||||
delete: "Delete"
|
||||
duplicate: "Duplicate"
|
||||
move: "Move"
|
||||
inline_html: "Large"
|
||||
thumbnail_html: "Thumbnail"
|
||||
|
|
|
@ -314,6 +314,9 @@ en:
|
|||
edit_image_on_inventory_item_html: "%{user} edited image %{asset_name} on inventory item %{repository_row} in inventory %{repository}: %{action}."
|
||||
edit_wopi_file_on_inventory_item_html: "%{user} edited Office online file %{asset_name} on inventory item %{repository_row} in inventory %{repository}: %{action}."
|
||||
export_inventory_stock_consumption_html: "%{user} exported stock consumption for inventory item(s) %{inventory_items} in inventory %{repository}."
|
||||
task_step_file_duplicated_html: "%{user} duplicated file <strong>%{file}</strong> on protocol's step <strong>%{step}</strong> on task <strong>%{my_module}</strong>."
|
||||
result_file_duplicated_html: "%{user} duplicated file <strong>%{file}</strong> on result <strong>%{result}</strong> on task <strong>%{my_module}</strong>."
|
||||
protocol_step_file_duplicated_html: "%{user} duplicated file <strong>%{file}</strong> on protocol's step <strong>%{step}</strong> in Protocol repository."
|
||||
activity_name:
|
||||
create_project: "Project created"
|
||||
rename_project: "Project renamed"
|
||||
|
@ -585,6 +588,9 @@ en:
|
|||
edit_image_on_inventory_item: "Inventory item image edited"
|
||||
edit_wopi_file_on_inventory_item: "Inventory item wopi file edited"
|
||||
export_inventory_stock_consumption: "Inventory stock consumptions exported"
|
||||
task_step_file_duplicated: "File attachment on Task step duplicated"
|
||||
result_file_duplicated: "File attachment on Task result duplicated"
|
||||
protocol_step_file_duplicated: "File attachment on Protocol step duplicated"
|
||||
activity_group:
|
||||
projects: "Projects"
|
||||
task_results: "Task results"
|
||||
|
|
|
@ -822,6 +822,7 @@ Rails.application.routes.draw do
|
|||
get 'files/:id/move_targets', to: 'assets#move_targets', as: 'asset_move_tagets'
|
||||
post 'files/:id/move', to: 'assets#move', as: 'asset_move'
|
||||
delete 'files/:id/', to: 'assets#destroy', as: 'asset_destroy'
|
||||
post 'files/:id/duplicate', to: 'assets#duplicate', as: 'asset_duplicate'
|
||||
post 'files/create_wopi_file',
|
||||
to: 'assets#create_wopi_file',
|
||||
as: 'create_wopi_file'
|
||||
|
|
Loading…
Reference in a new issue