Skip to content

Commit

Permalink
Fixes #21378 - Add CSV export on tasks index
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrisker committed Nov 5, 2017
1 parent 05a1c7e commit 96466f1
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 23 deletions.
9 changes: 9 additions & 0 deletions app/controllers/foreman_tasks/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ForemanTasks
class TasksController < ::ApplicationController
include Foreman::Controller::AutoCompleteSearch
include Foreman::Controller::CsvResponder

before_action :restrict_dangerous_actions, :only => [:unlock, :force_unlock]

Expand All @@ -11,6 +12,14 @@ def show
def index
params[:order] ||= 'started_at DESC'
@tasks = filter(resource_base)
respond_to do |format|
format.html do
render :index
end
format.csv do
csv_response(@tasks, [:to_label, :state, :result, 'started_at.in_time_zone', 'ended_at.in_time_zone', :username], ['Action', 'State', 'Result', 'Started At', 'Ended At', 'User'])
end
end
end

def sub_tasks
Expand Down
14 changes: 2 additions & 12 deletions app/helpers/foreman_tasks/tasks_helper.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
module ForemanTasks
module TasksHelper
def format_task_input(task, include_action = false)
return '-' unless task
parts = []
parts << task.get_humanized(:name) if include_action
parts << Array(task.get_humanized(:input)).map do |part|
if part.is_a? Array
part[1][:text]
else
part.to_s
end
end.join('; ')
parts.join(' ')
def format_task_input(task)
task ? task.to_label : '-'
end

def format_recurring_logic_limit(thing)
Expand Down
13 changes: 13 additions & 0 deletions app/models/foreman_tasks/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ def sub_tasks_counts
result.symbolize_keys
end

def to_label
parts = []
parts << get_humanized(:name)
parts << Array(get_humanized(:input)).map do |part|
if part.is_a? Array
part[1][:text]
else
part.to_s
end
end.join('; ')
parts.join(' ')
end

protected

def generate_id
Expand Down
2 changes: 1 addition & 1 deletion app/views/foreman_tasks/recurring_logics/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<tr>
<td><%= link_to(recurring_logic.cron_line, foreman_tasks_recurring_logic_path(recurring_logic)) %></td>
<td><%= link_to(recurring_logic.tasks.count, foreman_tasks_tasks_url(:search => "task_group.id = #{recurring_logic.task_group.id}")) %></td>
<td><%= format_task_input(recurring_logic.tasks.first, true) %></td>
<td><%= format_task_input(recurring_logic.tasks.first) %></td>
<td><%= recurring_logic.tasks.order(:started_at).where('started_at IS NOT NULL').last.try(:started_at) || "-" %></td>
<td><%= recurring_logic_next_occurrence recurring_logic %></td>
<td><%= recurring_logic.iteration %></td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</tr>
<tr>
<th><%= N_("Action") %></th>
<td><%= format_task_input(recurring_logic.tasks.last, true) %></td>
<td><%= format_task_input(recurring_logic.tasks.last) %></td>
</tr>
<tr>
<th><%= N_("Last occurrence") %></th>
Expand Down
3 changes: 1 addition & 2 deletions app/views/foreman_tasks/tasks/_details.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@
<div class="col-md-6">
<div>
<span class="param-name list-group-item-heading"><%= _("Name") %>:</span>
<% task_label = format_task_input(@task, true) %>
<span class="param-value" data-original-title="<%= task_label %>" rel="twipsy"> <%= truncate(task_label, :length => 50) %></span>
<span class="param-value" data-original-title="<%= @task.to_label %>" rel="twipsy"> <%= truncate(@task.to_label, :length => 50) %></span>
</div>
<div>
<span class="param-name list-group-item-heading"><%= _("Result") %>:</span>
Expand Down
4 changes: 2 additions & 2 deletions app/views/foreman_tasks/tasks/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% title _("Tasks") %>
<% title_actions SETTINGS[:version].short <= '1.13' ? help_path : help_button %>
<% title_actions csv_link, help_button %>
<% stylesheet 'foreman_tasks/tasks' %>

<script>
Expand Down Expand Up @@ -27,7 +27,7 @@ $(document).on('click', ".table-two-pane td.two-pane-link", function(e) {
<% for task in @tasks %>
<tr>
<td class="task-id two-pane-link ellipsis">
<%= link_to_if_authorized(format_task_input(task, true),
<%= link_to_if_authorized(task.to_label,
hash_for_foreman_tasks_task_path(:id => task)) %>
</td>
<td class="ellipsis"><%= task.state %></td>
Expand Down
8 changes: 3 additions & 5 deletions test/helpers/foreman_tasks/tasks_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ class TasksHelperTest < ActionView::TestCase
end

it 'formats the task input properly' do
format_task_input(@task).must_equal("user 'Anonymous Admin'")
format_task_input(@task, true).must_equal("Create user 'Anonymous Admin'")
format_task_input(@task).must_equal("Create user 'Anonymous Admin'")
end

it 'displays the dash if task is nil' do
format_task_input(nil, true).must_equal('-')
format_task_input(nil).must_equal('-')
end
end

Expand All @@ -36,8 +35,7 @@ class TasksHelperTest < ActionView::TestCase

it 'formats the task input properly' do
response = "product 'product-2'; organization 'test-0'"
format_task_input(@task).must_equal(response)
format_task_input(@task, true).must_equal("Create #{response}")
format_task_input(@task).must_equal("Create #{response}")
end
end
end
Expand Down

0 comments on commit 96466f1

Please sign in to comment.