Skip to content

Commit 178225e

Browse files
authored
Clear emails interface (#20)
* Adds ability to clear icons from the interface
1 parent 0acbc14 commit 178225e

File tree

9 files changed

+108
-21
lines changed

9 files changed

+108
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changelog
22

3+
* 3.2 Adds ability to resize panels and clear messages. (Thanks @jensenbox)
34
* 1.2.5 Fixes subject line (Thanks @indrif)
45
* 1.2.4 Resolves conflicts with other libraries accessing mail (Thanks @ciotto)
56
* 1.2.3 Updates import from DebugPanel to Panel (Thanks @ciotto & @WilliamMayor!)

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def read(fname):
2424

2525
setup(
2626
name='django-mail-panel',
27-
version='3.1',
27+
version='3.2',
2828
description='A panel for django-debug-toolbar that allows for ' +
2929
'viewing of recently sent email.',
3030
url='https://github.com/scuml/django-mail-panel',
@@ -59,5 +59,6 @@ def read(fname):
5959
'Programming Language :: Python :: 3.6',
6060
'Programming Language :: Python :: 3.7',
6161
'Programming Language :: Python :: 3.8',
62+
'Programming Language :: Python :: 3.9',
6263
],
6364
)

src/mail_panel/static/debug_toolbar/mail/mail_toolbar.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,7 @@
132132
height: 15px;
133133
margin-right: 5px;
134134
}
135+
#djDebug .djm-mail-toolbar .table-icon{
136+
height: 14px;
137+
margin-top: 2px;
138+
}

src/mail_panel/static/debug_toolbar/mail/toolbar.mail.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ function djmail_document_ready(f){
66
}
77
}
88

9+
function djmail_get(url, callback)
10+
{
11+
var req = new XMLHttpRequest();
12+
req.onreadystatechange = function() {
13+
if (req.readyState == 4 && req.status == 200) {
14+
try {
15+
var data = JSON.parse(req.responseText);
16+
} catch(err) {
17+
console.log(err.message + " in " + req.responseText);
18+
return;
19+
}
20+
callback(data);
21+
}
22+
};
23+
req.open("GET", url, true);
24+
req.send();
25+
}
26+
927
function djmail_load(url, element, callback)
1028
{
1129
var req = new XMLHttpRequest();
@@ -29,7 +47,9 @@ djmail_document_ready(function(){
2947
resize_message()
3048
});
3149
let child = $q('.djm-message-list');
32-
observer.observe(child, { attributes: true });
50+
if(child){
51+
observer.observe(child, { attributes: true });
52+
}
3353

3454
function resize_message() {
3555
let new_height = window.innerHeight - $q("#djm_message_container").getBoundingClientRect().top + window.scrollY + window.pageYOffset -10
@@ -49,6 +69,38 @@ djmail_document_ready(function(){
4969
if (sidebar_textbox)
5070
sidebar_textbox.innerHTML = unread_text;
5171
}
72+
73+
function clear_message(url){
74+
djmail_get(url, function(data){
75+
if(data["status"] != "success"){
76+
return
77+
}
78+
$q("#djm_message_table tbody tr [url='"+ url +"']").closest("tr").remove();
79+
$q('#djm_message_container').style.opacity = 0;
80+
resize_message();
81+
})
82+
83+
84+
}
85+
window.settings["clear_message"] = clear_message
86+
87+
function clear_all_messages(url){
88+
var confirm = window.confirm("Are you sure you want to clear all messages?");
89+
if (confirm == false) {
90+
return
91+
}
92+
djmail_get(url, function(data){
93+
if(data["status"] != "success"){
94+
return
95+
}
96+
$qa("#djm_message_table tbody tr").forEach(el => el.remove());
97+
$q('#djm_message_container').style.opacity = 0;
98+
resize_message();
99+
})
100+
}
101+
window.settings["clear_all_messages"] = clear_all_messages
102+
103+
52104
function load_message(element){
53105
let message_id = element.id;
54106
let url = element.getAttribute('url');
@@ -116,11 +168,17 @@ djmail_document_ready(function(){
116168
}
117169

118170
$qa(".djm-message-row").forEach(function(e){
119-
console.info("e");
120171
e.addEventListener('click', function(){
121-
console.info("click", this);
122172
load_message(this);
123173
})
124174
});
175+
$qa("[jsmethod]").forEach(function(e){
176+
e.addEventListener('click', function(e){
177+
e.stopPropagation(); // stop row onClick event
178+
var method = window.settings[this.getAttribute("jsmethod")]
179+
method(this.getAttribute("url"))
180+
181+
})
125182

183+
})
126184
});
684 Bytes
Loading

src/mail_panel/templates/mail_panel/panel.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<th>Subject</th>
1919
<th>Date Sent</th>
2020
<th></th>
21+
<th style="text-align:center"><img jsmethod="clear_all_messages" url="{% url 'djdt:clear_all_messages' %}" class="table-icon" src="{% static 'debug_toolbar/mail/trash.png' %}" alt=""></th>
2122
</tr>
2223
</thead>
2324
{% endspaceless %}
@@ -29,6 +30,7 @@
2930
<td>{{message.subject}}</td>
3031
<td>{{message.date_sent}}</td>
3132
<td style='text-align:right'>{% if message.attachments %}<span class='djm-attachment'></span>{{message.attachments|length}} items{% endif %}</td>
33+
<td style="text-align:center"><img jsmethod="clear_message" url="{% url 'djdt:clear_message' message_id %}" class="table-icon" src="{% static 'debug_toolbar/mail/trash.png' %}" alt=""></td>
3234
</tr>
3335
{% endfor %}
3436
</tbody>

src/mail_panel/urls.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from django.conf.urls import url
2-
from .views import load_message, display_multipart, download_attachment
2+
from .views import (
3+
load_message, display_multipart, download_attachment, clear_message, clear_all_messages
4+
)
35

46
_PREFIX = 'mail_toolbar'
57

@@ -19,4 +21,15 @@
1921
display_multipart,
2022
name="display_multipart"
2123
),
24+
25+
url(
26+
r'^{0}/clear_message/(?P<message_id>[\w]+)/$'.format(_PREFIX),
27+
clear_message,
28+
name="clear_message"
29+
),
30+
url(
31+
r'^{0}/clear_all_messages/$'.format(_PREFIX),
32+
clear_all_messages,
33+
name="clear_all_messages"
34+
),
2235
]

src/mail_panel/views.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
from django.http import HttpResponse
1+
from django.http import HttpResponse, JsonResponse
22
from django.conf import settings
33
from django.shortcuts import render
44
from django.utils.html import urlize
55
from django.utils.safestring import mark_safe
66

7-
from .utils import load_outbox, save_outbox
7+
from .utils import load_outbox, save_outbox, clear_outbox
88

99
if hasattr(settings, "DEBUG_TOOLBAR_FILTER_URL"):
1010
settings.DEBUG_TOOLBAR_FILTER_URL = settings.DEBUG_TOOLBAR_FILTER_URL + ("__mail_toolbar_debug__")
1111
else:
1212
settings.DEBUG_TOOLBAR_FILTER_URL = ("__mail_toolbar_debug__",)
1313

14+
1415
def load_message(request, message_id):
1516
"""
1617
Loads a message template into the subframe
@@ -89,3 +90,24 @@ def download_attachment(request, message_id, attachment_id):
8990
)
9091

9192
return response
93+
94+
def clear_message(request, message_id):
95+
"""
96+
Clears a message from the cache
97+
"""
98+
mail_list = load_outbox()
99+
try:
100+
del(mail_list[message_id])
101+
except KeyError:
102+
pass
103+
save_outbox(mail_list)
104+
105+
return JsonResponse({"status": "success"})
106+
107+
def clear_all_messages(request):
108+
"""
109+
Clears all message from the cache
110+
"""
111+
clear_outbox()
112+
113+
return JsonResponse({"status": "success"})

test_build.sh

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@ echo $TEST
1313
FILE=`ls -1 dist/*.whl | tail -n 1`
1414
echo "Verifying build of $FILE"
1515

16-
if [ -z "$1" ] || [ "$1" -eq "2" ]; then
17-
echo "# Installing virtualenv for Python 2"
18-
rm -rf 27-sdist # ensure clean state if ran repeatedly
19-
virtualenv 27-sdist
20-
21-
echo "# Install Python 2 requirements"
22-
27-sdist/bin/pip install django $FILE
23-
24-
echo "# Run command with Python 2"
25-
27-sdist/bin/python -m "$TEST"
26-
27-
echo "# Test using Python 2 ended"
28-
fi
29-
3016
if [ -z "$1" ] || [ "$1" -eq "3" ]; then
3117
echo "# Installing virtualenv for Python 3"
3218
rm -rf 3-sdist # ensure clean state if ran repeatedly

0 commit comments

Comments
 (0)