users_export7.patch

Mizuki ISHIKAWA, 2018-07-17 07:43

Download (6.44 KB)

View differences:

app/controllers/users_controller.rb
28 28
  include SortHelper
29 29
  helper :custom_fields
30 30
  include CustomFieldsHelper
31
  include UsersHelper
31 32
  helper :principal_memberships
32 33
  helper :activities
33 34
  include ActivitiesHelper
......
61 62
        @groups = Group.givable.sort
62 63
        render :layout => !request.xhr?
63 64
      }
65
      format.csv {
66
        send_data(users_to_csv(scope.order(sort_clause)), :type => 'text/csv; header=present', :filename => 'users.csv')
67
      }
64 68
      format.api
65 69
    end
66 70
  end
app/helpers/users_helper.rb
18 18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 19

  
20 20
module UsersHelper
21
  include ApplicationHelper
22

  
21 23
  def users_status_options_for_select(selected)
22 24
    user_count_by_status = User.group('status').count.to_hash
23
    options_for_select([[l(:label_all), ''],
24
                        ["#{l(:status_active)} (#{user_count_by_status[1].to_i})", '1'],
25
                        ["#{l(:status_registered)} (#{user_count_by_status[2].to_i})", '2'],
26
                        ["#{l(:status_locked)} (#{user_count_by_status[3].to_i})", '3']], selected.to_s)
25
    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)
27 26
  end
28 27

  
29 28
  def user_mail_notification_options(user)
......
61 60
    end
62 61
    tabs
63 62
  end
63

  
64
  def users_to_csv(users)
65
    Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
66
      columns = [
67
        'login',
68
        'firstname',
69
        'lastname',
70
        'mail',
71
        'admin',
72
        'created_on',
73
        'last_login_on',
74
        'status'
75
      ]
76

  
77
      # csv header fields
78
      csv << columns.map{|column| l('field_' + column)}
79
      # csv lines
80
      users.each do |user|
81
        csv << columns.map do |column|
82
          if column == 'status'
83
            l(("status_#{User::LABEL_BY_STATUS[user.status]}"))
84
          else
85
            format_object(user.send(column), false)
86
          end
87
        end
88
      end
89
    end
90
  end
64 91
end
app/views/users/index.html.erb
4 4

  
5 5
<h2><%=l(:label_user_plural)%></h2>
6 6

  
7
<%= form_tag(users_path, :method => :get) do %>
7
<%= form_tag(users_path, { :method => :get, :id => 'users_form' }) do %>
8 8
<fieldset><legend><%= l(:label_filter_plural) %></legend>
9 9
<label for='status'><%= l(:field_status) %>:</label>
10 10
<%= select_tag 'status', users_status_options_for_select(@status), :class => "small", :onchange => "this.form.submit(); return false;"  %>
......
19 19
<%= submit_tag l(:button_apply), :class => "small", :name => nil %>
20 20
<%= link_to l(:button_clear), users_path, :class => 'icon icon-reload' %>
21 21
</fieldset>
22
<%= hidden_field_tag 'encoding', l(:general_csv_encoding) unless l(:general_csv_encoding).casecmp('UTF-8') == 0 %>
22 23
<% end %>
23 24
&nbsp;
24 25

  
......
55 56
</table>
56 57
</div>
57 58
<span class="pagination"><%= pagination_links_full @user_pages, @user_count %></span>
59
<% other_formats_links do |f| %>
60
  <%= f.link_to_with_query_parameters 'CSV', {}, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
61
<% end %>
62
<div id="csv-export-options" style="display: none;">
63
  <h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
64
  <%= export_csv_encoding_select_tag %>
65
  <p class="buttons">
66
    <%= submit_tag l(:button_export), :name => nil, :id => 'csv-export-button' %>
67
    <%= submit_tag l(:button_cancel), :name => nil, :onclick => 'hideModal(this);', :type => 'button' %>
68
  </p>
69
</div>
70
<%= javascript_tag do %>
71
$(document).ready(function(){
72
  $('input#csv-export-button').click(function(){
73
    $('form input#encoding').val($('select#encoding option:selected').val());
74
    $('form#users_form').attr('action', "<%= users_path(:format => 'csv') %>").submit();
75
    $('form#users_form').attr('action', '<%= users_path %>');
76
    hideModal(this);
77
  });
78
});
79
<% end %>
58 80
<% else %>
59 81
<p class="nodata"><%= l(:label_no_data) %></p>
60 82
<% end %>
test/functional/users_controller_test.rb
64 64
    end
65 65
  end
66 66

  
67
  def test_index_csv
68
    with_settings :default_language => 'en' do
69
      get :index, :params => { :format => 'csv' }
70
      assert_response :success
71

  
72
      assert_equal User.logged.status(1).count, response.body.chomp.split("\n").size - 1
73
      assert_include 'active', response.body
74
      assert_not_include 'locked', response.body
75
      assert_equal 'text/csv; header=present', @response.content_type
76
    end
77
  end
78

  
79
  def test_index_csv_with_status_filter
80
    with_settings :default_language => 'en' do
81
      get :index, :params => { :status => 3, :format => 'csv' }
82
      assert_response :success
83

  
84
      assert_equal User.logged.status(3).count, response.body.chomp.split("\n").size - 1
85
      assert_include 'locked', response.body
86
      assert_not_include 'active', response.body
87
      assert_equal 'text/csv; header=present', @response.content_type
88
    end
89
  end
90

  
91
  def test_index_csv_with_name_filter
92
    get :index, :params => {:name => 'John', :format => 'csv'}
93
    assert_response :success
94

  
95
    assert_equal User.logged.like('John').count, response.body.chomp.split("\n").size - 1
96
    assert_include 'John', response.body
97
    assert_equal 'text/csv; header=present', @response.content_type
98
  end
99

  
100
  def test_index_csv_with_group_filter
101
    get :index, :params => {:group_id => '10', :format => 'csv'}
102
    assert_response :success
103

  
104
    assert_equal Group.find(10).users.count, response.body.chomp.split("\n").size - 1
105
    assert_equal 'text/csv; header=present', @response.content_type
106
  end
107

  
67 108
  def test_show
68 109
    @request.session[:user_id] = nil
69 110
    get :show, :params => {:id => 2}