Skip to content

Commit 8ffa6f9

Browse files
committed
add bulk delete endpoints
1 parent d0803a9 commit 8ffa6f9

File tree

10 files changed

+229
-5
lines changed

10 files changed

+229
-5
lines changed

lib/superset/chart/bulk_delete.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
# TODO: the gui delete has a confirmation step, this does not. Potentially add a confirm_delete parameter to the constructor
3+
# that would ensure that all charts belong to an expected dashboard before deleting. ( not sure if this is a good idea )
4+
5+
6+
module Superset
7+
module Chart
8+
class BulkDelete < Superset::Request
9+
attr_reader :chart_ids
10+
11+
def initialize(chart_ids: [])
12+
@chart_ids = chart_ids
13+
end
14+
15+
def perform
16+
raise InvalidParameterError, "chart_ids array of integers expected" unless chart_ids.is_a?(Array)
17+
raise InvalidParameterError, "chart_ids array must contin Integer only values" unless chart_ids.all? { |item| item.is_a?(Integer) }
18+
19+
logger.info("Attempting to delete charts with id: #{chart_ids.join(', ')}")
20+
response
21+
end
22+
23+
def response
24+
@response ||= client.delete(route, params)
25+
end
26+
27+
private
28+
29+
def params
30+
{ q: "!(#{chart_ids.join(',')})" }
31+
end
32+
33+
def route
34+
"chart/"
35+
end
36+
end
37+
end
38+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
# TODO: the gui delete has a confirmation step, this does not. Potentially add a confirm_delete parameter to the constructor
3+
# that would ensure that no charts belong to a dashboard before deleting
4+
5+
module Superset
6+
module Dashboard
7+
class BulkDelete < Superset::Request
8+
attr_reader :dashboard_ids
9+
10+
def initialize(dashboard_ids: [])
11+
@dashboard_ids = dashboard_ids
12+
end
13+
14+
def perform
15+
raise InvalidParameterError, "dashboard_ids array of integers expected" unless dashboard_ids.is_a?(Array)
16+
raise InvalidParameterError, "dashboard_ids array must contin Integer only values" unless dashboard_ids.all? { |item| item.is_a?(Integer) }
17+
18+
logger.info("Attempting to delete dashboards with id: #{dashboard_ids.join(', ')}")
19+
response
20+
end
21+
22+
def response
23+
@response ||= client.delete(route, params)
24+
end
25+
26+
private
27+
28+
def params
29+
{ q: "!(#{dashboard_ids.join(',')})" }
30+
end
31+
32+
def route
33+
"dashboard/"
34+
end
35+
end
36+
end
37+
end

lib/superset/dashboard/charts/list.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def rows
2222
c[:id],
2323
c[:slice_name],
2424
c[:form_data][:datasource],
25-
c[:form_data][:dashboards]
25+
# c[:form_data][:dashboards] # NOTE: form_data dashboards is not accurate .. looks to be bugs related to copying charts
2626
]
2727
end
2828
end
@@ -34,7 +34,7 @@ def route
3434
end
3535

3636
def list_attributes
37-
['id', 'slice_name', 'datasource', 'dashboards'].map(&:to_sym)
37+
['id', 'slice_name', 'datasource'].map(&:to_sym)
3838
end
3939

4040
# when displaying a list of datasets, show dashboard id and title as well

lib/superset/dashboard/delete.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def response
2424
@response ||= client.delete(route)
2525
end
2626

27-
# private
27+
private
2828

2929
def confirm_zero_charts_on_dashboard
3030
raise "Error: Dashboard includes #{dashboard_charts.count} charts. Please delete all charts before deleting the dashboard or override and set confirm_zero_charts: false" if dashboard_charts.count.positive?

lib/superset/dashboard/list.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def rows
5555
end
5656
end
5757

58+
def ids
59+
result.map { |d| d[:id] }
60+
end
61+
5862
private
5963

6064
def route
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
# TODO: the gui delete has a confirmation step, this does not. Potentially add a confirm_delete parameter to the constructor
3+
# that would ensure that no charts point to a dataset before deleting
4+
5+
module Superset
6+
module Dataset
7+
class BulkDelete < Superset::Request
8+
attr_reader :dataset_ids
9+
10+
def initialize(dataset_ids: [])
11+
@dataset_ids = dataset_ids
12+
end
13+
14+
def perform
15+
raise InvalidParameterError, "dataset_ids array of integers expected" unless dataset_ids.is_a?(Array)
16+
raise InvalidParameterError, "dataset_ids array must contin Integer only values" unless dataset_ids.all? { |item| item.is_a?(Integer) }
17+
18+
logger.info("Attempting to delete datasets with id: #{dataset_ids.join(', ')}")
19+
response
20+
end
21+
22+
def response
23+
@response ||= client.delete(route, params)
24+
end
25+
26+
private
27+
28+
def params
29+
{ q: "!(#{dataset_ids.join(',')})" }
30+
end
31+
32+
def route
33+
"dataset/"
34+
end
35+
end
36+
end
37+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Superset::Chart::BulkDelete do
4+
subject { described_class.new(chart_ids: chart_ids) }
5+
let(:chart_ids) { nil }
6+
let(:response) { "some response" }
7+
8+
before do
9+
allow(subject).to receive(:response).and_return(response)
10+
end
11+
12+
describe '.perform' do
13+
context 'when chart_ids is not present' do
14+
it 'raises an error' do
15+
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "chart_ids array of integers expected")
16+
end
17+
end
18+
19+
context 'when chart_ids contains non integer values' do
20+
let(:chart_ids) { [1, 'string'] }
21+
22+
it 'raises an error' do
23+
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "chart_ids array must contin Integer only values")
24+
end
25+
end
26+
27+
context 'when chart_ids is an integer' do
28+
let(:chart_ids) { [1, 2] }
29+
30+
it 'deletes the charts' do
31+
expect(subject).to receive(:response)
32+
subject.perform
33+
end
34+
end
35+
end
36+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Superset::Dashboard::BulkDelete do
4+
subject { described_class.new(dashboard_ids: dashboard_ids) }
5+
let(:dashboard_ids) { nil }
6+
let(:response) { "some response" }
7+
8+
before do
9+
allow(subject).to receive(:response).and_return(response)
10+
end
11+
12+
describe '.perform' do
13+
context 'when dashboard_ids is not present' do
14+
it 'raises an error' do
15+
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "dashboard_ids array of integers expected")
16+
end
17+
end
18+
19+
context 'when dashboard_ids contains non integer values' do
20+
let(:dashboard_ids) { [1, 'string'] }
21+
22+
it 'raises an error' do
23+
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "dashboard_ids array must contin Integer only values")
24+
end
25+
end
26+
27+
context 'when dashboard_ids is an integer' do
28+
let(:dashboard_ids) { [1, 2] }
29+
30+
it 'deletes the dashboards' do
31+
expect(subject).to receive(:response)
32+
subject.perform
33+
end
34+
end
35+
end
36+
end

spec/superset/dashboard/charts/list_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
specify do
3737
expect(subject.rows).to match_array(
3838
[
39-
[248, "Chart 1", "114__table", [28]],
40-
[249, "Chart 2", "114__table", [28]]
39+
[248, "Chart 1", "114__table"],
40+
[249, "Chart 2", "114__table"]
4141
]
4242
)
4343
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Superset::Dataset::BulkDelete do
4+
subject { described_class.new(dataset_ids: dataset_ids) }
5+
let(:dataset_ids) { nil }
6+
let(:response) { "some response" }
7+
8+
before do
9+
allow(subject).to receive(:response).and_return(response)
10+
end
11+
12+
describe '.perform' do
13+
context 'when dataset_ids is not present' do
14+
it 'raises an error' do
15+
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "dataset_ids array of integers expected")
16+
end
17+
end
18+
19+
context 'when dataset_ids contains non integer values' do
20+
let(:dataset_ids) { [1, 'string'] }
21+
22+
it 'raises an error' do
23+
expect { subject.perform }.to raise_error(Superset::Request::InvalidParameterError, "dataset_ids array must contin Integer only values")
24+
end
25+
end
26+
27+
context 'when dataset_ids is an integer' do
28+
let(:dataset_ids) { [1, 2] }
29+
30+
it 'deletes the datasets' do
31+
expect(subject).to receive(:response)
32+
subject.perform
33+
end
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)