From 02cc98d61b3d8694e122114bec029ca860bfc47b Mon Sep 17 00:00:00 2001 From: Tino de Bruijn Date: Wed, 21 May 2014 09:50:54 +0200 Subject: [PATCH] Allow for floating pixelratios. (Happens when people use browser zoom) --- responsive/middleware.py | 4 ++-- responsive/tests/test_middleware.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/responsive/middleware.py b/responsive/middleware.py index bbf87e7..ea071d9 100644 --- a/responsive/middleware.py +++ b/responsive/middleware.py @@ -28,7 +28,7 @@ def process_request(self, request): if value is not None: try: width, height, pixelratio = value.split(':') - width, height, pixelratio = int(width), int(height), int(pixelratio) + width, height, pixelratio = int(width), int(height), float(pixelratio) except ValueError: # TODO: Add logging width = None @@ -36,7 +36,7 @@ def process_request(self, request): pixelratio = None info = {'width': width, 'height': height, 'pixelratio': pixelratio} if width is not None: - info['type'] = _get_device_type(width) + info['type'] = _get_device_type(width) else: info['type'] = None request.device_info = info diff --git a/responsive/tests/test_middleware.py b/responsive/tests/test_middleware.py index b90401b..017964d 100644 --- a/responsive/tests/test_middleware.py +++ b/responsive/tests/test_middleware.py @@ -51,6 +51,19 @@ def test_process_request_valid_cookie(self): self.assertEqual(device['type'], 'phone') self.assertEqual(device['pixelratio'], 2) + def test_process_request_float_pixelratio(self): + """ + `pixelratio` can be a float when the user uses in-browser zoom and + should give us valid device info. + """ + self.request.COOKIES['resolution'] = '1920:1200:1.100000023841858' # 110% + self.middleware.process_request(request=self.request) + device = self.request.device_info + self.assertEqual(device['width'], 1920) + self.assertEqual(device['height'], 1200) + self.assertEqual(device['type'], 'desktop') + self.assertEqual(device['pixelratio'], 1.100000023841858) + class DeviceInfoScriptTestCase(unittest.TestCase): "Middleware for including necessary script tags in HTML responses."