mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
FEATURE: export screened IPs list in a CSV file
This commit is contained in:
parent
0398ab7514
commit
515882d224
7 changed files with 75 additions and 16 deletions
|
@ -22,5 +22,14 @@ Discourse.ExportCsv.reopenClass({
|
|||
bootbox.alert(I18n.t("admin.export_csv.failed"));
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
Exports screened IPs list
|
||||
|
||||
@method export_screened_ips_list
|
||||
**/
|
||||
exportScreenedIpsList: function() {
|
||||
return Discourse.ajax("/admin/export_csv/screened_ips.json");
|
||||
}
|
||||
});
|
||||
|
|
15
app/assets/javascripts/admin/routes/admin-logs.js.es6
Normal file
15
app/assets/javascripts/admin/routes/admin-logs.js.es6
Normal file
|
@ -0,0 +1,15 @@
|
|||
export default Discourse.Route.extend({
|
||||
|
||||
actions: {
|
||||
exportScreenedIps: function() {
|
||||
Discourse.ExportCsv.exportScreenedIpsList().then(function(result) {
|
||||
if (result.success) {
|
||||
bootbox.alert(I18n.t("admin.export_csv.success"));
|
||||
} else {
|
||||
bootbox.alert(I18n.t("admin.export_csv.failed"));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
});
|
|
@ -8,6 +8,9 @@
|
|||
<li><a href="/logs" data-auto-route="true">{{i18n admin.logs.logster.title}}</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="pull-right">
|
||||
<button {{action "exportScreenedIps"}} class="btn" title="{{i18n admin.export_csv.screened_ips.title}}">{{fa-icon "download"}}{{i18n admin.export_csv.screened_ips.text}}</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="admin-container">
|
||||
|
|
|
@ -8,6 +8,12 @@ class Admin::ExportCsvController < Admin::AdminController
|
|||
render json: success_json
|
||||
end
|
||||
|
||||
def export_screened_ips_list
|
||||
# export csv file in a background thread
|
||||
Jobs.enqueue(:export_csv_file, entity: 'screened_ips', user_id: current_user.id)
|
||||
render json: success_json
|
||||
end
|
||||
|
||||
# download
|
||||
def show
|
||||
filename = params.fetch(:id)
|
||||
|
|
|
@ -6,6 +6,7 @@ module Jobs
|
|||
class ExportCsvFile < Jobs::Base
|
||||
CSV_USER_ATTRS = ['id','name','username','email','title','created_at','trust_level','active','admin','moderator','ip_address']
|
||||
CSV_USER_STATS = ['topics_entered','posts_read_count','time_read','topic_count','post_count','likes_given','likes_received']
|
||||
SCREENED_IP_ATTRS = ['ip_address','action_type','match_count','last_match_at','created_at']
|
||||
|
||||
sidekiq_options retry: false
|
||||
attr_accessor :current_user
|
||||
|
@ -24,20 +25,26 @@ module Jobs
|
|||
when 'user'
|
||||
query = ::AdminUserIndexQuery.new
|
||||
user_data = query.find_users_query.to_a
|
||||
data = Array.new
|
||||
|
||||
data = []
|
||||
user_data.each do |user|
|
||||
user_array = Array.new
|
||||
group_names = get_group_names(user).join(';')
|
||||
user_array = get_user_fields(user)
|
||||
user_array.push(group_names) if group_names != ''
|
||||
data.push(user_array)
|
||||
end
|
||||
when 'screened_ips'
|
||||
screened_ips_data = ScreenedIpAddress.order('id desc').to_a
|
||||
data = []
|
||||
screened_ips_data.each do |screened_ip|
|
||||
screened_ip_array = get_screened_ip_fields(screened_ip)
|
||||
data.push(screened_ip_array)
|
||||
end
|
||||
end
|
||||
|
||||
if data && data.length > 0
|
||||
set_file_path
|
||||
write_csv_file(data)
|
||||
header = get_header(entity)
|
||||
write_csv_file(data, header)
|
||||
end
|
||||
|
||||
notify_user
|
||||
|
@ -46,7 +53,7 @@ module Jobs
|
|||
private
|
||||
|
||||
def get_group_names(user)
|
||||
group_names = Array.new
|
||||
group_names = []
|
||||
groups = user.groups
|
||||
groups.each do |group|
|
||||
group_names.push(group.name)
|
||||
|
@ -55,7 +62,7 @@ module Jobs
|
|||
end
|
||||
|
||||
def get_user_fields(user)
|
||||
user_array = Array.new
|
||||
user_array = []
|
||||
|
||||
CSV_USER_ATTRS.each do |attr|
|
||||
user_array.push(user.attributes[attr])
|
||||
|
@ -74,16 +81,31 @@ module Jobs
|
|||
return user_array
|
||||
end
|
||||
|
||||
def get_header
|
||||
header_array = CSV_USER_ATTRS + CSV_USER_STATS
|
||||
def get_screened_ip_fields(screened_ip)
|
||||
screened_ip_array = []
|
||||
|
||||
user_custom_fields = UserField.all
|
||||
if user_custom_fields.present?
|
||||
user_custom_fields.each do |custom_field|
|
||||
header_array.push("#{custom_field.name} (custom user field)")
|
||||
end
|
||||
SCREENED_IP_ATTRS.each do |attr|
|
||||
screened_ip_array.push(screened_ip.attributes[attr])
|
||||
end
|
||||
|
||||
return screened_ip_array
|
||||
end
|
||||
|
||||
def get_header(entity)
|
||||
|
||||
case entity
|
||||
when 'user'
|
||||
header_array = CSV_USER_ATTRS + CSV_USER_STATS
|
||||
user_custom_fields = UserField.all
|
||||
if user_custom_fields.present?
|
||||
user_custom_fields.each do |custom_field|
|
||||
header_array.push("#{custom_field.name} (custom user field)")
|
||||
end
|
||||
end
|
||||
header_array.push("group_names")
|
||||
when 'screened_ips'
|
||||
header_array = SCREENED_IP_ATTRS
|
||||
end
|
||||
header_array.push("group_names")
|
||||
|
||||
return header_array
|
||||
end
|
||||
|
@ -95,10 +117,10 @@ module Jobs
|
|||
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
|
||||
end
|
||||
|
||||
def write_csv_file(data)
|
||||
def write_csv_file(data, header)
|
||||
# write to CSV file
|
||||
CSV.open(File.expand_path("#{ExportCsv.base_directory}/#{@file_name}", __FILE__), "w") do |csv|
|
||||
csv << get_header
|
||||
csv << header
|
||||
data.each do |value|
|
||||
csv << value
|
||||
end
|
||||
|
|
|
@ -1686,6 +1686,9 @@ en:
|
|||
users:
|
||||
text: "Export Users"
|
||||
title: "Export user list in a CSV file."
|
||||
screened_ips:
|
||||
text: "Export Screened IPs"
|
||||
title: "Export screened IPs list in a CSV file."
|
||||
success: "Export has been initiated, you will be notified shortly with progress."
|
||||
failed: "Export failed. Please check the logs."
|
||||
|
||||
|
|
|
@ -159,6 +159,7 @@ Discourse::Application.routes.draw do
|
|||
resources :export_csv, constraints: AdminConstraint.new do
|
||||
collection do
|
||||
get "users" => "export_csv#export_user_list"
|
||||
get "screened_ips" => "export_csv#export_screened_ips_list"
|
||||
end
|
||||
member do
|
||||
get "" => "export_csv#show", constraints: { id: /[^\/]+/ }
|
||||
|
|
Loading…
Reference in a new issue