2021-06-24 01:48:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BioEddieAssetsController < ApplicationController
|
2021-07-20 03:33:28 +08:00
|
|
|
include BioEddieActions
|
2021-06-24 01:48:44 +08:00
|
|
|
include ActiveStorage::SetCurrent
|
|
|
|
|
2021-07-01 18:03:25 +08:00
|
|
|
before_action :load_vars, except: %i(create bmt_request license)
|
2021-06-24 01:48:44 +08:00
|
|
|
before_action :load_create_vars, only: :create
|
|
|
|
|
2021-07-01 18:03:25 +08:00
|
|
|
before_action :check_read_permission, except: %i(update create start_editing bmt_request license)
|
2021-06-24 01:48:44 +08:00
|
|
|
before_action :check_edit_permission, only: %i(update create start_editing)
|
|
|
|
|
|
|
|
def create
|
|
|
|
asset = BioEddieService.create_molecule(bio_eddie_params, current_user, current_team)
|
|
|
|
|
2021-07-20 03:33:28 +08:00
|
|
|
create_create_bio_eddie_activity(asset, current_user)
|
|
|
|
|
2021-06-24 01:48:44 +08:00
|
|
|
if asset && bio_eddie_params[:object_type] == 'Step'
|
2021-08-13 21:15:45 +08:00
|
|
|
create_register_bio_eddie_activity(asset, current_user) if bio_eddie_params[:schedule_for_registration] == 'true'
|
2021-06-24 01:48:44 +08:00
|
|
|
render json: {
|
2023-06-21 20:13:20 +08:00
|
|
|
html: render_to_string(partial: 'assets/asset', locals: {
|
2021-06-24 01:48:44 +08:00
|
|
|
asset: asset,
|
|
|
|
gallery_view_id: bio_eddie_params[:object_id]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
elsif asset && bio_eddie_params[:object_type] == 'Result'
|
2021-08-13 21:15:45 +08:00
|
|
|
create_register_bio_eddie_activity(asset, current_user) if bio_eddie_params[:schedule_for_registration] == 'true'
|
2021-06-24 01:48:44 +08:00
|
|
|
render json: { status: 'created' }, status: :ok
|
|
|
|
else
|
|
|
|
render json: asset.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2021-08-11 15:50:00 +08:00
|
|
|
asset = BioEddieService.update_molecule(bio_eddie_params, current_team)
|
2021-06-24 01:48:44 +08:00
|
|
|
|
2021-07-20 03:33:28 +08:00
|
|
|
create_edit_bio_eddie_activity(asset, current_user, :finish_editing)
|
|
|
|
|
2021-06-24 01:48:44 +08:00
|
|
|
if asset
|
2021-08-13 21:15:45 +08:00
|
|
|
create_register_bio_eddie_activity(asset, current_user) if bio_eddie_params[:schedule_for_registration] == 'true'
|
2021-06-24 01:48:44 +08:00
|
|
|
render json: { url: rails_representation_url(asset.medium_preview),
|
|
|
|
id: asset.id,
|
|
|
|
file_name: asset.blob.metadata['name'] }
|
|
|
|
else
|
|
|
|
render json: { error: t('bio_eddie.no_molecules_found') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-01 18:03:25 +08:00
|
|
|
def license
|
|
|
|
license_file_path = Rails.root.join('data/bioeddie/license.cxl')
|
|
|
|
if File.file?(license_file_path)
|
|
|
|
send_file(license_file_path)
|
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-30 23:04:19 +08:00
|
|
|
def bmt_request
|
2021-09-07 17:39:49 +08:00
|
|
|
return render_404 unless Rails.application.config.x.biomolecule_toolkit_base_url
|
2021-06-30 23:04:19 +08:00
|
|
|
|
2021-09-07 17:39:49 +08:00
|
|
|
uri = URI.parse(Rails.application.config.x.biomolecule_toolkit_base_url)
|
2021-09-07 22:28:14 +08:00
|
|
|
uri.path = File.join(uri.path, request.original_fullpath.remove('/biomolecule_toolkit'))
|
2021-06-30 23:04:19 +08:00
|
|
|
|
|
|
|
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
|
|
|
api_request = "Net::HTTP::#{request.request_method.capitalize}".constantize.new(uri)
|
2021-09-07 17:39:49 +08:00
|
|
|
if Rails.application.config.x.biomolecule_toolkit_api_key
|
2021-09-07 19:27:32 +08:00
|
|
|
api_request['x-api-key'] = Rails.application.config.x.biomolecule_toolkit_api_key
|
2021-09-07 17:39:49 +08:00
|
|
|
end
|
2021-06-30 23:04:19 +08:00
|
|
|
api_request['Content-Type'] = 'application/json'
|
|
|
|
request_body = request.body.read
|
2021-08-27 22:02:11 +08:00
|
|
|
|
2021-06-30 23:04:19 +08:00
|
|
|
api_request.body = request_body if request_body.present?
|
|
|
|
api_response = http.request(api_request)
|
2021-08-27 22:02:11 +08:00
|
|
|
|
2021-06-30 23:04:19 +08:00
|
|
|
render body: api_response.body, content_type: api_response.content_type, status: api_response.code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-24 01:48:44 +08:00
|
|
|
def start_editing
|
2021-07-20 03:33:28 +08:00
|
|
|
create_edit_bio_eddie_activity(@asset, current_user, :start_editing)
|
2021-06-24 01:48:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars
|
|
|
|
@asset = current_team.assets.find_by(id: params[:id])
|
|
|
|
return render_404 unless @asset
|
|
|
|
|
|
|
|
@assoc = @asset.step || @asset.result
|
|
|
|
|
|
|
|
case @assoc
|
|
|
|
when Step
|
|
|
|
@protocol = @assoc.protocol
|
|
|
|
when Result
|
|
|
|
@my_module = @assoc.my_module
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_create_vars
|
|
|
|
case bio_eddie_params[:object_type]
|
|
|
|
when 'Step'
|
|
|
|
@assoc = Step.find_by(id: bio_eddie_params[:object_id])
|
|
|
|
@protocol = @assoc.protocol
|
|
|
|
when 'Result'
|
|
|
|
@assoc = MyModule.find_by(id: bio_eddie_params[:object_id])
|
|
|
|
@my_module = @assoc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_read_permission
|
|
|
|
case @assoc
|
|
|
|
when Step
|
|
|
|
return render_403 unless can_read_protocol_in_module?(@protocol) ||
|
|
|
|
can_read_protocol_in_repository?(@protocol)
|
|
|
|
when Result, MyModule
|
|
|
|
return render_403 unless can_read_experiment?(@my_module.experiment)
|
|
|
|
else
|
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_edit_permission
|
|
|
|
case @assoc
|
|
|
|
when Step
|
2021-10-08 18:43:26 +08:00
|
|
|
return render_403 unless can_manage_step?(@assoc)
|
2021-06-24 01:48:44 +08:00
|
|
|
when Result, MyModule
|
2021-09-14 17:08:35 +08:00
|
|
|
return render_403 unless can_manage_my_module?(@my_module)
|
2021-06-24 01:48:44 +08:00
|
|
|
else
|
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def bio_eddie_params
|
2021-07-15 18:37:04 +08:00
|
|
|
params.permit(:id, :description, :object_id, :object_type, :name, :image, :schedule_for_registration)
|
2021-06-24 01:48:44 +08:00
|
|
|
end
|
|
|
|
end
|