2017-01-25 00:06:51 +08:00
|
|
|
class TeamsController < ApplicationController
|
2016-02-12 23:52:43 +08:00
|
|
|
before_action :load_vars, only: [:parse_sheet, :import_samples, :export_samples]
|
|
|
|
|
|
|
|
before_action :check_create_sample_permissions, only: [:parse_sheet, :import_samples]
|
|
|
|
before_action :check_view_samples_permission, only: [:export_samples]
|
|
|
|
|
|
|
|
def parse_sheet
|
|
|
|
session[:return_to] ||= request.referer
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if params[:file]
|
|
|
|
begin
|
|
|
|
|
2016-10-05 23:45:20 +08:00
|
|
|
if params[:file].size > Constants::FILE_MAX_SIZE_MB.megabytes
|
|
|
|
error = t 'general.file.size_exceeded',
|
|
|
|
file_size: Constants::FILE_MAX_SIZE_MB
|
2016-09-28 18:54:47 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
flash[:alert] = error
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {message: error},
|
|
|
|
status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2017-01-25 00:06:51 +08:00
|
|
|
sheet = Team.open_spreadsheet(params[:file])
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Check if we actually have any rows (last_row > 1)
|
|
|
|
if sheet.last_row.between?(0, 1)
|
|
|
|
flash[:notice] = t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.parse_sheet.errors.empty_file")
|
2016-02-12 23:52:43 +08:00
|
|
|
redirect_to session.delete(:return_to) and return
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get data (it will trigger any errors as well)
|
|
|
|
@header = sheet.row(1)
|
|
|
|
@rows = [];
|
|
|
|
@rows << Hash[[@header, sheet.row(2)].transpose]
|
|
|
|
|
|
|
|
# Fill in fields for dropdown
|
2017-01-24 23:57:14 +08:00
|
|
|
@available_fields = @team.get_available_sample_fields
|
2016-12-21 21:34:53 +08:00
|
|
|
# Truncate long fields
|
|
|
|
@available_fields.update(@available_fields) do |_k, v|
|
|
|
|
v.truncate(Constants::NAME_TRUNCATION_LENGTH_DROPDOWN)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Save file for next step (importing)
|
|
|
|
@temp_file = TempFile.new(
|
|
|
|
session_id: session.id,
|
|
|
|
file: params[:file]
|
|
|
|
)
|
|
|
|
|
|
|
|
if @temp_file.save
|
2017-05-09 20:05:21 +08:00
|
|
|
@temp_file.destroy_obsolete
|
2016-02-12 23:52:43 +08:00
|
|
|
# format.html
|
|
|
|
format.json {
|
|
|
|
render :json => {
|
|
|
|
:html => render_to_string({
|
|
|
|
:partial => "samples/parse_samples_modal.html.erb"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-01-24 23:57:14 +08:00
|
|
|
error = t("teams.parse_sheet.errors.temp_file_failure")
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
flash[:alert] = error
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {message: error},
|
|
|
|
status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue ArgumentError, CSV::MalformedCSVError
|
2017-01-24 23:57:14 +08:00
|
|
|
error = t('teams.parse_sheet.errors.invalid_file',
|
2017-01-06 00:00:21 +08:00
|
|
|
encoding: ''.encoding)
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
flash[:alert] = error
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {message: error},
|
|
|
|
status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
rescue TypeError
|
2017-01-24 23:57:14 +08:00
|
|
|
error = t("teams.parse_sheet.errors.invalid_extension")
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
flash[:alert] = error
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {message: error},
|
|
|
|
status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
2017-01-24 23:57:14 +08:00
|
|
|
error = t("teams.parse_sheet.errors.no_file_selected")
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
flash[:alert] = error
|
|
|
|
session[:return_to] ||= request.referer
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {message: error},
|
|
|
|
status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_samples
|
|
|
|
session[:return_to] ||= request.referer
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if params[:file_id]
|
|
|
|
@temp_file = TempFile.find_by_id(params[:file_id])
|
|
|
|
|
|
|
|
if @temp_file
|
|
|
|
# Check if session_id is equal to prevent file stealing
|
|
|
|
if @temp_file.session_id == session.id
|
|
|
|
# Check if mappings exists or else we don't have anything to parse
|
|
|
|
if params[:mappings]
|
2017-01-25 00:06:51 +08:00
|
|
|
@sheet = Team.open_spreadsheet(@temp_file.file)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Check for duplicated values
|
|
|
|
h1 = params[:mappings].clone.delete_if { |k, v| v.empty? }
|
|
|
|
if h1.length == h1.invert.length
|
|
|
|
|
|
|
|
# Check if there exist mapping for sample name (it's mandatory)
|
|
|
|
if params[:mappings].has_value?("-1")
|
2017-01-24 23:57:14 +08:00
|
|
|
result = @team.import_samples(@sheet, params[:mappings], current_user)
|
2016-02-12 23:52:43 +08:00
|
|
|
nr_of_added = result[:nr_of_added]
|
|
|
|
total_nr = result[:total_nr]
|
|
|
|
|
|
|
|
if result[:status] == :ok
|
|
|
|
# If no errors are present, redirect back
|
|
|
|
# to samples table
|
|
|
|
flash[:success] = t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.success_flash",
|
2016-02-12 23:52:43 +08:00
|
|
|
nr: nr_of_added,
|
|
|
|
samples: t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.sample",
|
2016-02-12 23:52:43 +08:00
|
|
|
count: total_nr
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@temp_file.destroy
|
|
|
|
format.html {
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
flash.keep(:success)
|
|
|
|
render json: { status: :ok }
|
|
|
|
}
|
|
|
|
else
|
|
|
|
# Otherwise, also redirect back,
|
|
|
|
# but display different message
|
|
|
|
flash[:alert] = t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.partial_success_flash",
|
2016-02-12 23:52:43 +08:00
|
|
|
nr: nr_of_added,
|
|
|
|
samples: t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.sample",
|
2016-02-12 23:52:43 +08:00
|
|
|
count: total_nr
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@temp_file.destroy
|
|
|
|
format.html {
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
flash.keep(:alert)
|
|
|
|
render json: { status: :unprocessable_entity }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# This is currently the only AJAX error response
|
|
|
|
flash_alert = t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.errors.no_sample_name")
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
flash[:alert] = flash_alert
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "parse_error.html.erb",
|
|
|
|
locals: { error: flash_alert }
|
|
|
|
})
|
|
|
|
},
|
|
|
|
status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# This code should never execute unless user tampers with
|
|
|
|
# JS (selects same column in more than one dropdown)
|
|
|
|
flash_alert = t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.errors.duplicated_values")
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
flash[:alert] = flash_alert
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "parse_error.html.erb",
|
|
|
|
locals: { error: flash_alert }
|
|
|
|
})
|
|
|
|
},
|
|
|
|
status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@temp_file.destroy
|
|
|
|
flash[:alert] = t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.errors.no_data_to_parse")
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
flash.keep(:alert)
|
|
|
|
render json: { status: :unprocessable_entity }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@temp_file.destroy
|
|
|
|
flash[:alert] = t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.errors.session_expired")
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
flash.keep(:alert)
|
|
|
|
render json: { status: :unprocessable_entity }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# No temp file to begin with, so no need to destroy it
|
|
|
|
flash[:alert] = t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.errors.temp_file_not_found")
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
flash.keep(:alert)
|
|
|
|
render json: { status: :unprocessable_entity }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
flash[:alert] = t(
|
2017-01-24 23:57:14 +08:00
|
|
|
"teams.import_samples.errors.temp_file_not_found")
|
2016-02-12 23:52:43 +08:00
|
|
|
format.html {
|
|
|
|
redirect_to session.delete(:return_to)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
flash.keep(:alert)
|
|
|
|
render json: { status: :unprocessable_entity }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_samples
|
2017-05-29 20:42:55 +08:00
|
|
|
if params[:sample_ids] && params[:header_ids]
|
2017-06-07 19:51:16 +08:00
|
|
|
generate_samples_zip
|
2017-05-29 20:42:55 +08:00
|
|
|
else
|
|
|
|
flash[:alert] = t('zip_export.export_error')
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2017-05-29 20:42:55 +08:00
|
|
|
redirect_to :back
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2017-05-29 20:42:55 +08:00
|
|
|
def routing_error(error = 'Routing error', status = :not_found, exception=nil)
|
|
|
|
redirect_to root_path
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def load_vars
|
2017-01-25 00:06:51 +08:00
|
|
|
@team = Team.find_by_id(params[:id])
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
unless @team
|
2016-02-12 23:52:43 +08:00
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_create_sample_permissions
|
2017-01-24 23:57:14 +08:00
|
|
|
unless can_create_samples(@team)
|
2016-02-12 23:52:43 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_view_samples_permission
|
2017-01-24 23:57:14 +08:00
|
|
|
unless can_view_samples(@team)
|
2016-02-12 23:52:43 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-07 19:51:16 +08:00
|
|
|
def generate_samples_zip
|
2017-05-29 20:42:55 +08:00
|
|
|
zip = ZipExport.create(user: current_user)
|
|
|
|
zip.generate_exportable_zip(
|
|
|
|
current_user,
|
|
|
|
@team.to_csv(Sample.where(id: params[:sample_ids]), params[:header_ids]),
|
|
|
|
:samples
|
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|