From a7e908b102d80f222eec90366753ee744552c73b Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 12 Nov 2013 16:04:40 +0400 Subject: [PATCH] #319: Added ability to mass adding projects to the repository --- .../api/v1/repositories_controller.rb | 2 +- .../platforms/repositories_controller.rb | 26 +++++++++++++---- app/models/repository.rb | 29 +++++++++++++++++++ .../repositories/projects_list.html.haml | 10 ++++++- .../repositories/remove_project.html.haml | 12 ++++++++ .../platforms/repositories/show.html.haml | 2 ++ config/locales/models/repository.en.yml | 8 +++-- config/locales/models/repository.ru.yml | 7 ++++- config/routes.rb | 1 + 9 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 app/views/platforms/repositories/remove_project.html.haml diff --git a/app/controllers/api/v1/repositories_controller.rb b/app/controllers/api/v1/repositories_controller.rb index a5d7a2f60..3a0bf8279 100644 --- a/app/controllers/api/v1/repositories_controller.rb +++ b/app/controllers/api/v1/repositories_controller.rb @@ -45,7 +45,7 @@ class Api::V1::RepositoriesController < Api::V1::BaseController def add_project project = Project.where(:id => params[:project_id]).first - if project + if project && can?(:read, project) begin @repository.projects << project render_json_response @repository, "Project '#{project.id}' has been added to repository successfully" diff --git a/app/controllers/platforms/repositories_controller.rb b/app/controllers/platforms/repositories_controller.rb index eface6394..8b6981a35 100644 --- a/app/controllers/platforms/repositories_controller.rb +++ b/app/controllers/platforms/repositories_controller.rb @@ -83,12 +83,20 @@ class Platforms::RepositoriesController < Platforms::BaseController end def add_project + if projects_list = params.try(:[], :repository).try(:[], :projects_list) + @repository.add_projects projects_list, current_user + redirect_to platform_repository_path(@platform, @repository), :notice => t('flash.repository.projects_will_be_added') + end if params[:project_id] @project = Project.find(params[:project_id]) - begin - @repository.projects << @project - flash[:notice] = t('flash.repository.project_added') - rescue ActiveRecord::RecordInvalid + if can?(:read, @project) + begin + @repository.projects << @project + flash[:notice] = t('flash.repository.project_added') + rescue ActiveRecord::RecordInvalid + flash[:error] = t('flash.repository.project_not_added') + end + else flash[:error] = t('flash.repository.project_not_added') end redirect_to platform_repository_path(@platform, @repository) @@ -136,8 +144,14 @@ class Platforms::RepositoriesController < Platforms::BaseController end def remove_project - ProjectToRepository.where(:project_id => params[:project_id], :repository_id => @repository.id).destroy_all - redirect_to platform_repository_path(@platform, @repository), :notice => t('flash.repository.project_removed') + if projects_list = params.try(:[], :repository).try(:[], :projects_list) + @repository.remove_projects projects_list + redirect_to platform_repository_path(@platform, @repository), :notice => t('flash.repository.projects_will_be_removed') + end + if params[:project_id] + ProjectToRepository.where(:project_id => params[:project_id], :repository_id => @repository.id).destroy_all + redirect_to platform_repository_path(@platform, @repository), :notice => t('flash.repository.project_removed') + end end def regenerate_metadata diff --git a/app/models/repository.rb b/app/models/repository.rb index e03452539..b4ef59437 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -28,6 +28,7 @@ class Repository < ActiveRecord::Base attr_accessible :name, :description, :publish_without_qa attr_readonly :name, :platform_id + attr_accessor :projects_list def regenerate(build_for_platform_id = nil) build_for_platform = Platform.main.find build_for_platform_id if platform.personal? @@ -57,6 +58,34 @@ class Repository < ActiveRecord::Base end later :clone_relations, :loner => true, :queue => :clone_build + def add_projects(list, user) + current_ability = Ability.new(user) + list.lines.each do |line| + begin + line.chomp!; line.strip! + owner, name = line.split('/') + next if owner.blank? || name.blank? + + project = Project.where(:owner_uname => owner, :name => name).accessible_by(current_ability, :read).first + projects << project if project + rescue RuntimeError, Exception + end + end + end + later :add_projects, :queue => :clone_build + + def remove_projects(list) + list.lines.each do |name| + begin + name.chomp!; name.strip! + next if name.blank? + project_to_repositories.where(:projects => { :name => name }).joins(:project).destroy_all + rescue RuntimeError, Exception + end + end + end + later :remove_projects, :queue => :clone_build + def full_clone(attrs = {}) base_clone(attrs).tap do |c| with_skip {c.save} and c.clone_relations(self) # later with resque diff --git a/app/views/platforms/repositories/projects_list.html.haml b/app/views/platforms/repositories/projects_list.html.haml index 26de0610c..4813b2ac0 100644 --- a/app/views/platforms/repositories/projects_list.html.haml +++ b/app/views/platforms/repositories/projects_list.html.haml @@ -1,6 +1,14 @@ = render 'submenu' = render 'sidebar' -%h3= raw "#{t("layout.repositories.add_project_to")}: #{link_to @repository.name, platform_repository_path(@platform, @repository)}" +%h3= raw "#{t("layout.repositories.add_projects_to")}: #{link_to @repository.name, platform_repository_path(@platform, @repository)}" + += form_for :repository, :url => add_project_platform_repository_path(@platform, @repository), :html => { :class => :form } do |f| + .leftlist= f.label :projects_list + .rightlist= f.text_area :projects_list + .both + .hr + .button_block + = submit_tag t('layout.add'), :data => {'disable-with' => t('layout.saving')} = render 'proj_list', :object => @projects diff --git a/app/views/platforms/repositories/remove_project.html.haml b/app/views/platforms/repositories/remove_project.html.haml new file mode 100644 index 000000000..3aacb9077 --- /dev/null +++ b/app/views/platforms/repositories/remove_project.html.haml @@ -0,0 +1,12 @@ += render 'submenu' += render 'sidebar' + +%h3= raw "#{t("layout.repositories.remove_projects_from")}: #{link_to @repository.name, platform_repository_path(@platform, @repository)}" + += form_for :repository, :url => remove_project_platform_repository_path(@platform, @repository), :html => { :class => :form } do |f| + .leftlist= f.label :projects_list + .rightlist= f.text_area :projects_list + .both + .hr + .button_block + = submit_tag t('layout.delete'), :data => {'disable-with' => t('layout.saving')} diff --git a/app/views/platforms/repositories/show.html.haml b/app/views/platforms/repositories/show.html.haml index 5bc491bfe..ddfba3750 100644 --- a/app/views/platforms/repositories/show.html.haml +++ b/app/views/platforms/repositories/show.html.haml @@ -16,5 +16,7 @@ %h3.fix= t("layout.projects.list_header") - if can? :add_project, @repository = link_to t("layout.projects.add"), add_project_platform_repository_path(@platform, @repository), :class => 'button' +- if can? :remove_project, @repository + = link_to t("layout.repositories.mass_delete"), remove_project_platform_repository_path(@platform, @repository), :class => 'button' = render 'proj_list' diff --git a/config/locales/models/repository.en.yml b/config/locales/models/repository.en.yml index 5feef3825..85be9cd2d 100644 --- a/config/locales/models/repository.en.yml +++ b/config/locales/models/repository.en.yml @@ -5,7 +5,8 @@ en: add_sync_lock_file: "Add '.sync.lock' file" remove_sync_lock_file: "Remove '.sync.lock' file" sync_lock_file_info: "Presence of '.sync.lock' file means that repository is not ready for sync with mirror" - add_project_to: Add project to repository + add_projects_to: Add projects to repository + remove_projects_from: Remove projets from repository edit: Settings list: List about: About repository @@ -23,6 +24,7 @@ en: clear_confirm: Are you sure you want to clear this platform? clear_warning: Attention! Cleared packages cannot be restored! regenerate_metadata: Regenerate metadata + mass_delete: Mass delete personal_repositories: settings_header: Settings @@ -34,7 +36,8 @@ en: repository: sync_lock_file_added: "'.sync.lock' file has been added to repository" sync_lock_file_removed: "'.sync.lock' file has been removed from repository" - + projects_will_be_removed: Projects added to queue for removing + projects_will_be_added: Projects added to queue for adding saved: Repository added updated: Repository updated @@ -57,6 +60,7 @@ en: repository: Repository attributes: repository: + projects_list: Projects list name: Name description: Description publish_without_qa: Publication without QA diff --git a/config/locales/models/repository.ru.yml b/config/locales/models/repository.ru.yml index 1042b0b51..a218a2806 100644 --- a/config/locales/models/repository.ru.yml +++ b/config/locales/models/repository.ru.yml @@ -5,7 +5,8 @@ ru: add_sync_lock_file: "Добавить '.sync.lock' файл" remove_sync_lock_file: "Удалить '.sync.lock' файл" sync_lock_file_info: "Наличие '.sync.lock' файла означает, что репозиторий не готов для синхронизации с зеркалом" - add_project_to: Добавить проект к репозиторию + add_projects_to: Добавить проекты к репозиторию + remove_projects_from: Удалить проекты из репозитория edit: Настройки list: Список about: О репозитории @@ -23,6 +24,7 @@ ru: clear_confirm: Уверены, что хотите очистить платформу? clear_warning: Внимание! Очищенные пакеты не могут быть восстановлены! regenerate_metadata: Регенерировать метаданные + mass_delete: Массовое удаление personal_repositories: settings_header: Настройки @@ -34,6 +36,8 @@ ru: repository: sync_lock_file_added: "'.sync.lock' файл добавлен в репозиторий" sync_lock_file_removed: "'.sync.lock' файл удален из репозитория" + projects_will_be_removed: Проекты добавлены в очередь на удаление + projects_will_be_added: Проекты добавлены в очередь на добавление saved: Репозиторий успешно добавлен updated: Репозиторий успешно обновлен @@ -56,6 +60,7 @@ ru: repository: Репозиторий attributes: repository: + projects_list: Список проектов name: Название description: Описание publish_without_qa: Публикация без участия QA diff --git a/config/routes.rb b/config/routes.rb index 23f0f1ef1..0bbaf8e0a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -192,6 +192,7 @@ Rosa::Application.routes.draw do resources :repositories do member do get :add_project + get :remove_project delete :remove_project get :projects_list post :remove_members # fixme: change post to delete