Project

General

Profile

Feature #1459 » 0002-Import-and-export-non-working-dates-as-CSV-files.patch

Go MAEDA, 2026-10-08 10:29

View differences:

app/assets/stylesheets/application.css
stroke: var(--oc-yellow-8);
}
.icon-info svg.icon-svg {
stroke: var(--oc-indigo-9);
}
.icon-only span {
display: none;
}
app/controllers/non_working_dates_controller.rb
self.main_menu = false
menu_item :settings
include NonWorkingDatesHelper
before_action :require_admin
before_action :find_non_working_date, :only => [:edit, :update, :destroy]
def index
@year = params[:year].to_i if /\A\d{4}\z/.match?(params[:year].to_s)
@year ||= User.current.today.year
years = NonWorkingDate.years
@years = (years | [@year, User.current.today.year]).sort
@previous_year = @year - 1 if years.any? {|year| year < @year}
@next_year = @year + 1 if years.any? {|year| year > @year}
@non_working_dates = NonWorkingDate.in_year(@year).sorted.to_a
@non_working_date_count = NonWorkingDate.count
respond_to do |format|
format.html do
@year = params[:year].to_i if /\A\d{4}\z/.match?(params[:year].to_s)
@year ||= User.current.today.year
years = NonWorkingDate.years
@years = (years | [@year, User.current.today.year]).sort
@previous_year = @year - 1 if years.any? {|year| year < @year}
@next_year = @year + 1 if years.any? {|year| year > @year}
@non_working_dates = NonWorkingDate.in_year(@year).sorted.to_a
@non_working_date_count = NonWorkingDate.count
end
format.csv do
send_data(non_working_dates_to_csv(NonWorkingDate.sorted), :type => 'text/csv; header=present', :filename => 'non_working_dates.csv')
end
end
end
def new
app/helpers/non_working_dates_helper.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module NonWorkingDatesHelper
def non_working_dates_to_csv(non_working_dates)
Redmine::Export::CSV.generate(:encoding => params[:encoding], :field_separator => params[:field_separator]) do |csv|
csv << [l(:field_date), l(:field_name)]
non_working_dates.each do |non_working_date|
csv << [format_date(non_working_date.date), non_working_date.name]
end
end
end
end
app/models/non_working_date_import.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class NonWorkingDateImport < Import
AUTO_MAPPABLE_FIELDS = {
'date' => 'field_date',
'name' => 'field_name'
}
def self.menu_item
:settings
end
def self.layout
'admin'
end
def self.authorized?(user)
user.admin?
end
# Returns the objects that were imported
def saved_objects
NonWorkingDate.where(:id => saved_items.pluck(:obj_id)).sorted
end
def mappable_custom_fields
[]
end
private
def build_object(row, item)
NonWorkingDate.new(
:date => row_date(row, 'date'),
:name => row_value(row, 'name')
)
end
end
app/views/imports/_non_working_dates_mapping.html.erb
<fieldset class="box tabular">
<legend><%= l(:label_fields_mapping) %></legend>
<p>
<label for="import_mapping_date"><%= l(:field_date) %></label>
<%= mapping_select_tag @import, 'date', :required => true %>
</p>
<p>
<label for="import_mapping_name"><%= l(:field_name) %></label>
<%= mapping_select_tag @import, 'name' %>
</p>
</fieldset>
app/views/imports/_non_working_dates_saved_objects.html.erb
<table id="saved-items" class="list">
<thead>
<tr>
<th><%= t(:field_date) %></th>
<th><%= t(:field_name) %></th>
</tr>
</thead>
<tbody>
<% saved_objects.each do |non_working_date| %>
<tr>
<td><%= link_to format_date(non_working_date.date), edit_non_working_date_path(non_working_date) %></td>
<td><%= non_working_date.name %></td>
</tr>
<% end %>
</tbody>
</table>
<p><%= link_to l(:label_non_working_date_plural), non_working_dates_path(:year => saved_objects.first.date.year) %></p>
app/views/non_working_dates/index.html.erb
<div class="contextual">
<%= link_to(sprite_icon('add', l(:label_non_working_date_new)), new_non_working_date_path, :class => 'icon icon-add') %>
<%= link_to(sprite_icon('import', l(:button_import)), new_non_working_dates_import_path, :class => 'icon icon-import') %>
</div>
<h2><%= l(:label_non_working_date_plural) %></h2>
......
<p class="nodata"><%= l(:label_no_data) %></p>
<% end %>
<% if @non_working_date_count > 0 %>
<% other_formats_links do |f| %>
<%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
<% end %>
<div id="csv-export-options" style="display: none;">
<h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
<%= form_tag(non_working_dates_path(:format => 'csv'), :method => :get, :id => 'csv-export-form') do %>
<%= export_csv_encoding_select_tag %>
<%= export_csv_separator_select_tag %>
<p class="icon icon-info"><%= sprite_icon('help', l(:text_non_working_dates_export_all_years)) %></p>
<p class="buttons">
<%= submit_tag l(:button_export), :name => nil, :onclick => 'hideModal(this);', :data => {:disable_with => false} %>
<%= link_to_function l(:button_cancel), 'hideModal(this);' %>
</p>
<% end %>
</div>
<% end %>
<% html_title(l(:label_non_working_date_plural)) -%>
config/locales/en.yml
text_repository_identifier_info: 'Only lower case letters (a-z), numbers, dashes and underscores are allowed.<br />Once saved, the identifier cannot be changed.'
text_login_required_html: When not requiring authentication, public projects and their contents are openly available on the network. You can <a href="%{anonymous_role_path}">edit the applicable permissions</a>.
text_non_working_dates_html: 'You can add non-working dates such as holidays: <a href="%{non_working_dates_path}">Manage non-working dates</a>'
text_non_working_dates_export_all_years: Non-working dates of all years will be exported.
label_login_required_yes: "Yes"
label_login_required_no: "No, allow anonymous access to public projects"
text_project_is_public_non_member: Public projects and their contents are available to all logged-in users.
text_project_is_public_anonymous: Public projects and their contents are openly available on the network.
label_import_time_entries: Import time entries
label_import_users: Import users
label_import_non_working_dates: Import non-working dates
sudo_mode_new_info_html: <strong>What's happening?</strong> You need to reconfirm your password before taking any administrative actions, this ensures your account stays protected.
twofa__totp__name: Authenticator app
config/routes.rb
:defaults => {:type => 'TimeEntryImport'}, :as => 'new_time_entries_import'
get '/users/imports/new', :to => 'imports#new',
:defaults => {:type => 'UserImport'}, :as => 'new_users_import'
get '/non_working_dates/imports/new', :to => 'imports#new',
:defaults => {:type => 'NonWorkingDateImport'}, :as => 'new_non_working_dates_import'
post '/imports', :to => 'imports#create', :as => 'imports'
get '/imports/:id', :to => 'imports#show', :as => 'import'
match '/imports/:id/settings', :to => 'imports#settings', :via => [:get, :post], :as => 'import_settings'
test/fixtures/files/import_non_working_dates.csv
date,name
2026-06-25,Company Foundation Day
2026-08-13,Summer Shutdown
2026-12-30,Year-end Shutdown
test/functional/imports_controller_test.rb
assert_select 'table#unsaved-items'
assert_select 'table#unsaved-items tbody tr', import.unsaved_items.count
end
def test_import_non_working_dates
@request.session[:user_id] = 1
import = NonWorkingDateImport.new
import.user_id = 1
import.file = uploaded_test_file('import_non_working_dates.csv', 'text/csv')
import.settings = {
'separator' => ',', 'wrapper' => '"', 'encoding' => 'UTF-8', 'date_format' => '%Y-%m-%d',
'mapping' => {'date' => '0', 'name' => '1'}
}
import.save!
get(:mapping, :params => {:id => import.to_param})
assert_response :success
assert_select 'select[name=?]', 'import_settings[mapping][date]'
assert_select 'select[name=?]', 'import_settings[mapping][name]'
assert_difference 'NonWorkingDate.count', 3 do
post(:run, :params => {:id => import.to_param})
end
assert_redirected_to "/imports/#{import.to_param}"
get(:show, :params => {:id => import.to_param})
assert_response :success
assert_select 'table#saved-items tbody tr', 3
assert_select 'a[href=?]', '/non_working_dates?year=2026', :text => 'Non-working dates'
end
end
test/functional/non_working_dates_controller_test.rb
assert_select 'table.non_working_dates td.name', :text => 'Company Foundation Day'
assert_select 'form[data-cm-url=?] input[name=?]', '/non_working_dates/context_menu', 'ids[]', 1
assert_select 'span.pagination span.items', :text => '(1/2)'
assert_select '#csv-export-options p', :text => 'Non-working dates of all years will be exported.'
assert_select 'form.non-working-dates-year' do
assert_select 'select[name=year] option', 2
assert_select 'select[name=year] option[selected=selected]', :text => year.to_s
......
assert_select 'table.non_working_dates td.name', :text => 'Year-end Shutdown'
end
def test_index_csv_should_export_non_working_dates_of_all_years
NonWorkingDate.create!(:date => '2021-06-25', :name => 'Company Foundation Day')
NonWorkingDate.create!(:date => '2020-12-30')
with_settings :date_format => '%Y-%m-%d' do
get :index, :params => {:year => '2021', :format => 'csv'}
end
assert_response :success
assert_equal 'text/csv; header=present', @response.media_type
assert_equal ['Date,Name', '2020-12-30,', '2021-06-25,Company Foundation Day'], @response.body.lines.map(&:chomp)
end
def test_index_by_non_admin_should_respond_with_403
@request.session[:user_id] = 2
get :index
test/unit/non_working_date_import_test.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require_relative '../test_helper'
class NonWorkingDateImportTest < ActiveSupport::TestCase
include Redmine::I18n
def setup
set_language_if_valid 'en'
User.current = nil
end
def test_run_should_import_dates_and_names
import = generate_import_with_mapping
first, second, third = new_records(NonWorkingDate, 3) {import.run}
assert_equal Date.new(2026, 6, 25), first.date
assert_equal 'Company Foundation Day', first.name
assert_equal Date.new(2026, 8, 13), second.date
assert_equal 'Summer Shutdown', second.name
assert_equal Date.new(2026, 12, 30), third.date
assert_equal 'Year-end Shutdown', third.name
end
def test_run_should_not_import_already_registered_dates
NonWorkingDate.create!(:date => '2026-08-13', :name => 'Existing')
import = generate_import_with_mapping
assert_difference 'NonWorkingDate.count', 2 do
import.run
end
assert_equal 'Existing', NonWorkingDate.find_by(:date => '2026-08-13').name
item = import.unsaved_items.first
assert_equal 2, item.position
assert_include 'Date has already been taken', item.message
end
protected
def generate_import_with_mapping
import = NonWorkingDateImport.new
import.user_id = 1
import.file = uploaded_test_file('import_non_working_dates.csv', 'text/csv')
import.settings = {
'separator' => ',', 'wrapper' => '"', 'encoding' => 'UTF-8', 'date_format' => '%Y-%m-%d',
'mapping' => {'date' => '0', 'name' => '1'}
}
import.save!
import
end
end
(8-8/8)