Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin: adding export to CSV #2120

Merged
merged 8 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,17 @@ def user_for_paper_trail
def paginate?
params[:results_per_page].to_i.positive?
end

def render_by_format(page, filename)
respond_to do |format|
format.html { render page }
format.csv do
raw_csv = @q.result.to_csv
send_data raw_csv,
filename: "#{filename}_#{Time.zone.now.to_formatted_s(:number)}.csv",
type: "#{Mime[:csv]}; charset=utf-8"
end
end
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/admin/blocked_domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def index
@q = domains.search(params[:q])
@domains = @q.result.page(params[:page])
@domains = @domains.per(params[:results_per_page]) if params[:results_per_page].to_i.positive?

render_by_format('admin/blocked_domains/index', 'blocked_domains')
end

def new
Expand Down
1 change: 1 addition & 0 deletions app/controllers/admin/contact_versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def index
@versions = @q.result.page(params[:page])
@versions = @versions.per(params[:results_per_page]) if params[:results_per_page].to_i.positive?

render_by_format('admin/contact_versions/index', 'contact_history')
end

def show
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/admin/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def index
end

@contacts = @contacts.per(params[:results_per_page]) if params[:results_per_page].to_i.positive?

render_by_format('admin/contacts/index', 'contacts')
end

def filter_by_flags(contacts)
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/admin/disputes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def index
params[:q] ||= {}
@disputes = sortable_dispute_query_for(Dispute.active.all, params[:q])
@closed_disputes = sortable_dispute_query_for(Dispute.closed.all, params[:q], closed: true)

render_by_format('admin/disputes/index', 'disputes')
end

# GET /admin/disputes/1
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/admin/domain_versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def index
@q = versions.search(params[:q])
@versions = @q.result.page(params[:page])
@versions = @versions.per(params[:results_per_page]) if params[:results_per_page].to_i.positive?
render "admin/domain_versions/archive"

render_by_format('admin/domain_versions/archive', 'domain_history')
end

def show
Expand Down
10 changes: 1 addition & 9 deletions app/controllers/admin/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ def index
end
@domains = @domains.per(params[:results_per_page]) if params[:results_per_page].to_i.positive?

respond_to do |format|
format.html do
@domains
end
format.csv do
raw_csv = @domains.to_csv
send_data raw_csv, filename: 'domains.csv', type: "#{Mime[:csv]}; charset=utf-8"
end
end
render_by_format('admin/domains/index', 'domains')
end

def show
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/admin/epp_logs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def index
@epp_logs = @epp_logs.page(params[:page])
@count = @q.result.count
@epp_logs = @epp_logs.per(params[:results_per_page]) if paginate?

render_by_format('admin/epp_logs/index', 'epp_logs')
end

def show
Expand All @@ -31,7 +33,6 @@ def set_default_dates

params[:q][:created_at_gteq] = Date.send(default_date).strftime("%Y-%m-%d")
end

end
end
end
3 changes: 2 additions & 1 deletion app/controllers/admin/repp_logs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def index
@repp_logs = @repp_logs.page(params[:page])
@count = @q.result.count
@repp_logs = @repp_logs.per(params[:results_per_page]) if paginate?

render_by_format('admin/repp_logs/index', 'repp_logs')
end

def show
Expand All @@ -32,7 +34,6 @@ def set_default_dates

params[:q][:created_at_gteq] = Date.send(default_date).strftime("%Y-%m-%d")
end

end
end
end
2 changes: 2 additions & 0 deletions app/controllers/admin/reserved_domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def index
@q = domains.search(params[:q])
@domains = @q.result.page(params[:page])
@domains = @domains.per(params[:results_per_page]) if params[:results_per_page].to_i.positive?

render_by_format('admin/reserved_domains/index', 'reserved_domains')
end

def new
Expand Down
10 changes: 10 additions & 0 deletions app/lib/to_csv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module ToCsv
def to_csv
CSV.generate do |csv|
csv << column_names
all.find_each do |item|
csv << item.attributes.values_at(*column_names)
end
end
end
end
1 change: 1 addition & 0 deletions app/models/api_log/epp_log.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module ApiLog
class EppLog < Db
extend ToCsv
end
end
1 change: 1 addition & 0 deletions app/models/api_log/repp_log.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module ApiLog
class ReppLog < Db
extend ToCsv
end
end
1 change: 1 addition & 0 deletions app/models/blocked_domain.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class BlockedDomain < ApplicationRecord
include Versions
extend ToCsv
before_save :generate_data
after_destroy :remove_data

Expand Down
1 change: 1 addition & 0 deletions app/models/dispute.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Dispute < ApplicationRecord
extend ToCsv
include WhoisStatusPopulate
validates :domain_name, :password, :starts_at, :expires_at, presence: true
before_validation :fill_empty_passwords, :set_expiry_date
Expand Down
31 changes: 22 additions & 9 deletions app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ def registrant_user_indirect_domains(registrant_user)
)
end

def to_csv
CSV.generate do |csv|
headers = column_names.dup
swap_elements(headers, [[0, 1], [1, 5]])
headers[0] = 'Domain'
headers[1] = headers[1].humanize
csv << headers
all.find_each do |item|
row = item.attributes.values_at(*column_names)
swap_elements(row, [[0, 1], [1, 5]])
csv << row
end
end
end

private

def registrant_user_domains_by_registrant(registrant_user)
Expand Down Expand Up @@ -720,15 +735,6 @@ def contact_emails_verification_failed
contacts.select(&:email_verification_failed?)&.map(&:email)&.uniq
end

def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |domain|
csv << domain.attributes.values_at(*column_names)
end
end
end

def self.pdf(html)
kit = PDFKit.new(html)
kit.to_pdf
Expand All @@ -749,4 +755,11 @@ def self.outzone_candidates
def self.uses_zone?(zone)
exists?(["name ILIKE ?", "%.#{zone.origin}"])
end

def self.swap_elements(array, indexes)
indexes.each do |index|
array[index[0]], array[index[1]] = array[index[1]], array[index[0]]
end
array
end
end
1 change: 1 addition & 0 deletions app/models/reserved_domain.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ReservedDomain < ApplicationRecord
extend ToCsv
include Versions # version/reserved_domain_version.rb
include WhoisStatusPopulate
before_save :fill_empty_passwords
Expand Down
4 changes: 2 additions & 2 deletions app/models/version/contact_version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Version::ContactVersion < PaperTrail::Version
extend ToCsv
include VersionSession

self.table_name = :log_contacts
self.sequence_name = :log_contacts_id_seq

# scope :deleted, -> { where(event: 'destroy') }
end
1 change: 1 addition & 0 deletions app/models/version/domain_version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Version::DomainVersion < PaperTrail::Version
extend ToCsv
include VersionSession

self.table_name = :log_domains
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/blocked_domains/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
.form-group
= label_tag t(:results_per_page)
= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page)
.col-md-3{style: 'padding-top: 25px;'}
.col-md-4{style: 'padding-top: 25px;'}
%button.btn.btn-primary
&nbsp;
%span.glyphicon.glyphicon-search
&nbsp;
= link_to(t('.csv_btn'), admin_blocked_domains_path(format: :csv, params: params.permit!), class: 'btn btn-default')
= link_to(t('.reset_btn'), admin_blocked_domains_path, class: 'btn btn-default')
%hr
.row
Expand Down
25 changes: 13 additions & 12 deletions app/views/admin/contact_versions/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
= label_tag :action
= select_tag '[q][event]', options_for_select([['Update', 'update'], ['Destroy', 'destroy'], ['Create', 'create']], params[:q][:event]), { include_blank:true, multiple: false, placeholder: t(:choose), class: 'form-control js-combobox' }
.row
.col-md-3
.col-md-3
.col-md-3
.form-group
= label_tag t(:results_per_page)
= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page)
.col-md-3{style: 'padding-top: 25px;'}
%button.btn.btn-primary
&nbsp;
%span.glyphicon.glyphicon-search
&nbsp;
= link_to(t('.reset_btn'), admin_contact_versions_path, class: 'btn btn-default')
.col-md-3
.col-md-3
.col-md-3
.form-group
= label_tag t(:results_per_page)
= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page)
.col-md-3{style: 'padding-top: 25px;float:right;padding-right: 0px;'}
%button.btn.btn-primary
&nbsp;
%span.glyphicon.glyphicon-search
&nbsp;
= link_to(t('.csv_btn'), admin_contact_versions_path(format: :csv, params: params.permit!), class: 'btn btn-default')
= link_to(t('.reset_btn'), admin_contact_versions_path, class: 'btn btn-default')
%hr


Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/contacts/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@
= check_box_tag :email_verification_failed, '1',params[:email_verification_failed].eql?('1'), style: 'width:auto;height:auto;float:right'

.row
.col-md-3{style: 'padding-top: 25px;float:right;'}
.col-md-3{style: 'padding-top: 25px;float:right;padding-right: 0px'}
%button.btn.btn-primary
&nbsp;
%span.glyphicon.glyphicon-search
&nbsp;
= link_to(t('.csv_btn'), admin_contacts_path(format: :csv, params: params.permit!), class: 'btn btn-default')
= link_to(t('.reset_btn'), admin_contacts_path, class: 'btn btn-default')
%hr
.row
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/disputes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
<%= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page) %>
</div>
</div>
<div class="col-md-3" style="padding-top: 25px;">
<div class="col-md-4" style="padding-top: 25px;">
<button class="btn btn-primary">
&nbsp;
<span class="glyphicon glyphicon-search"></span>
&nbsp;
</button>
<%= link_to(t('.csv_btn'), admin_disputes_path(format: :csv, params: params.permit!), class: 'btn btn-default') %>
<%= link_to(t('.reset_btn'), admin_disputes_path, class: 'btn btn-default') %>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/domain_versions/archive.haml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
.form-group
= label_tag t(:results_per_page)
= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page)
.col-md-3{style: 'padding-top: 25px;'}
.col-md-3{style: 'padding-top: 25px;float:right;padding-right: 0px;'}
%button.btn.btn-primary
&nbsp;
%span.glyphicon.glyphicon-search
&nbsp;
= link_to(t('.csv_btn'), admin_domain_versions_path(format: :csv, params: params.permit!), class: 'btn btn-default')
= link_to(t('.reset_btn'), admin_domain_versions_path, class: 'btn btn-default')
%hr

Expand Down
4 changes: 3 additions & 1 deletion app/views/admin/epp_logs/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
.form-group
= f.label t(:created_before)
= f.search_field :created_at_lteq, value: params[:q][:created_at_lteq], class: 'form-control js-datepicker', placeholder: t(:created_before)
.col-md-3{style: 'padding-top: 25px;'}
.col-md-3{style: 'padding-top: 25px;float:right;padding-right: 0px;'}
%button.btn.btn-primary
&nbsp;
%span.glyphicon.glyphicon-search
&nbsp;
= link_to(t('.csv_btn'), admin_epp_logs_path(format: :csv, params: params.permit!), class: 'btn btn-default')
= link_to(t('.reset_btn'), admin_epp_logs_path, class: 'btn btn-default')
.row
.col-md-12
Expand All @@ -49,6 +50,7 @@
.pull-right
.pagination
= t(:result_count, count: @count) if @count > 0
.row
.table-responsive
%table.table.table-hover.table-bordered.table-condensed
%thead
Expand Down
4 changes: 3 additions & 1 deletion app/views/admin/repp_logs/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
.form-group
= f.label t(:created_before)
= f.search_field :created_at_lteq, value: params[:q][:created_at_lteq], class: 'form-control js-datepicker', placeholder: t(:created_before)
.col-md-3{style: 'padding-top: 25px;'}
.col-md-3{style: 'padding-top: 25px;float:right;padding-right: 0px;'}
%button.btn.btn-primary
&nbsp;
%span.glyphicon.glyphicon-search
&nbsp;
= link_to(t('.csv_btn'), admin_repp_logs_path(format: :csv, params: params.permit!), class: 'btn btn-default')
= link_to(t('.reset_btn'), admin_repp_logs_path, class: 'btn btn-default')
%hr
.row
Expand All @@ -48,6 +49,7 @@
.pull-right
.pagination
= t(:result_count, count: @count) if @count > 0
.row
.table-responsive
%table.table.table-hover.table-bordered.table-condensed
%thead
Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/reserved_domains/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
.form-group
= label_tag t(:results_per_page)
= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page)
.col-md-3{style: 'padding-top: 25px;'}
.col-md-4{style: 'padding-top: 25px;'}
%button.btn.btn-primary
&nbsp;
%span.glyphicon.glyphicon-search
&nbsp;
= link_to(t('.csv_btn'), admin_reserved_domains_path(format: :csv, params: params.permit!), class: 'btn btn-default')
= link_to(t('.reset_btn'), admin_reserved_domains_path, class: 'btn btn-default')
%hr
.row
Expand Down
1 change: 1 addition & 0 deletions config/locales/admin/blocked_domains.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ en:
title: Blocked domains
new_btn: New blocked domain
reset_btn: Reset
csv_btn: CSV
1 change: 1 addition & 0 deletions config/locales/admin/contact_versions.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ en:
contact_versions:
index:
reset_btn: Reset
csv_btn: CSV
1 change: 1 addition & 0 deletions config/locales/admin/contacts.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ en:
contacts:
index:
reset_btn: Reset
csv_btn: CSV

edit:
new_status_btn: Add new status
Expand Down
Loading