added mass create

This commit is contained in:
duratarskeyk 2015-12-16 18:36:38 +00:00
parent 160a8312d4
commit 001b7ef25e
13 changed files with 47049 additions and 3 deletions

View File

@ -33,6 +33,11 @@ class Projects::ProjectsController < Projects::BaseController
@project = Project.new(mass_import: true) @project = Project.new(mass_import: true)
end end
def mass_create
authorize :project
@project = Project.new(mass_create: true)
end
def run_mass_import def run_mass_import
@project = Project.new project_params @project = Project.new project_params
@project.owner = choose_owner @project.owner = choose_owner
@ -48,6 +53,21 @@ class Projects::ProjectsController < Projects::BaseController
end end
end end
def run_mass_create
@project = Project.new project_params
@project.owner = choose_owner
authorize @project
@project.valid?
@project.errors.messages.slice! :url
if @project.errors.messages.blank? # We need only url validation
@project.init_mass_create
flash[:notice] = t('flash.project.mass_create_added_to_queue')
redirect_to projects_path
else
render :mass_create
end
end
def edit def edit
authorize @project authorize @project
@project_aliases = Project.project_aliases(@project).paginate(page: current_page) @project_aliases = Project.project_aliases(@project).paginate(page: current_page)

View File

@ -0,0 +1,27 @@
require 'nokogiri'
require 'open-uri'
module Project::MassCreate
extend ActiveSupport::Concern
module ClassMethods
def run_mass_create(url, visibility, owner, add_to_repository_id)
repository = Repository.find add_to_repository_id
open(url) { |f|
f.each_line {
|line|
project = owner.projects.build(
name: line,
visibility: visibility,
is_package: false
)
project.owner = owner
if project.save
repository.projects << project rescue nil
project.update_attributes(is_package: true)
end
}
}
end
end
end

View File

@ -10,6 +10,7 @@ class Project < ActiveRecord::Base
include Project::DefaultBranch include Project::DefaultBranch
include Project::Finders include Project::Finders
include Project::GithubApi include Project::GithubApi
include Project::MassCreate
VISIBILITIES = ['open', 'hidden'] VISIBILITIES = ['open', 'hidden']
MAX_OWN_PROJECTS = 32000 MAX_OWN_PROJECTS = 32000
@ -41,8 +42,8 @@ class Project < ActiveRecord::Base
message: I18n.t("activerecord.errors.project.uname") }, message: I18n.t("activerecord.errors.project.uname") },
length: { maximum: 100 } length: { maximum: 100 }
validates :maintainer, presence: true, unless: :new_record? validates :maintainer, presence: true, unless: :new_record?
validates :url, presence: true, format: { with: /\Ahttps?:\/\/[\S]+\z/ }, if: :mass_import validates :url, presence: true, format: { with: /\Ahttps?:\/\/[\S]+\z/ }, if: :mass_import or :mass_create
validates :add_to_repository_id, presence: true, if: :mass_import validates :add_to_repository_id, presence: true, if: :mass_import or :mass_create
validates :visibility, presence: true, inclusion: { in: VISIBILITIES } validates :visibility, presence: true, inclusion: { in: VISIBILITIES }
validate { errors.add(:base, :can_have_less_or_equal, count: MAX_OWN_PROJECTS) if owner.projects.size >= MAX_OWN_PROJECTS } validate { errors.add(:base, :can_have_less_or_equal, count: MAX_OWN_PROJECTS) if owner.projects.size >= MAX_OWN_PROJECTS }
# throws validation error message from ProjectToRepository model into Project model # throws validation error message from ProjectToRepository model into Project model
@ -62,12 +63,16 @@ class Project < ActiveRecord::Base
after_save :attach_to_personal_repository after_save :attach_to_personal_repository
after_update -> { update_path_to_project(name_was) }, if: :name_changed? after_update -> { update_path_to_project(name_was) }, if: :name_changed?
attr_accessor :url, :srpms_list, :mass_import, :add_to_repository_id attr_accessor :url, :srpms_list, :mass_import, :add_to_repository_id, :mass_create
def init_mass_import def init_mass_import
Project.perform_later :low, :run_mass_import, url, srpms_list, visibility, owner, add_to_repository_id Project.perform_later :low, :run_mass_import, url, srpms_list, visibility, owner, add_to_repository_id
end end
def init_mass_create
Project.perform_later :low, :run_mass_create, url, visibility, owner, add_to_repository_id
end
def name_with_owner def name_with_owner
"#{owner_uname || owner.uname}/#{name}" "#{owner_uname || owner.uname}/#{name}"
end end

View File

@ -56,6 +56,8 @@ class ProjectPolicy < ApplicationPolicy
is_admin? || user.platforms.main.find{ |p| local_admin?(p) }.present? is_admin? || user.platforms.main.find{ |p| local_admin?(p) }.present?
end end
alias_method :mass_create?, :mass_import?
def run_mass_import? def run_mass_import?
return true if is_admin? return true if is_admin?
return false unless owner_policy.write? return false unless owner_policy.write?
@ -63,6 +65,8 @@ class ProjectPolicy < ApplicationPolicy
repo.platform.main? && PlatformPolicy.new(user, repo.platform).add_project? repo.platform.main? && PlatformPolicy.new(user, repo.platform).add_project?
end end
alias_method :run_mass_create?, :run_mass_import?
# for grack # for grack
def write? def write?
return false if user.guest? return false if user.guest?

View File

@ -6,6 +6,8 @@
%p= link_to t('layout.projects.new'), new_project_path, class: 'btn btn-primary' %p= link_to t('layout.projects.new'), new_project_path, class: 'btn btn-primary'
- if policy(:project).mass_import? - if policy(:project).mass_import?
%p= link_to t('layout.projects.mass_import'), mass_import_projects_path, class: 'btn btn-primary' %p= link_to t('layout.projects.mass_import'), mass_import_projects_path, class: 'btn btn-primary'
- if policy(:project).mass_create?
%p= link_to t('layout.projects.mass_create'), mass_create_projects_path, class: 'btn btn-primary'
%hr.offset10 %hr.offset10
%h3=t('layout.relations.filters') %h3=t('layout.relations.filters')
%ul.nav.nav-pills.nav-stacked %ul.nav.nav-pills.nav-stacked

View File

@ -0,0 +1,20 @@
.container.col-md-offset-2.col-md-8
h3
= title t("layout.projects.mass_create")
.row ng-init="owner = 'me'"
= simple_form_for @project, url: run_mass_create_projects_path do |f|
= f.hidden_field :mass_create
= f.input :url
= f.input :add_to_repository_id,
as: :grouped_select,
collection: mass_import_repositories_for_group_select,
group_method: :last,
include_blank: false
= render 'owner', f: f
= f.input :visibility,
collection: project_visibility_options,
include_blank: false
= f.button :submit, t('layout.add')

View File

@ -19,6 +19,7 @@ en:
owner_name: Owner name owner_name: Owner name
build_schedule: Build schedule build_schedule: Build schedule
mass_import: Mass import mass_import: Mass import
mass_create: Mass create
branches: Branches branches: Branches
delete_branch: Delete branch delete_branch: Delete branch
restore_branch: Restore branch restore_branch: Restore branch
@ -108,6 +109,7 @@ en:
flash: flash:
project: project:
mass_import_added_to_queue: Mass import added to queue mass_import_added_to_queue: Mass import added to queue
mass_create_added_to_queue: Mass create added to queue
saved: Project saved saved: Project saved
save_error: Unable to save project save_error: Unable to save project
save_warning_ssh_key: Project owner must provide a SSH key in his profile save_warning_ssh_key: Project owner must provide a SSH key in his profile

View File

@ -20,6 +20,7 @@ ru:
owner_name: Владелец проекта owner_name: Владелец проекта
build_schedule: Расписание сборок build_schedule: Расписание сборок
mass_import: Массовый импорт mass_import: Массовый импорт
mass_create: Массовое создание
branches: Ветки branches: Ветки
delete_branch: Удалить ветку delete_branch: Удалить ветку
restore_branch: Восстановить ветку restore_branch: Восстановить ветку
@ -109,6 +110,7 @@ ru:
flash: flash:
project: project:
mass_import_added_to_queue: Массовый импорт добавлен в очередь mass_import_added_to_queue: Массовый импорт добавлен в очередь
mass_create_added_to_queue: Массовый создание добавлено в очередь
saved: Проект успешно сохранен saved: Проект успешно сохранен
save_error: Не удалось сохранить проект save_error: Не удалось сохранить проект
save_warning_ssh_key: Владельцу проекта необходимо указать в профиле свой SSH ключ save_warning_ssh_key: Владельцу проекта необходимо указать в профиле свой SSH ключ

View File

@ -298,6 +298,8 @@ Rails.application.routes.draw do
collection do collection do
post :run_mass_import post :run_mass_import
get :mass_import get :mass_import
post :run_mass_create
get :mass_create
end end
end end
scope '*name_with_owner', name_with_owner: Project::OWNER_AND_NAME_REGEXP do # project scope '*name_with_owner', name_with_owner: Project::OWNER_AND_NAME_REGEXP do # project

15654
open-uri20151216-12288-18s9dqz Normal file

File diff suppressed because it is too large Load Diff

15654
open-uri20151216-12533-af6h7x Normal file

File diff suppressed because it is too large Load Diff

15654
open-uri20151216-13209-1v3m2kg Normal file

File diff suppressed because it is too large Load Diff