Skip to content

Commit

Permalink
Merge pull request #63 from modoboa/feature/mark_as_spam
Browse files Browse the repository at this point in the history
New 'Mark as junk' button.
  • Loading branch information
tonioo committed Mar 17, 2017
2 parents 87b9732 + b92ed66 commit bf7092d
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 62 deletions.
5 changes: 5 additions & 0 deletions modoboa_webmail/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ class UserSettings(param_forms.UserParametersForm):
label=_("Drafts folder"),
help_text=_("Folder where drafts go")
)
junk_folder = forms.CharField(
initial="Junk",
label=_("Junk folder"),
help_text=_("Folder where junk messages should go")
)

sep3 = form_utils.SeparatorField(label=_("Composing messages"))

Expand Down
3 changes: 2 additions & 1 deletion modoboa_webmail/lib/imaputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ def getmboxes(
"label": _("Inbox")},
{"name": user.parameters.get_value("drafts_folder"),
"class": "fa fa-file", "label": _("Drafts")},
{"name": "Junk", "class": "fa fa-fire", "label": _("Junk")},
{"name": user.parameters.get_value("junk_folder"),
"class": "fa fa-fire", "label": _("Junk")},
{"name": user.parameters.get_value("sent_folder"),
"class": "fa fa-envelope", "label": _("Sent")},
{"name": user.parameters.get_value("trash_folder"),
Expand Down
47 changes: 47 additions & 0 deletions modoboa_webmail/static/modoboa_webmail/js/webmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ Webmail.prototype = {
"click", "a[name=compose]", $.proxy(this.compose_loader, this));
$(document).on(
"click", "a[name=totrash]", $.proxy(this.delete_messages, this));
$(document).on(
"click", "a[name=mark_as_junk_multi]", $.proxy(this.markMessagesAsJunk, this));

$(document).on(
"click", "a[name*=mark-]", $.proxy(this.send_mark_request, this));
$(document).on("click", "a[name=compress]", $.proxy(this.compress, this));
Expand All @@ -129,6 +132,7 @@ Webmail.prototype = {
$(document).on("click", "a[name=replyall]", $.proxy(this.reply_loader, this));
$(document).on("click", "a[name=forward]", $.proxy(this.reply_loader, this));
$(document).on("click", "a[name=delete]", $.proxy(this.delete_message, this));
$(document).on("click", "a[name=mark_as_junk]", $.proxy(this.markMessageAsJunk, this));
$(document).on(
"click", "a[name=activate_links]", $.proxy(function(e) { this.display_mode(e, "1"); }, this));
$(document).on("click", "a[name=disable_links]", $.proxy(function(e) { this.display_mode(e, "0"); }, this));
Expand Down Expand Up @@ -877,6 +881,44 @@ Webmail.prototype = {
}).done($.proxy(this.delete_callback, this));
},

markMessageAsJunk: function(e) {
var $link = get_target(e, 'a');
e.preventDefault();
$.ajax({
url: $link.attr('href'),
dataType: 'json'
}).done($.proxy(this.markAsJunkCallback, this));
},

markMessagesAsJunk: function(e) {
e.preventDefault();
var $link = get_target(e, 'a');
if ($link.hasClass("disabled")) {
return;
}
var msgs = this.htmltable.current_selection();
var selection = [];
var unseen_cnt = 0;

if (!msgs.length) {
return;
}
$link.addClass("disabled");
$.each(msgs, function(idx, item) {
var $tr = $(item);
selection.push($tr.attr("id"));
if ($tr.hasClass("unseen")) {
unseen_cnt++;
}
});
this.change_unseen_messages(this.get_current_mailbox(), -unseen_cnt);
this.change_unseen_messages(this.options.trash, unseen_cnt);
$.ajax({
url: $link.attr("href"),
data: {mbox: this.get_current_mailbox(), selection: selection}
}).done($.proxy(this.markAsJunkCallback, this));
},

display_mode: function(e, value) {
e.preventDefault();
this.navobject.setparam("links", value).update();
Expand Down Expand Up @@ -1103,6 +1145,11 @@ Webmail.prototype = {
$("body").notify("success", data, 2000);
},

markAsJunkCallback: function(data) {
this.go_back_to_listing();
$("body").notify("success", data, 2000);
},

/*
* Mailbox form initialization
*/
Expand Down
138 changes: 77 additions & 61 deletions modoboa_webmail/templatetags/webmail_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,55 @@
@register.simple_tag
def viewmail_menu(selection, folder, user, mail_id=None):
"""Menu of the viewmail location."""
entries = [
{"name": "back",
"url": "javascript:history.go(-1)",
"img": "fa fa-arrow-left",
"class": "btn-default",
"label": _("Back")},
{"name": "reply",
"url": "action=reply&mbox=%s&mailid=%s" % (folder, mail_id),
"img": "fa fa-mail-reply",
"class": "btn-primary",
"label": _("Reply"),
"menu": [
{"name": "replyall",
"url": "action=reply&mbox=%s&mailid=%s&all=1" % (folder, mail_id),
"img": "fa fa-mail-reply-all",
"label": _("Reply all")},
{"name": "forward",
"url": "action=forward&mbox=%s&mailid=%s" % (folder, mail_id),
"img": "fa fa-mail-forward",
"label": _("Forward")},
]},
{"name": "delete",
"img": "fa fa-trash",
"class": "btn-danger",
"url": u"{0}?mbox={1}&selection[]={2}".format(
reverse("modoboa_webmail:mail_delete"), folder, mail_id),
"title": _("Delete")
},
{"name": "display_options",
"title": _("Display options"),
"img": "fa fa-cog",
"menu": [
{"name": "activate_links",
"label": _("Activate links")},
{"name": "disable_links",
"label": _("Disable links")}
]
}
]
entries = [{
"name": "back",
"url": "javascript:history.go(-1)",
"img": "fa fa-arrow-left",
"class": "btn-default",
"label": _("Back")
}, {
"name": "reply",
"url": "action=reply&mbox=%s&mailid=%s" % (folder, mail_id),
"img": "fa fa-mail-reply",
"class": "btn-primary",
"label": _("Reply"),
"menu": [{
"name": "replyall",
"url": "action=reply&mbox=%s&mailid=%s&all=1" % (folder, mail_id),
"img": "fa fa-mail-reply-all",
"label": _("Reply all")
}, {
"name": "forward",
"url": "action=forward&mbox=%s&mailid=%s" % (folder, mail_id),
"img": "fa fa-mail-forward",
"label": _("Forward")
}]
}, {
"name": "delete",
"img": "fa fa-trash",
"class": "btn-danger",
"url": u"{0}?mbox={1}&selection[]={2}".format(
reverse("modoboa_webmail:mail_delete"), folder, mail_id),
"title": _("Delete")
}, {
"name": "mark_as_junk",
"img": "fa fa-fire",
"class": "btn-warning",
"url": u"{0}?mbox={1}&selection[]={2}".format(
reverse("modoboa_webmail:mail_mark_as_junk"), folder, mail_id),
"title": _("Mark as spam")
}, {
"name": "display_options",
"title": _("Display options"),
"img": "fa fa-cog",
"menu": [{
"name": "activate_links",
"label": _("Activate links")
}, {
"name": "disable_links",
"label": _("Disable links")
}]
}]
menu = render_to_string('common/buttons_list.html',
{"selection": selection, "entries": entries,
"user": user, "extraclasses": "pull-left"})
Expand Down Expand Up @@ -87,28 +97,34 @@ def compose_menu(selection, backurl, user, **kwargs):
@register.simple_tag
def listmailbox_menu(selection, folder, user):
"""The menu of the listmailbox action."""
entries = [
{"name": "totrash",
"title": _("Delete"),
"class": "btn-danger",
"img": "fa fa-trash",
"url": reverse("modoboa_webmail:mail_delete")
},
{"name": "actions",
"label": _("Actions"),
"class": "btn btn-default",
"menu": [
{"name": "mark-read",
"label": _("Mark as read"),
"url": u"{0}?status=read".format(
reverse("modoboa_webmail:mail_mark", args=[folder]))},
{"name": "mark-unread",
"label": _("Mark as unread"),
"url": u"{0}?status=unread".format(
reverse("modoboa_webmail:mail_mark", args=[folder]))},
]
},
]
entries = [{
"name": "totrash",
"title": _("Delete"),
"class": "btn-danger",
"img": "fa fa-trash",
"url": reverse("modoboa_webmail:mail_delete")
}, {
"name": "mark_as_junk_multi",
"img": "fa fa-fire",
"class": "btn-warning",
"url": reverse("modoboa_webmail:mail_mark_as_junk"),
"title": _("Mark as spam")
}, {
"name": "actions",
"label": _("Actions"),
"class": "btn btn-default",
"menu": [{
"name": "mark-read",
"label": _("Mark as read"),
"url": u"{0}?status=read".format(
reverse("modoboa_webmail:mail_mark", args=[folder]))
}, {
"name": "mark-unread",
"label": _("Mark as unread"),
"url": u"{0}?status=unread".format(
reverse("modoboa_webmail:mail_mark", args=[folder]))
}]
}]
if folder == user.parameters.get_value("trash_folder"):
entries[0]["class"] += " disabled"
entries[1]["menu"] += [
Expand Down
2 changes: 2 additions & 0 deletions modoboa_webmail/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
name="mail_move"),
url(r'^mark/(?P<name>.+)/$', views.mark,
name="mail_mark"),
url(r'^mark_as_junk/$', views.mark_as_junk,
name="mail_mark_as_junk"),

url(r'^newfolder/$', views.newfolder,
name="folder_add"),
Expand Down
19 changes: 19 additions & 0 deletions modoboa_webmail/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ def mark(request, name):
})


@login_required
@needs_mailbox()
def mark_as_junk(request):
"""Mark a message as SPAM."""
mbox = request.GET.get("mbox")
selection = request.GET.getlist("selection[]")
if mbox is None or selection is None:
raise BadRequest(_("Invalid request"))
selection = [item for item in selection if item.isdigit()]
mbc = get_imapconnector(request)
mbc.move(",".join(selection), mbox,
request.user.parameters.get_value("junk_folder"))
count = len(selection)
message = ungettext("%(count)d message marked",
"%(count)d messages marked",
count) % {"count": count}
return render_to_json_response(message)


@login_required
@needs_mailbox()
def empty(request):
Expand Down

0 comments on commit bf7092d

Please sign in to comment.