3
3
from collections import OrderedDict
4
4
import datetime
5
5
6
+ from django .template .loader import render_to_string
6
7
from django .templatetags .static import static
7
8
from django .utils .timezone import now
8
9
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
14
11
15
12
from .conf import MAIL_TOOLBAR_TTL
16
13
from .utils import load_outbox , save_outbox
@@ -24,17 +21,19 @@ class MailToolbarPanel(Panel):
24
21
name = 'Mail'
25
22
template = 'mail_panel/panel.html'
26
23
has_content = True
24
+ is_historical = False
27
25
mail_list = OrderedDict ()
28
26
27
+ def __init__ (self , * args , ** kwargs ):
28
+ super ().__init__ (* args , ** kwargs )
29
+
29
30
@property
30
31
def scripts (self ):
31
32
scripts = super ().scripts
32
33
scripts .append (static ("debug_toolbar/mail/toolbar.mail.js" ))
33
34
return scripts
34
35
35
- def nav_title (self ):
36
- return _ ('Mail' )
37
-
36
+ @property
38
37
def nav_subtitle (self ):
39
38
mail_list = load_outbox ()
40
39
unread_count = 0
@@ -47,17 +46,31 @@ def nav_subtitle(self):
47
46
return '{0} unread messages' .format (unread_count )
48
47
return ''
49
48
49
+ @property
50
50
def title (self ):
51
51
return _ ('Mail' )
52
52
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 ):
54
65
"""
55
66
Main panel view. Loads and displays listing of mail.
56
67
"""
57
-
58
68
mail_list = load_outbox ()
69
+
70
+ # Return empty mail list early if nothing there.
59
71
if mail_list == {}:
60
- return
72
+ return mail_list
73
+
61
74
mail_list = OrderedDict (
62
75
sorted (iter (mail_list .items ()),
63
76
key = lambda x : x [1 ].date_sent ,
@@ -68,24 +81,20 @@ def generate_stats(self, request, response):
68
81
# Expire messages past TTL
69
82
expire_at = now () - datetime .timedelta (
70
83
seconds = MAIL_TOOLBAR_TTL )
84
+
71
85
for message_id , message in list (mail_list .items ()):
72
86
if message .date_sent < expire_at :
73
87
del mail_list [message_id ]
74
88
75
89
if prev_len != len (mail_list ):
76
90
save_outbox (mail_list )
77
91
78
- self .mail_list = mail_list
79
- self .record_stats ({
80
- 'mail_list' : self .mail_list ,
81
- })
92
+ return mail_list
82
93
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" })
89
98
90
99
@classmethod
91
100
def get_urls (cls ):
0 commit comments