Skip to content

Commit e654bd1

Browse files
ciottoscuml
authored andcommitted
add django-debug-toolbar 2.0 compatibility and fix tests (#14)
1 parent 2c41f80 commit e654bd1

File tree

5 files changed

+58
-14
lines changed

5 files changed

+58
-14
lines changed

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import os
2-
from pathlib import Path
32
import sys
43
from setuptools import setup, find_packages
54

65
# allow setup.py to be run from any path
7-
os.chdir(str(Path(__file__).absolute().parent))
6+
os.chdir(os.path.abspath(os.path.dirname(os.path.realpath(__file__))))
87

98
if 'publish' in sys.argv:
109
if 'test' in sys.argv:

src/mail_panel/panels.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
from collections import OrderedDict
44
import datetime
55

6-
from debug_toolbar.panels import DebugPanel
6+
try:
7+
from debug_toolbar.panels import Panel
8+
except ImportError:
9+
# django-debug-toolbar 1.x back compatibility
10+
from debug_toolbar.panels import DebugPanel as Panel
711

812
from .conf import MAIL_TOOLBAR_TTL
913
from .utils import load_outbox, save_outbox
1014
from .urls import urlpatterns
1115

12-
class MailToolbarPanel(DebugPanel):
16+
17+
class MailToolbarPanel(Panel):
1318
"""
1419
Panel that displays informations about mail
1520
"""
@@ -36,7 +41,7 @@ def nav_subtitle(self):
3641
def title(self):
3742
return _('Mail')
3843

39-
def process_response(self, request, response):
44+
def generate_stats(self, request, response):
4045
"""
4146
Main panel view. Loads and displays listing of mail.
4247
"""
@@ -66,6 +71,13 @@ def process_response(self, request, response):
6671
'mail_list': self.mail_list,
6772
})
6873

74+
def process_response(self, request, response):
75+
"""
76+
generate_stats replace process_response in django-debug-toolbar 2.0.
77+
Call generate_stats for back compatibility.
78+
"""
79+
self.generate_stats(request, response)
80+
6981
@classmethod
7082
def get_urls(cls):
7183
return urlpatterns

test_build.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
# Builds package and tests wheel in python 2 and 3
55
# Add 2 or 3 to test a particular version of python
66

7-
python setup.py sdist bdist_wheel
7+
python3 setup.py sdist bdist_wheel
88

9-
DJANGO_SETTINGS_MODULE='tests.settings'
10-
COMMAND="from mail_panel.panels import MailToolbarPanel; MailToolbarPanel(object)"
11-
echo $COMMAND
9+
export DJANGO_SETTINGS_MODULE='tests.settings'
10+
TEST="tests"
11+
echo $TEST
1212

1313
FILE=`ls -1 dist/*.whl | tail -n 1`
1414
echo "Verifying build of $FILE"
@@ -17,14 +17,26 @@ if [ -z "$1" ] || [ "$1" -eq "2" ]; then
1717
echo "# Installing virtualenv for Python 2"
1818
rm -rf 27-sdist # ensure clean state if ran repeatedly
1919
virtualenv 27-sdist
20+
21+
echo "# Install Python 2 requirements"
2022
27-sdist/bin/pip install django $FILE
21-
27-sdist/bin/python -c"$COMMAND"
23+
24+
echo "# Run command with Python 2"
25+
27-sdist/bin/python -m "$TEST"
26+
27+
echo "# Test using Python 2 ended"
2228
fi
2329

2430
if [ -z "$1" ] || [ "$1" -eq "3" ]; then
2531
echo "# Installing virtualenv for Python 3"
2632
rm -rf 3-sdist # ensure clean state if ran repeatedly
2733
virtualenv -p python3 3-sdist
34+
35+
echo "# Install Python 3 requirements"
2836
3-sdist/bin/pip3 install django $FILE
29-
3-sdist/bin/python3 -c"$COMMAND"
37+
38+
echo "# Run command with Python 3"
39+
3-sdist/bin/python3 -m "$TEST"
40+
41+
echo "# Test using Python 3 ended"
3042
fi

tests/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .test_toolbar import main
2+
3+
if __name__ == "__main__":
4+
main()

tests/test_toolbar.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
from .context import *
22

33
import unittest
4+
import debug_toolbar
45
from mail_panel.panels import MailToolbarPanel
56

7+
68
class ToolbarSuite(unittest.TestCase):
9+
def setUp(self):
10+
debug_toolbar_version = debug_toolbar.VERSION
11+
12+
# django-debug-toolbar 1.x take 1 argument, 2.x take 2 arguments
13+
self.panel_args = (None, None)
14+
if debug_toolbar_version < '2.0':
15+
self.panel_args = (None, )
716

817
def test_panel(self):
918
"""
1019
General 'does it run' test.
1120
"""
12-
p = MailToolbarPanel(None)
13-
assert(p.toolbar is None)
21+
p = MailToolbarPanel(*self.panel_args)
22+
self.assertIsNone(p.toolbar)
23+
24+
def test_generate_stats(self):
25+
p = MailToolbarPanel(*self.panel_args)
26+
p.generate_stats(None, None)
27+
28+
def test_process_response(self):
29+
p = MailToolbarPanel(*self.panel_args)
30+
p.process_response(None, None)
1431

1532
def suite():
1633
suite = unittest.TestSuite()
1734
suite.addTest(unittest.makeSuite(ToolbarSuite))
1835
return suite
1936

20-
if __name__ == "__main__":
37+
def main():
2138
unittest.TextTestRunner().run(suite())

0 commit comments

Comments
 (0)