mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-03 19:24:48 +08:00
Create templates project when syncing templates if team doesn't have it [SCI-3039]
This commit is contained in:
parent
b71c9d96a2
commit
da30d187a6
5 changed files with 27 additions and 27 deletions
|
@ -313,16 +313,6 @@ class Team < ApplicationRecord
|
|||
|
||||
def generate_template_project
|
||||
return if without_templates
|
||||
user = created_by
|
||||
return unless user
|
||||
Project.transaction do
|
||||
tmpl_project = projects.create!(
|
||||
name: Constants::TEMPLATES_PROJECT_NAME,
|
||||
visibility: :visible,
|
||||
template: true
|
||||
)
|
||||
tmpl_project.user_projects.create!(user: user, role: 0)
|
||||
TemplatesService.new.update_project(tmpl_project)
|
||||
end
|
||||
TemplatesService.new.update_team(self)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,33 +13,42 @@ class TemplatesService
|
|||
end
|
||||
end
|
||||
|
||||
def update_project(project)
|
||||
return unless project.template
|
||||
owner = project.user_projects
|
||||
.where(role: 'owner')
|
||||
.order(:created_at)
|
||||
.first
|
||||
.user
|
||||
def update_team(team)
|
||||
tmpl_project = team.projects.where(template: true).take
|
||||
unless tmpl_project
|
||||
Project.transaction do
|
||||
tmpl_project = team.projects.create!(
|
||||
name: Constants::TEMPLATES_PROJECT_NAME,
|
||||
visibility: :visible,
|
||||
template: true
|
||||
)
|
||||
tmpl_project.user_projects.create!(user: team.created_by, role: 'owner')
|
||||
end
|
||||
end
|
||||
owner = tmpl_project.user_projects
|
||||
.where(role: 'owner')
|
||||
.order(:created_at)
|
||||
.first&.user
|
||||
return unless owner.present?
|
||||
updated = false
|
||||
exp_tmplt_dir_prefix = "#{@base_dir}/experiment_"
|
||||
existing = project.experiments.where.not(uuid: nil).pluck(:uuid)
|
||||
existing = tmpl_project.experiments.where.not(uuid: nil).pluck(:uuid)
|
||||
@experiment_templates.except(*existing).each_value do |id|
|
||||
importer_service = TeamImporter.new
|
||||
importer_service.import_experiment_template_from_dir(
|
||||
exp_tmplt_dir_prefix + id.to_s, project.id, owner.id
|
||||
exp_tmplt_dir_prefix + id.to_s, tmpl_project.id, owner.id
|
||||
)
|
||||
updated = true
|
||||
end
|
||||
updated
|
||||
end
|
||||
|
||||
def update_all_projects
|
||||
def update_all_templates
|
||||
processed_counter = 0
|
||||
updated_counter = 0
|
||||
Project.where(template: true).find_each do |project|
|
||||
Team.find_each do |team|
|
||||
processed_counter += 1
|
||||
updated_counter += 1 if update_project(project)
|
||||
updated_counter += 1 if update_team(team)
|
||||
end
|
||||
[updated_counter, processed_counter]
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@ if ENV['ENABLE_TEMPLATES_SYNC'] && ARGV[0] == 'jobs:work'
|
|||
# Templates sync periodic task
|
||||
scheduler.every '1h' do
|
||||
Rails.logger.info('Templates, syncing all template projects')
|
||||
updated, total = TemplatesService.new.update_all_projects
|
||||
updated, total = TemplatesService.new.update_all_templates
|
||||
Rails.logger.info(
|
||||
"Templates, total number of updated projects: #{updated} out of #{total}}"
|
||||
)
|
||||
|
|
|
@ -133,7 +133,7 @@ namespace :data do
|
|||
desc 'Update all templates projects'
|
||||
task update_all_templates: :environment do
|
||||
Rails.logger.info('Templates, syncing all templates projects')
|
||||
updated, total = TemplatesService.new.update_all_projects
|
||||
updated, total = TemplatesService.new.update_all_templates
|
||||
Rails.logger.info(
|
||||
"Templates, total number of updated projects: #{updated} out of #{total}}"
|
||||
)
|
||||
|
|
|
@ -25,12 +25,13 @@ describe TemplatesService do
|
|||
FileUtils.remove_dir(tmplts_dir) if Dir.exist?(tmplts_dir)
|
||||
FileUtils.mkdir(tmplts_dir)
|
||||
FileUtils.mv(exp_dir, "#{tmplts_dir}/experiment_#{demo_exp.id}")
|
||||
templates_project = create :project, name: 'Templates', template: true
|
||||
templates_project =
|
||||
create :project, name: 'Templates', template: true, team: main_team
|
||||
create(
|
||||
:user_project, :owner, project: templates_project, user: admin_user
|
||||
)
|
||||
ts = TemplatesService.new(tmplts_dir)
|
||||
ts.update_project(templates_project)
|
||||
ts.update_team(main_team)
|
||||
Delayed::Job.all.each { |job| dj_worker.run(job) }
|
||||
tmpl_exp = templates_project.experiments.first
|
||||
|
||||
|
|
Loading…
Reference in a new issue