Skip to content

Update to work with serializable #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changelog

* 4.0.5 Updates panel to work with upcoming `serializable` branch.
* 4.0.4 Patches issue with missing alternatives attribute in mail object.
* 4.0.2 Mail panel is added by default to the debug toolbar.
* 4.0.0 Adds support for Django 4.0
Expand Down
15 changes: 8 additions & 7 deletions mail_panel/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@

class MailToolbarBackendEmail(mail.EmailMultiAlternatives):
def __init__(self, message):
try:
self.id = uuid4().get_hex()
except AttributeError:
self.id = uuid4().hex # python 3
self.id = uuid4().hex
self.date_sent = now()
self.read = False
message.message() # triggers header validation

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

super(MailToolbarBackendEmail, self).__init__(
super().__init__(
subject=message.subject,
to=message.to,
cc=message.cc,
Expand All @@ -33,6 +30,10 @@ def __init__(self, message):
)


@property
def get_subject(self):
return self.subject

class MailToolbarBackend(EmailBackend):
"""A email backend for use during testing.

Expand All @@ -44,7 +45,7 @@ class MailToolbarBackend(EmailBackend):
"""

def __init__(self, *args, **kwargs):
super(MailToolbarBackend, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

self.outbox = load_outbox()

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

save_outbox(self.outbox)

return super(MailToolbarBackend, self).send_messages(messages)
return super().send_messages(messages)
51 changes: 30 additions & 21 deletions mail_panel/panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
from collections import OrderedDict
import datetime

from django.template.loader import render_to_string
from django.templatetags.static import static
from django.utils.timezone import now

try:
from debug_toolbar.panels import Panel
except ImportError:
# django-debug-toolbar 1.x back compatibility
from debug_toolbar.panels import DebugPanel as Panel
from debug_toolbar.panels import Panel

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

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

@property
def scripts(self):
scripts = super().scripts
scripts.append(static("debug_toolbar/mail/toolbar.mail.js"))
return scripts

def nav_title(self):
return _('Mail')

@property
def nav_subtitle(self):
mail_list = load_outbox()
unread_count = 0
Expand All @@ -47,17 +46,31 @@ def nav_subtitle(self):
return '{0} unread messages'.format(unread_count)
return ''

@property
def title(self):
return _('Mail')

def generate_stats(self, request, response):
def get_stats(self):
return self.generate_stats()

@property
def content(self):
mail_list = self.get_mail_list()
self.record_stats({"mail_list": mail_list})
return render_to_string('mail_panel/panel.html', {
"mail_list": mail_list
})

def get_mail_list(self):
"""
Main panel view. Loads and displays listing of mail.
"""

mail_list = load_outbox()

# Return empty mail list early if nothing there.
if mail_list == {}:
return
return mail_list

mail_list = OrderedDict(
sorted(iter(mail_list.items()),
key=lambda x: x[1].date_sent,
Expand All @@ -68,24 +81,20 @@ def generate_stats(self, request, response):
# Expire messages past TTL
expire_at = now() - datetime.timedelta(
seconds=MAIL_TOOLBAR_TTL)

for message_id, message in list(mail_list.items()):
if message.date_sent < expire_at:
del mail_list[message_id]

if prev_len != len(mail_list):
save_outbox(mail_list)

self.mail_list = mail_list
self.record_stats({
'mail_list': self.mail_list,
})
return mail_list

def process_response(self, request, response):
"""
generate_stats replace process_response in django-debug-toolbar 2.0.
Call generate_stats for back compatibility.
"""
self.generate_stats(request, response)
def generate_stats(self, request, response):
# Need dummy info here to record data
# Mail is handled globally and not per-request.
self.record_stats({"a":"1"})

@classmethod
def get_urls(cls):
Expand Down
4 changes: 3 additions & 1 deletion mail_panel/templates/mail_panel/panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
<div class='djm-mail-toolbar'>

{% if not mail_list %}
<div style="font-size:1.5rem;">
<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>
No recent mail found.
</div>
{% else %}

{% spaceless %}
<div class='djm-message-list djm-no-select'>
<table id='djm_message_table'>
Expand Down