mirror of
https://github.com/redmine/redmine.git
synced 2025-11-15 17:56:03 +01:00
Export users list to CSV (#5957).
Contriubted by Mizuki ISHIKAWA and Yuki Kita. git-svn-id: http://svn.redmine.org/redmine/trunk@17463 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -28,6 +28,7 @@ class UsersController < ApplicationController
|
|||||||
include SortHelper
|
include SortHelper
|
||||||
helper :custom_fields
|
helper :custom_fields
|
||||||
include CustomFieldsHelper
|
include CustomFieldsHelper
|
||||||
|
include UsersHelper
|
||||||
helper :principal_memberships
|
helper :principal_memberships
|
||||||
helper :activities
|
helper :activities
|
||||||
include ActivitiesHelper
|
include ActivitiesHelper
|
||||||
@@ -61,6 +62,9 @@ class UsersController < ApplicationController
|
|||||||
@groups = Group.givable.sort
|
@groups = Group.givable.sort
|
||||||
render :layout => !request.xhr?
|
render :layout => !request.xhr?
|
||||||
}
|
}
|
||||||
|
format.csv {
|
||||||
|
send_data(users_to_csv(scope.order(sort_clause)), :type => 'text/csv; header=present', :filename => 'users.csv')
|
||||||
|
}
|
||||||
format.api
|
format.api
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,12 +18,11 @@
|
|||||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
|
||||||
module UsersHelper
|
module UsersHelper
|
||||||
|
include ApplicationHelper
|
||||||
|
|
||||||
def users_status_options_for_select(selected)
|
def users_status_options_for_select(selected)
|
||||||
user_count_by_status = User.group('status').count.to_hash
|
user_count_by_status = User.group('status').count.to_hash
|
||||||
options_for_select([[l(:label_all), ''],
|
options_for_select([[l(:label_all), '']] + (User.valid_statuses.map {|c| ["#{l('status_' + User::LABEL_BY_STATUS[c])} (#{user_count_by_status[c].to_i})", c]}), selected.to_s)
|
||||||
["#{l(:status_active)} (#{user_count_by_status[1].to_i})", '1'],
|
|
||||||
["#{l(:status_registered)} (#{user_count_by_status[2].to_i})", '2'],
|
|
||||||
["#{l(:status_locked)} (#{user_count_by_status[3].to_i})", '3']], selected.to_s)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_mail_notification_options(user)
|
def user_mail_notification_options(user)
|
||||||
@@ -61,4 +60,32 @@ module UsersHelper
|
|||||||
end
|
end
|
||||||
tabs
|
tabs
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def users_to_csv(users)
|
||||||
|
Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
|
||||||
|
columns = [
|
||||||
|
'login',
|
||||||
|
'firstname',
|
||||||
|
'lastname',
|
||||||
|
'mail',
|
||||||
|
'admin',
|
||||||
|
'created_on',
|
||||||
|
'last_login_on',
|
||||||
|
'status'
|
||||||
|
]
|
||||||
|
|
||||||
|
# csv header fields
|
||||||
|
csv << columns.map{|column| l('field_' + column)}
|
||||||
|
# csv lines
|
||||||
|
users.each do |user|
|
||||||
|
csv << columns.map do |column|
|
||||||
|
if column == 'status'
|
||||||
|
l(("status_#{User::LABEL_BY_STATUS[user.status]}"))
|
||||||
|
else
|
||||||
|
format_object(user.send(column), false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<h2><%=l(:label_user_plural)%></h2>
|
<h2><%=l(:label_user_plural)%></h2>
|
||||||
|
|
||||||
<%= form_tag(users_path, :method => :get) do %>
|
<%= form_tag(users_path, { :method => :get, :id => 'users_form' }) do %>
|
||||||
<fieldset><legend><%= l(:label_filter_plural) %></legend>
|
<fieldset><legend><%= l(:label_filter_plural) %></legend>
|
||||||
<label for='status'><%= l(:field_status) %>:</label>
|
<label for='status'><%= l(:field_status) %>:</label>
|
||||||
<%= select_tag 'status', users_status_options_for_select(@status), :class => "small", :onchange => "this.form.submit(); return false;" %>
|
<%= select_tag 'status', users_status_options_for_select(@status), :class => "small", :onchange => "this.form.submit(); return false;" %>
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
<%= submit_tag l(:button_apply), :class => "small", :name => nil %>
|
<%= submit_tag l(:button_apply), :class => "small", :name => nil %>
|
||||||
<%= link_to l(:button_clear), users_path, :class => 'icon icon-reload' %>
|
<%= link_to l(:button_clear), users_path, :class => 'icon icon-reload' %>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
<%= hidden_field_tag 'encoding', l(:general_csv_encoding) unless l(:general_csv_encoding).casecmp('UTF-8') == 0 %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
|
||||||
@@ -55,6 +56,27 @@
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
<span class="pagination"><%= pagination_links_full @user_pages, @user_count %></span>
|
<span class="pagination"><%= pagination_links_full @user_pages, @user_count %></span>
|
||||||
|
<% 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>
|
||||||
|
<%= export_csv_encoding_select_tag %>
|
||||||
|
<p class="buttons">
|
||||||
|
<%= submit_tag l(:button_export), :name => nil, :id => 'csv-export-button' %>
|
||||||
|
<%= submit_tag l(:button_cancel), :name => nil, :onclick => 'hideModal(this);', :type => 'button' %>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<%= javascript_tag do %>
|
||||||
|
$(document).ready(function(){
|
||||||
|
$('input#csv-export-button').click(function(){
|
||||||
|
$('form input#encoding').val($('select#encoding option:selected').val());
|
||||||
|
$('form#users_form').attr('action', "<%= users_path(:format => 'csv') %>").submit();
|
||||||
|
$('form#users_form').attr('action', '<%= users_path %>');
|
||||||
|
hideModal(this);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
<% end %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<p class="nodata"><%= l(:label_no_data) %></p>
|
<p class="nodata"><%= l(:label_no_data) %></p>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -64,6 +64,47 @@ class UsersControllerTest < Redmine::ControllerTest
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_index_csv
|
||||||
|
with_settings :default_language => 'en' do
|
||||||
|
get :index, :params => { :format => 'csv' }
|
||||||
|
assert_response :success
|
||||||
|
|
||||||
|
assert_equal User.logged.status(1).count, response.body.chomp.split("\n").size - 1
|
||||||
|
assert_include 'active', response.body
|
||||||
|
assert_not_include 'locked', response.body
|
||||||
|
assert_equal 'text/csv; header=present', @response.content_type
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_index_csv_with_status_filter
|
||||||
|
with_settings :default_language => 'en' do
|
||||||
|
get :index, :params => { :status => 3, :format => 'csv' }
|
||||||
|
assert_response :success
|
||||||
|
|
||||||
|
assert_equal User.logged.status(3).count, response.body.chomp.split("\n").size - 1
|
||||||
|
assert_include 'locked', response.body
|
||||||
|
assert_not_include 'active', response.body
|
||||||
|
assert_equal 'text/csv; header=present', @response.content_type
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_index_csv_with_name_filter
|
||||||
|
get :index, :params => {:name => 'John', :format => 'csv'}
|
||||||
|
assert_response :success
|
||||||
|
|
||||||
|
assert_equal User.logged.like('John').count, response.body.chomp.split("\n").size - 1
|
||||||
|
assert_include 'John', response.body
|
||||||
|
assert_equal 'text/csv; header=present', @response.content_type
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_index_csv_with_group_filter
|
||||||
|
get :index, :params => {:group_id => '10', :format => 'csv'}
|
||||||
|
assert_response :success
|
||||||
|
|
||||||
|
assert_equal Group.find(10).users.count, response.body.chomp.split("\n").size - 1
|
||||||
|
assert_equal 'text/csv; header=present', @response.content_type
|
||||||
|
end
|
||||||
|
|
||||||
def test_show
|
def test_show
|
||||||
@request.session[:user_id] = nil
|
@request.session[:user_id] = nil
|
||||||
get :show, :params => {:id => 2}
|
get :show, :params => {:id => 2}
|
||||||
|
|||||||
Reference in New Issue
Block a user