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