Skip to content

Commit ed38168

Browse files
authored
Update to work with serializable (#43)
1 parent 4b1d1a1 commit ed38168

File tree

4 files changed

+42
-29
lines changed

4 files changed

+42
-29
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+
* 4.0.5 Updates panel to work with upcoming `serializable` branch.
34
* 4.0.4 Patches issue with missing alternatives attribute in mail object.
45
* 4.0.2 Mail panel is added by default to the debug toolbar.
56
* 4.0.0 Adds support for Django 4.0

mail_panel/backend.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99

1010
class MailToolbarBackendEmail(mail.EmailMultiAlternatives):
1111
def __init__(self, message):
12-
try:
13-
self.id = uuid4().get_hex()
14-
except AttributeError:
15-
self.id = uuid4().hex # python 3
12+
self.id = uuid4().hex
1613
self.date_sent = now()
1714
self.read = False
1815
message.message() # triggers header validation
1916

2017
alternatives = getattr(message, "alternatives", None)
2118

22-
super(MailToolbarBackendEmail, self).__init__(
19+
super().__init__(
2320
subject=message.subject,
2421
to=message.to,
2522
cc=message.cc,
@@ -33,6 +30,10 @@ def __init__(self, message):
3330
)
3431

3532

33+
@property
34+
def get_subject(self):
35+
return self.subject
36+
3637
class MailToolbarBackend(EmailBackend):
3738
"""A email backend for use during testing.
3839
@@ -44,7 +45,7 @@ class MailToolbarBackend(EmailBackend):
4445
"""
4546

4647
def __init__(self, *args, **kwargs):
47-
super(MailToolbarBackend, self).__init__(*args, **kwargs)
48+
super().__init__(*args, **kwargs)
4849

4950
self.outbox = load_outbox()
5051

@@ -57,4 +58,4 @@ def send_messages(self, messages):
5758

5859
save_outbox(self.outbox)
5960

60-
return super(MailToolbarBackend, self).send_messages(messages)
61+
return super().send_messages(messages)

mail_panel/panels.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
from collections import OrderedDict
44
import datetime
55

6+
from django.template.loader import render_to_string
67
from django.templatetags.static import static
78
from django.utils.timezone import now
89

9-
try:
10-
from debug_toolbar.panels import Panel
11-
except ImportError:
12-
# django-debug-toolbar 1.x back compatibility
13-
from debug_toolbar.panels import DebugPanel as Panel
10+
from debug_toolbar.panels import Panel
1411

1512
from .conf import MAIL_TOOLBAR_TTL
1613
from .utils import load_outbox, save_outbox
@@ -24,17 +21,19 @@ class MailToolbarPanel(Panel):
2421
name = 'Mail'
2522
template = 'mail_panel/panel.html'
2623
has_content = True
24+
is_historical = False
2725
mail_list = OrderedDict()
2826

27+
def __init__(self, *args, **kwargs):
28+
super().__init__(*args, **kwargs)
29+
2930
@property
3031
def scripts(self):
3132
scripts = super().scripts
3233
scripts.append(static("debug_toolbar/mail/toolbar.mail.js"))
3334
return scripts
3435

35-
def nav_title(self):
36-
return _('Mail')
37-
36+
@property
3837
def nav_subtitle(self):
3938
mail_list = load_outbox()
4039
unread_count = 0
@@ -47,17 +46,31 @@ def nav_subtitle(self):
4746
return '{0} unread messages'.format(unread_count)
4847
return ''
4948

49+
@property
5050
def title(self):
5151
return _('Mail')
5252

53-
def generate_stats(self, request, response):
53+
def get_stats(self):
54+
return self.generate_stats()
55+
56+
@property
57+
def content(self):
58+
mail_list = self.get_mail_list()
59+
self.record_stats({"mail_list": mail_list})
60+
return render_to_string('mail_panel/panel.html', {
61+
"mail_list": mail_list
62+
})
63+
64+
def get_mail_list(self):
5465
"""
5566
Main panel view. Loads and displays listing of mail.
5667
"""
57-
5868
mail_list = load_outbox()
69+
70+
# Return empty mail list early if nothing there.
5971
if mail_list == {}:
60-
return
72+
return mail_list
73+
6174
mail_list = OrderedDict(
6275
sorted(iter(mail_list.items()),
6376
key=lambda x: x[1].date_sent,
@@ -68,24 +81,20 @@ def generate_stats(self, request, response):
6881
# Expire messages past TTL
6982
expire_at = now() - datetime.timedelta(
7083
seconds=MAIL_TOOLBAR_TTL)
84+
7185
for message_id, message in list(mail_list.items()):
7286
if message.date_sent < expire_at:
7387
del mail_list[message_id]
7488

7589
if prev_len != len(mail_list):
7690
save_outbox(mail_list)
7791

78-
self.mail_list = mail_list
79-
self.record_stats({
80-
'mail_list': self.mail_list,
81-
})
92+
return mail_list
8293

83-
def process_response(self, request, response):
84-
"""
85-
generate_stats replace process_response in django-debug-toolbar 2.0.
86-
Call generate_stats for back compatibility.
87-
"""
88-
self.generate_stats(request, response)
94+
def generate_stats(self, request, response):
95+
# Need dummy info here to record data
96+
# Mail is handled globally and not per-request.
97+
self.record_stats({"a":"1"})
8998

9099
@classmethod
91100
def get_urls(cls):

mail_panel/templates/mail_panel/panel.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
<div class='djm-mail-toolbar'>
66

77
{% if not mail_list %}
8+
<div style="font-size:1.5rem;">
9+
<svg xmlns="http://www.w3.org/2000/svg" width="2rem" height="2rem" viewBox="0 0 576 512"> <path fill="#999" d="M0 208L0 384c0 35.3 28.7 64 64 64l160 0c35.3 0 64-28.7 64-64l0-176c0-79.5-64.5-144-144-144S0 128.5 0 208zm64 0c0-8.8 7.2-16 16-16l128 0c8.8 0 16 7.2 16 16s-7.2 16-16 16L80 224c-8.8 0-16-7.2-16-16zm256 0l0 176c0 24.6-9.2 47-24.4 64L512 448c35.3 0 64-28.7 64-64l0-176c0-79.5-64.5-144-144-144L245.2 64C290.4 95.9 320 148.5 320 208zm64 0c0-8.8 7.2-16 16-16l64 0 32 0c8.8 0 16 7.2 16 16l0 64c0 8.8-7.2 16-16 16l-32 0c-8.8 0-16-7.2-16-16l0-48-48 0c-8.8 0-16-7.2-16-16z"/></svg>
810
No recent mail found.
11+
</div>
912
{% else %}
10-
1113
{% spaceless %}
1214
<div class='djm-message-list djm-no-select'>
1315
<table id='djm_message_table'>

0 commit comments

Comments
 (0)