FEATURE: add filters on email logs

This commit is contained in:
Régis Hanol 2014-02-15 00:50:08 +01:00
parent 21f757fd72
commit 3f3c9ca7cb
16 changed files with 145 additions and 67 deletions

View File

@ -0,0 +1,20 @@
/**
This controller supports email logs functionality.
@class AdminEmailSkippedController
@extends Discourse.Controller
@namespace Discourse
@module Discourse
**/
Discourse.AdminEmailSkippedController = Discourse.Controller.extend({
filterEmailLogs: Discourse.debounce(function() {
var self = this;
this.set("loading", true);
Discourse.EmailLog.findAll(this.get("filter")).then(function(logs) {
self.set("model", false);
self.set("model", logs);
});
}, 250).observes("filter.user", "filter.address", "filter.type", "filter.reason"),
});

View File

@ -0,0 +1,20 @@
/**
This controller supports email logs functionality.
@class AdminEmailSentController
@extends Discourse.Controller
@namespace Discourse
@module Discourse
**/
Discourse.AdminEmailSentController = Discourse.Controller.extend({
filterEmailLogs: Discourse.debounce(function() {
var self = this;
this.set("loading", true);
Discourse.EmailLog.findAll(this.get("filter")).then(function(logs) {
self.set("loading", false);
self.set("model", logs);
});
}, 250).observes("filter.user", "filter.address", "filter.type", "filter.reply_key"),
});

View File

@ -9,25 +9,27 @@
Discourse.EmailLog = Discourse.Model.extend({});
Discourse.EmailLog.reopenClass({
create: function(attrs) {
attrs = attrs || {};
if (attrs.user) {
attrs.user = Discourse.AdminUser.create(attrs.user);
}
return this._super(attrs);
},
findAll: function(filter) {
var result = Em.A();
Discourse.ajax("/admin/email/" + (filter === 'skipped' ? 'skipped' : 'logs') + ".json", {
data: { filter: filter }
}).then(function(logs) {
_.each(logs,function(log) {
result.pushObject(Discourse.EmailLog.create(log));
filter = filter || {};
var status = filter.status || "sent";
filter = _.omit(filter, "status");
return Discourse.ajax("/admin/email/" + status + ".json", { data: filter }).then(function(logs) {
return _.map(logs, function (log) {
return Discourse.EmailLog.create(log);
});
});
return result;
}
});

View File

@ -8,13 +8,11 @@
**/
Discourse.AdminEmailIndexRoute = Discourse.Route.extend({
setupController: function(controller) {
Discourse.EmailSettings.find().then(function (model) {
controller.set('model', model);
});
model: function() {
return Discourse.EmailSettings.find();
},
renderTemplate: function() {
this.render('admin/templates/email_index', {into: 'adminEmail'});
this.render('admin/templates/email_index', { into: 'adminEmail' });
}
});

View File

@ -1,17 +0,0 @@
/**
Handles routes related to viewing email logs.
@class AdminEmailLogsRoute
@extends Discourse.Route
@namespace Discourse
@module Discourse
**/
Discourse.AdminEmailLogsRoute = Discourse.Route.extend({
model: function() {
return Discourse.EmailLog.findAll();
},
renderTemplate: function() {
this.render('admin/templates/email_logs', {into: 'adminEmail'});
}
});

View File

@ -0,0 +1,27 @@
/**
Handles routes related to viewing email logs.
@class AdminEmailSentRoute
@extends Discourse.Route
@namespace Discourse
@module Discourse
**/
Discourse.AdminEmailLogsRoute = Discourse.Route.extend({
model: function() {
return Discourse.EmailLog.findAll({ status: this.get("status") });
},
setupController: function(controller) {
// resets the filters
controller.set("filter", { status: this.get("status") });
},
renderTemplate: function() {
this.render("admin/templates/email_" + this.get("status"), { into: "adminEmail" });
}
});
Discourse.AdminEmailSentRoute = Discourse.AdminEmailLogsRoute.extend({ status: "sent" });
Discourse.AdminEmailSkippedRoute = Discourse.AdminEmailLogsRoute.extend({ status: "skipped" });

View File

@ -1,17 +0,0 @@
/**
Handles routes related to viewing email logs of emails that were NOT sent.
@class AdminEmailSkippedRoute
@extends Discourse.Route
@namespace Discourse
@module Discourse
**/
Discourse.AdminEmailSkippedRoute = Discourse.Route.extend({
model: function() {
return Discourse.EmailLog.findAll('skipped');
},
renderTemplate: function() {
this.render('admin/templates/email_skipped', {into: 'adminEmail'});
}
});

View File

@ -16,7 +16,7 @@ Discourse.Route.buildRoutes(function() {
});
this.resource('adminEmail', { path: '/email'}, function() {
this.route('logs');
this.route('sent');
this.route('skipped');
this.route('previewDigest', { path: '/preview-digest' });
});

View File

@ -2,7 +2,7 @@
<div class='span15'>
<ul class="nav nav-pills">
<li>{{#link-to 'adminEmail.index'}}{{i18n admin.email.settings}}{{/link-to}}</li>
<li>{{#link-to 'adminEmail.logs'}}{{i18n admin.email.logs}}{{/link-to}}</li>
<li>{{#link-to 'adminEmail.sent'}}{{i18n admin.email.sent}}{{/link-to}}</li>
<li>{{#link-to 'adminEmail.skipped'}}{{i18n admin.email.skipped}}{{/link-to}}</li>
<li>{{#link-to 'adminEmail.previewDigest'}}{{i18n admin.email.preview_digest}}{{/link-to}}</li>
</ul>

View File

@ -9,7 +9,15 @@
</tr>
</thead>
{{#if model.length}}
<tr class="filters">
<td>{{i18n admin.email.logs.filters.title}}</td>
<td>{{textField value=filter.user placeholderKey="admin.email.logs.filters.user_placeholder"}}</td>
<td>{{textField value=filter.address placeholderKey="admin.email.logs.filters.address_placeholder"}}</td>
<td>{{textField value=filter.type placeholderKey="admin.email.logs.filters.type_placeholder"}}</td>
<td>{{textField value=filter.reply_key placeholderKey="admin.email.logs.filters.reply_key_placeholder"}}</td>
</tr>
{{#if model}}
{{#groupedEach model}}
<tr>
<td>{{unboundDate created_at}}</td>
@ -26,6 +34,8 @@
<td>{{reply_key}}</td>
</tr>
{{/groupedEach}}
{{else}}
<tr><td colspan="5">{{i18n admin.email.logs.none}}</td></tr>
{{/if}}
</table>

View File

@ -9,7 +9,15 @@
</tr>
</thead>
{{#if model.length}}
<tr class="filters">
<td>{{i18n admin.email.logs.filters.title}}</td>
<td>{{textField value=filter.user placeholderKey="admin.email.logs.filters.user_placeholder"}}</td>
<td>{{textField value=filter.address placeholderKey="admin.email.logs.filters.address_placeholder"}}</td>
<td>{{textField value=filter.type placeholderKey="admin.email.logs.filters.type_placeholder"}}</td>
<td>{{textField value=filter.skipReason placeholderKey="admin.email.logs.filters.skip_reason_placeholder"}}</td>
</tr>
{{#if model}}
{{#groupedEach model}}
<tr>
<td>{{unboundDate created_at}}</td>
@ -23,11 +31,11 @@
</td>
<td><a href='mailto:{{unbound to_address}}'>{{to_address}}</a></td>
<td>{{email_type}}</td>
<td>
{{skipped_reason}}
</td>
<td>{{skipped_reason}}</td>
</tr>
{{/groupedEach}}
{{else}}
<tr><td colspan="5">{{i18n admin.email.logs.none}}</td></tr>
{{/if}}
</table>

View File

@ -12,6 +12,7 @@
td {border-bottom: 1px solid $primary_lighter; border-top: 1px solid $primary_lighter;}
tr:hover { background-color: darken($secondary, 2.5%); }
tr.selected { background-color: lighten($tertiary, 58%); }
.filters input { margin-bottom: 0px; }
}
.content-list li a span.count {

View File

@ -3,10 +3,8 @@ require_dependency 'email/renderer'
class Admin::EmailController < Admin::AdminController
def index
render_json_dump({
delivery_method: delivery_method,
settings: delivery_settings
})
data = { delivery_method: delivery_method, settings: delivery_settings }
render_json_dump(data)
end
def test
@ -15,14 +13,14 @@ class Admin::EmailController < Admin::AdminController
render nothing: true
end
def logs
@email_logs = EmailLog.sent.limit(50).includes(:user).order('created_at desc').to_a
render_serialized(@email_logs, EmailLogSerializer)
def sent
email_logs = filter_email_logs(EmailLog.sent, params)
render_serialized(email_logs, EmailLogSerializer)
end
def skipped
@email_logs = EmailLog.skipped.limit(50).includes(:user).order('created_at desc').to_a
render_serialized(@email_logs, EmailLogSerializer)
email_logs = filter_email_logs(EmailLog.skipped, params)
render_serialized(email_logs, EmailLogSerializer)
end
def preview_digest
@ -33,6 +31,16 @@ class Admin::EmailController < Admin::AdminController
private
def filter_email_logs(email_logs, params)
email_logs = email_logs.limit(50).includes(:user).order("email_logs.created_at desc").references(:user)
email_logs = email_logs.where("users.username LIKE ?", "%#{params[:user]}%") if params[:user].present?
email_logs = email_logs.where("email_logs.to_address LIKE ?", "%#{params[:address]}%") if params[:address].present?
email_logs = email_logs.where("email_logs.email_type LIKE ?", "%#{params[:type]}%") if params[:type].present?
email_logs = email_logs.where("email_logs.reply_key LIKE ?", "%#{params[:reply_key]}%") if params[:reply_key].present?
email_logs = email_logs.where("email_logs.skipped_reason LIKE ?", "%#{params[:reason]}%") if params[:reason].present?
email_logs.to_a
end
def delivery_settings
action_mailer_settings
.reject { |k, v| k == :password }

View File

@ -1382,7 +1382,7 @@ en:
email:
title: "Email"
settings: "Settings"
logs: "Logs"
sent: "Sent"
skipped: "Skipped"
sent_at: "Sent At"
time: "Time"
@ -1402,6 +1402,15 @@ en:
last_seen_user: "Last Seen User:"
reply_key: "Reply Key"
skip_reason: "Skip Reason"
logs:
none: "No logs found."
filters:
title: "Filter"
user_placeholder: "username"
address_placeholder: "em@il.com"
type_placeholder: "digest, signup..."
reply_key_placeholder: ""
skip_reason_placeholder: "reason"
logs:
title: "Logs"

View File

@ -67,7 +67,7 @@ Discourse::Application.routes.draw do
resources :email do
collection do
post "test"
get "logs"
get "sent"
get "skipped"
get "preview-digest" => "email#preview_digest"
end

View File

@ -27,9 +27,18 @@ describe Admin::EmailController do
end
end
context '.logs' do
context '.sent' do
before do
xhr :get, :logs
xhr :get, :sent
end
subject { response }
it { should be_success }
end
context '.skipped' do
before do
xhr :get, :skipped
end
subject { response }