Merge pull request #4152 from aignatov-bio/ai-sci-6875-update-team-import-export

Update team import/export scripts [SCI-6875]
This commit is contained in:
aignatov-bio 2022-06-08 15:43:50 +02:00 committed by GitHub
commit 81773151a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 21 deletions

View file

@ -43,15 +43,26 @@ module ModelExporters
@assets_to_copy.push(step.assets.to_a) if step.assets.present?
{
step: step,
checklists: step.checklists.map { |c| checklist(c) },
step_orderable_elements: step.step_orderable_elements.map { |e| step_orderable_element(e) },
step_comments: step.step_comments,
step_assets: step.step_assets,
assets: step.assets.map { |a| assets_data(a) },
step_tables: step.step_tables,
tables: step.tables.map { |t| table(t) }
}
end
def step_orderable_element(element)
element_json = element.as_json
case element.orderable_type
when 'StepText'
element_json['step_text'] = element.orderable.as_json
when 'Checklist'
element_json['checklist'] = checklist(element.orderable)
when 'StepTable'
element_json['table'] = table(element.orderable.table)
end
element_json
end
def assets_data(asset)
return unless asset.file.attached?

View file

@ -808,8 +808,14 @@ class TeamImporter
step_comment.save!
end
step_json['tables'].each do |table_json|
table = Table.new(table_json)
step_json['step_orderable_elements'].each do |element_json|
if element_json['step_text']
orderable = StepText.new(element_json['step_text'])
orderable.step_id = step.id
orderable.id = nil
orderable.save!
elsif element_json['table']
table = Table.new(element_json['table'])
orig_table_id = table.id
table.id = nil
table.created_by_id = user_id || find_user(table.created_by_id)
@ -820,15 +826,21 @@ class TeamImporter
table.data_vector = Base64.decode64(table.data_vector)
table.save!
@table_mappings[orig_table_id] = table.id
StepTable.create!(step: step, table: table)
orderable = StepTable.create!(step: step, table: table)
elsif element_json['checklist']
orderable = create_step_checklist(element_json['checklist'], step, user_id)
end
StepOrderableElement.create!(
position: element_json['position'],
step: step,
orderable: orderable
)
end
step_json['assets'].each do |asset_json|
asset = create_asset(asset_json, protocol.team, user_id)
StepAsset.create!(step: step, asset: asset)
end
create_step_checklists(step_json['checklists'], step, user_id)
end
end
@ -915,8 +927,7 @@ class TeamImporter
asset
end
def create_step_checklists(step_checklists_json, step, user_id = nil)
step_checklists_json.each do |checklist_json|
def create_step_checklist(checklist_json, step, user_id = nil)
checklist = Checklist.new(checklist_json['checklist'])
orig_checklist_id = checklist.id
checklist.id = nil
@ -937,7 +948,7 @@ class TeamImporter
user_id || find_user(checklist_item.last_modified_by_id)
checklist_item.save!
end
end
checklist
end
def create_reports(reports_json, team)