From d385cbba6f42c989d58849c99ef4d1324b89beae Mon Sep 17 00:00:00 2001 From: Davide Date: Thu, 27 Jul 2023 11:14:10 +0200 Subject: [PATCH] Use hasattr to check if any of required attribute is present --- django_downloadview/middlewares.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/django_downloadview/middlewares.py b/django_downloadview/middlewares.py index e24b083..e877fb3 100644 --- a/django_downloadview/middlewares.py +++ b/django_downloadview/middlewares.py @@ -75,14 +75,10 @@ def is_download_response(self, response): whose file attribute have either an URL or a file name. """ - if super().is_download_response(response): - try: - return response.file.url or response.file.name - except AttributeError: - return False - else: - return True - return False + return ( + super().is_download_response(response) + and bool(getattr(response.file, 'url', None) or getattr(response.file, 'name', None)) + ) class DownloadDispatcher: