Merge pull request #2466 from consul/2120-admin_emails_list

Admin emails list download
This commit is contained in:
Alberto Calderón Queimadelos
2018-02-21 11:44:17 +01:00
committed by GitHub
13 changed files with 105 additions and 37 deletions

View File

@@ -41,7 +41,6 @@ gem 'redcarpet', '~> 3.4.0'
gem 'responders', '~> 2.4.0'
gem 'rinku', '~> 2.0.2', require: 'rails_rinku'
gem 'rollbar', '~> 2.15.5'
gem 'rubyzip', '~> 1.2.0'
gem 'sass-rails', '~> 5.0', '>= 5.0.4'
gem 'savon', '~> 2.11.1'
gem 'sitemap_generator', '~> 6.0.1'

View File

@@ -22,11 +22,15 @@ App.PreventDoubleSubmission =
initialize: ->
$('form').on('submit', (event) ->
unless event.target.id == "new_officing_voter"
unless event.target.id == "new_officing_voter" ||
event.target.id == "admin_download_emails"
buttons = $(this).find(':button, :submit')
App.PreventDoubleSubmission.disable_buttons(buttons)
).on('ajax:success', (event) ->
unless event.target.id == "new_officing_voter"
unless event.target.id == "new_officing_voter" ||
event.target.id == "admin_download_emails"
buttons = $(this).find(':button, :submit')
App.PreventDoubleSubmission.reset_buttons(buttons)
)

View File

@@ -0,0 +1,18 @@
class Admin::EmailsDownloadController < Admin::BaseController
def index
end
def generate_csv
users_segment = params[:users_segment]
filename = t("admin.segment_recipient.#{users_segment}")
csv_file = users_segment_emails_csv(users_segment)
send_data csv_file, filename: "#{filename}.csv"
end
private
def users_segment_emails_csv(users_segment)
UserSegments.send(users_segment).pluck(:email).to_csv
end
end

View File

@@ -44,12 +44,6 @@ class Admin::NewslettersController < Admin::BaseController
redirect_to admin_newsletters_path, notice: t("admin.newsletters.delete_success")
end
def users
zip = NewsletterZip.new('emails')
zip.create
send_file(File.join(zip.path), type: 'application/zip')
end
def deliver
@newsletter = Newsletter.find(params[:id])
Mailer.newsletter(@newsletter).deliver_later

View File

@@ -63,7 +63,7 @@
</a>
<ul id="polls_menu" <%= "class=is-active" if menu_polls? || controller.class.parent == Admin::Poll::Questions::Answers %>>
<li <%= "class=active" if controller_name == "polls" && action_name != "booth_assignments" ||
(["booth_assignments", "officer_assignments", "recounts", "results"].include? controller_name) %>>
%w(booth_assignments officer_assignments recounts results).include?(controller_name) %>>
<%= link_to t('admin.menu.polls'), admin_polls_path %>
</li>
@@ -198,15 +198,18 @@
</li>
<% end %>
<li class="section-title">
<li class="section-title" <%= "class=active" if %w(newsletters emails_download).include?(controller_name) %>>
<a href="#">
<span class="icon-zip"></span>
<strong><%= t("admin.menu.emails") %></strong>
</a>
<ul id="emails_menu" <%= "class=is-active" if controller_name == "newsletters" %>>
<ul id="emails_menu" <%= "class=is-active" if %w(newsletters emails_download).include?(controller_name) %>>
<li <%= "class=active" if controller_name == "newsletters" %>>
<%= link_to t("admin.menu.newsletters"), admin_newsletters_path %>
</li>
<li <%= "class=active" if controller_name == "emails_download" %>>
<%= link_to t("admin.menu.emails_download"), admin_emails_download_index_path %>
</li>
</ul>
</li>
</ul>

View File

@@ -0,0 +1,20 @@
<h2 class="inline-block"><%= t("admin.emails_download.index.title") %></h2>
<div class="small-6">
<%= form_tag generate_csv_admin_emails_download_index_path,
method: :get,
id: "admin_download_emails" do %>
<label><%= t('admin.emails_download.index.download_segment') %></label>
<p class="help-text" id="emails-help-text">
<%= t('admin.emails_download.index.download_segment_help_text') %>
</p>
<%= select_tag :users_segment, options_for_select(UserSegments::SEGMENTS
.collect { |s| [t("admin.segment_recipient.#{s}"), s] }) %>
<div class="margin-top">
<%= submit_tag t('admin.emails_download.index.download_emails_button'), class: "button" %>
</div>
<% end %>
</div>

View File

@@ -467,6 +467,7 @@ en:
moderators: Moderators
emails: Sending of emails
newsletters: Newsletters
emails_download: Emails download
valuators: Valuators
poll_officers: Poll officers
polls: Polls
@@ -554,6 +555,12 @@ en:
body: Email content
body_help_text: This is how the users will see the email
send_alert: Are you sure you want to send this newsletter to %{n} users?
emails_download:
index:
title: Emails download
download_segment: Download segment
download_segment_help_text: Download in CSV format
download_emails_button: Download emails list
valuators:
index:
title: Valuators

View File

@@ -466,6 +466,7 @@ es:
moderators: Moderadores
emails: Envío de emails
newsletters: Newsletters
emails_download: Descarga de emails
valuators: Evaluadores
poll_officers: Presidentes de mesa
polls: Votaciones
@@ -553,6 +554,12 @@ es:
body: Contenido del email
body_help_text: Así es como verán el email los usuarios
send_alert: ¿Estás seguro/a de que quieres enviar esta newsletter a %{n} usuarios?
emails_download:
index:
title: Descarga de emails
download_segment: Descargar segmento
download_segment_help_text: Descarga en formato CSV
download_emails_button: Descargar lista de emails
valuators:
index:
title: Evaluadores

View File

@@ -149,6 +149,10 @@ namespace :admin do
get :users, on: :collection
end
resources :emails_download, only: :index do
get :generate_csv, on: :collection
end
resource :stats, only: :show do
get :proposal_notifications, on: :collection
get :direct_messages, on: :collection

View File

@@ -1,25 +0,0 @@
require 'zip'
class NewsletterZip
attr_accessor :filename
def initialize(filename)
@filename = filename
end
def emails
User.newsletter.pluck(:email).join("\n")
end
def path
Rails.root + "/tmp/#{filename}.zip"
end
def create
Zip::File.open(path, Zip::File::CREATE) do |zipfile|
zipfile.get_output_stream("#{filename}.txt") do |file|
file.write emails
end
end
end
end

View File

@@ -1,4 +1,11 @@
class UserSegments
SEGMENTS = %w(all_users
proposal_authors
investment_authors
feasible_and_undecided_investment_authors
selected_investment_authors
winner_investment_authors)
def self.all_users
User.newsletter.active
end

View File

@@ -0,0 +1,30 @@
require 'rails_helper'
feature "Admin download user emails" do
background do
admin = create(:administrator)
login_as(admin.user)
end
context "Index" do
scenario "returns the selected users segment csv file" do
user1 = create(:user)
user2 = create(:user)
visit admin_emails_download_index_path
within('#admin_download_emails') do
select 'All users', from: 'users_segment'
click_button 'Download emails list'
end
header = page.response_headers['Content-Disposition']
expect(header).to match /^attachment/
expect(header).to match /filename="All users.csv"$/
expect(page).to have_content(user1.email)
expect(page).to have_content(user2.email)
end
end
end