Merge pull request #4363 from aignatov-bio/ai-sci-7089-include-label-templates-to-team-import-export

Add labels template to team import/export [SCI-7089]
This commit is contained in:
aignatov-bio 2022-09-15 11:20:28 +02:00 committed by GitHub
commit 19c20f2549
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View file

@ -36,6 +36,8 @@ class LabelTemplatesController < ApplicationController
ActiveRecord::Base.transaction do
label_template = ZebraLabelTemplate.default
label_template.team = current_team
label_template.created_by = current_user
label_template.last_modified_by = current_user
label_template.save!
log_activity(:label_template_created, label_template)
redirect_to label_template_path(label_template, new_label: true)
@ -63,6 +65,8 @@ class LabelTemplatesController < ApplicationController
LabelTemplate.where(team_id: current_team.id, id: params[:selected_ids]).each do |template|
new_template = template.dup
new_template.default = false
new_template.created_by = current_user
new_template.last_modified_by = current_user
new_template.name = template.name + '(1)'
new_template.save!
log_activity(

View file

@ -57,7 +57,8 @@ module ModelExporters
protocol_keywords: team.protocol_keywords,
project_folders: team.project_folders,
projects: team.projects.map { |p| project(p) },
activities: team.activities.where(project_id: nil)
activities: team.activities.where(project_id: nil),
label_templates: label_templates(team.label_templates)
}
end
@ -70,6 +71,14 @@ module ModelExporters
notification_json
end
def label_templates(templates)
templates.where.not(type: 'FluicsLabelTemplate').map do |template|
template_json = template.as_json
template_json['type'] = template.type # as_json ignore 'type' column
template_json
end
end
def user(user)
user_json = user.as_json
# Looks like Devise doesn't export some fields to JSON, so add it manually

View file

@ -34,6 +34,7 @@ class TeamImporter
@protocol_counter = 0
@step_counter = 0
@report_counter = 0
@label_template_counter = 0
@my_module_counter = 0
@notification_counter = 0
@result_counter = 0
@ -82,6 +83,8 @@ class TeamImporter
create_project_folders(team_json['project_folders'], team)
create_projects(team_json['projects'], team)
create_repositories(team_json['repositories'], team)
create_label_templates(team_json['label_templates'], team)
# Second run, we needed it because of some models should be created
@ -150,6 +153,7 @@ class TeamImporter
puts "Imported results: #{@result_counter}"
puts "Imported assets: #{@asset_counter}"
puts "Imported tinyMCE assets: #{@mce_asset_counter}"
puts "Imported label templates: #{@label_template_counter}"
MyModule.set_callback(:create, :before, :create_blank_protocol)
Protocol.set_callback(:save, :after, :update_linked_children)
@ -307,6 +311,21 @@ class TeamImporter
end
end
def create_label_templates(label_templates_json, team)
# Destroy default templates generated on team creation
team.label_templates.where.not(type: 'FluicsLabelTemplate').destroy_all
label_templates_json.each do |template_json|
template = LabelTemplate.new(template_json)
template.id = nil
template.team_id = team.id
template.created_by_id = find_user(template.created_by_id) if template.created_by_id
template.last_modified_by_id = find_user(template.last_modified_by_id) if template.last_modified_by_id
template.save!
@label_template_counter += 1
end
end
def create_activities(activities_json, team)
activities_json.each do |activity_json|
activity = Activity.new(activity_json)