Makes all time entry columns available in the CSV export.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11442 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2013-02-19 20:41:54 +00:00
parent af92686c62
commit f8c649320f
4 changed files with 30 additions and 210 deletions

View File

@ -39,6 +39,7 @@ class TimelogController < ApplicationController
helper :custom_fields
include CustomFieldsHelper
helper :queries
include QueriesHelper
def index
@query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
@ -86,7 +87,7 @@ class TimelogController < ApplicationController
:include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
:order => sort_clause
)
send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
send_data(entries_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
}
end
end

View File

@ -86,44 +86,16 @@ module TimelogHelper
value)
end
def entries_to_csv(entries)
decimal_separator = l(:general_csv_decimal_separator)
custom_fields = TimeEntryCustomField.all
def entries_to_csv(entries, query, options={})
encoding = l(:general_csv_encoding)
columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
# csv header fields
headers = [l(:field_spent_on),
l(:field_user),
l(:field_activity),
l(:field_project),
l(:field_issue),
l(:field_tracker),
l(:field_subject),
l(:field_hours),
l(:field_comments)
]
# Export custom fields
headers += custom_fields.collect(&:name)
csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
c.to_s,
l(:general_csv_encoding) ) }
csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
# csv lines
entries.each do |entry|
fields = [format_date(entry.spent_on),
entry.user,
entry.activity,
entry.project,
(entry.issue ? entry.issue.id : nil),
(entry.issue ? entry.issue.tracker : nil),
(entry.issue ? entry.issue.subject : nil),
entry.hours.to_s.gsub('.', decimal_separator),
entry.comments
]
fields += custom_fields.collect {|f| show_value(entry.custom_field_values.detect {|v| v.custom_field_id == f.id}) }
csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
c.to_s,
l(:general_csv_encoding) ) }
csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, entry), encoding) }
end
end
export

View File

@ -22,8 +22,22 @@
<% other_formats_links do |f| %>
<%= f.link_to 'Atom', :url => params.merge({:issue_id => @issue, :key => User.current.rss_key}) %>
<%= f.link_to 'CSV', :url => params %>
<%= f.link_to 'CSV', :url => params, :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(params.merge({:format => 'csv',:page=>nil}), :method => :get, :id => 'csv-export-form') do %>
<p>
<label><%= radio_button_tag 'columns', '', true %> <%= l(:description_selected_columns) %></label><br />
<label><%= radio_button_tag 'columns', 'all' %> <%= l(:description_all_columns) %></label>
</p>
<p class="buttons">
<%= submit_tag l(:button_export), :name => nil, :onclick => "hideModal(this);" %>
<%= submit_tag l(:button_cancel), :name => nil, :onclick => "hideModal(this);", :type => 'button' %>
</p>
<% end %>
</div>
<% end %>
<% html_title l(:label_spent_time), l(:label_details) %>

View File

@ -22,7 +22,9 @@ class TimelogControllerTest < ActionController::TestCase
fixtures :projects, :enabled_modules, :roles, :members,
:member_roles, :issues, :time_entries, :users,
:trackers, :enumerations, :issue_statuses,
:custom_fields, :custom_values
:custom_fields, :custom_values,
:projects_trackers, :custom_fields_trackers,
:custom_fields_projects
include Redmine::I18n
@ -546,186 +548,17 @@ class TimelogControllerTest < ActionController::TestCase
assert assigns(:items).first.is_a?(TimeEntry)
end
def test_index_all_projects_csv_export
def test_index_csv_all_projects
Setting.date_format = '%m/%d/%Y'
get :index, :format => 'csv'
assert_response :success
assert_equal 'text/csv; header=present', @response.content_type
assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
assert @response.body.include?("\n04/21/2007,Redmine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
assert_equal 'text/csv; header=present', response.content_type
end
def test_index_csv_export
def test_index_csv
Setting.date_format = '%m/%d/%Y'
get :index, :project_id => 1, :format => 'csv'
assert_response :success
assert_equal 'text/csv; header=present', @response.content_type
assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
assert @response.body.include?("\n04/21/2007,Redmine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
end
def test_index_csv_export_with_multi_custom_field
field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'list',
:multiple => true, :possible_values => ['value1', 'value2'])
entry = TimeEntry.find(1)
entry.custom_field_values = {field.id => ['value1', 'value2']}
entry.save!
get :index, :project_id => 1, :format => 'csv'
assert_response :success
assert_include '"value1, value2"', @response.body
end
def test_csv_big_5
user = User.find_by_id(3)
user.language = "zh-TW"
assert user.save
str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
str_big5 = "\xa4@\xa4\xeb"
if str_utf8.respond_to?(:force_encoding)
str_utf8.force_encoding('UTF-8')
str_big5.force_encoding('Big5')
end
@request.session[:user_id] = 3
post :create, :project_id => 1,
:time_entry => {:comments => str_utf8,
# Not the default activity
:activity_id => '11',
:issue_id => '',
:spent_on => '2011-11-10',
:hours => '7.3'}
assert_redirected_to :action => 'index', :project_id => 'ecookbook'
t = TimeEntry.find_by_comments(str_utf8)
assert_not_nil t
assert_equal 11, t.activity_id
assert_equal 7.3, t.hours
assert_equal 3, t.user_id
get :index, :project_id => 1, :format => 'csv',
:from => '2011-11-10', :to => '2011-11-10'
assert_response :success
assert_equal 'text/csv; header=present', @response.content_type
ar = @response.body.chomp.split("\n")
s1 = "\xa4\xe9\xb4\xc1"
if str_utf8.respond_to?(:force_encoding)
s1.force_encoding('Big5')
end
assert ar[0].include?(s1)
assert ar[1].include?(str_big5)
end
def test_csv_cannot_convert_should_be_replaced_big_5
user = User.find_by_id(3)
user.language = "zh-TW"
assert user.save
str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
if str_utf8.respond_to?(:force_encoding)
str_utf8.force_encoding('UTF-8')
end
@request.session[:user_id] = 3
post :create, :project_id => 1,
:time_entry => {:comments => str_utf8,
# Not the default activity
:activity_id => '11',
:issue_id => '',
:spent_on => '2011-11-10',
:hours => '7.3'}
assert_redirected_to :action => 'index', :project_id => 'ecookbook'
t = TimeEntry.find_by_comments(str_utf8)
assert_not_nil t
assert_equal 11, t.activity_id
assert_equal 7.3, t.hours
assert_equal 3, t.user_id
get :index, :project_id => 1, :format => 'csv',
:from => '2011-11-10', :to => '2011-11-10'
assert_response :success
assert_equal 'text/csv; header=present', @response.content_type
ar = @response.body.chomp.split("\n")
s1 = "\xa4\xe9\xb4\xc1"
if str_utf8.respond_to?(:force_encoding)
s1.force_encoding('Big5')
end
assert ar[0].include?(s1)
s2 = ar[1].split(",")[8]
if s2.respond_to?(:force_encoding)
s3 = "\xa5H?"
s3.force_encoding('Big5')
assert_equal s3, s2
elsif RUBY_PLATFORM == 'java'
assert_equal "??", s2
else
assert_equal "\xa5H???", s2
end
end
def test_csv_tw
with_settings :default_language => "zh-TW" do
str1 = "test_csv_tw"
user = User.find_by_id(3)
te1 = TimeEntry.create(:spent_on => '2011-11-10',
:hours => 999.9,
:project => Project.find(1),
:user => user,
:activity => TimeEntryActivity.find_by_name('Design'),
:comments => str1)
te2 = TimeEntry.find_by_comments(str1)
assert_not_nil te2
assert_equal 999.9, te2.hours
assert_equal 3, te2.user_id
get :index, :project_id => 1, :format => 'csv',
:from => '2011-11-10', :to => '2011-11-10'
assert_response :success
assert_equal 'text/csv; header=present', @response.content_type
ar = @response.body.chomp.split("\n")
s2 = ar[1].split(",")[7]
assert_equal '999.9', s2
str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
if str_tw.respond_to?(:force_encoding)
str_tw.force_encoding('UTF-8')
end
assert_equal str_tw, l(:general_lang_name)
assert_equal ',', l(:general_csv_separator)
assert_equal '.', l(:general_csv_decimal_separator)
end
end
def test_csv_fr
with_settings :default_language => "fr" do
str1 = "test_csv_fr"
user = User.find_by_id(3)
te1 = TimeEntry.create(:spent_on => '2011-11-10',
:hours => 999.9,
:project => Project.find(1),
:user => user,
:activity => TimeEntryActivity.find_by_name('Design'),
:comments => str1)
te2 = TimeEntry.find_by_comments(str1)
assert_not_nil te2
assert_equal 999.9, te2.hours
assert_equal 3, te2.user_id
get :index, :project_id => 1, :format => 'csv',
:from => '2011-11-10', :to => '2011-11-10'
assert_response :success
assert_equal 'text/csv; header=present', @response.content_type
ar = @response.body.chomp.split("\n")
s2 = ar[1].split(";")[7]
assert_equal '999,9', s2
str_fr = "Fran\xc3\xa7ais"
if str_fr.respond_to?(:force_encoding)
str_fr.force_encoding('UTF-8')
end
assert_equal str_fr, l(:general_lang_name)
assert_equal ';', l(:general_csv_separator)
assert_equal ',', l(:general_csv_decimal_separator)
end
assert_equal 'text/csv; header=present', response.content_type
end
end