From d64bf097d3e2bb462ebb129b3c633736a8c4b432 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 18 Mar 2022 16:22:58 +0100
Subject: [PATCH 23/65] misc: fix wrong-import-position pylint error (#62099)

---
 passerelle/apps/api_particulier/models.py |  4 +--
 passerelle/apps/csvdatasource/models.py   | 41 +++++++++++------------
 passerelle/utils/__init__.py              | 11 +++---
 passerelle/utils/jsonresponse.py          |  3 +-
 passerelle/wsgi.py                        |  3 +-
 5 files changed, 30 insertions(+), 32 deletions(-)

diff --git a/passerelle/apps/api_particulier/models.py b/passerelle/apps/api_particulier/models.py
index c5cb4928..cfc3b9f7 100644
--- a/passerelle/apps/api_particulier/models.py
+++ b/passerelle/apps/api_particulier/models.py
@@ -20,6 +20,7 @@
 
 import re
 from collections import OrderedDict
+from urllib import parse
 
 import requests
 
@@ -28,9 +29,6 @@ try:
 except ImportError:
     JSONDecodeError = ValueError
 
-
-from urllib import parse
-
 from django.contrib.postgres.fields import ArrayField
 from django.core.cache import cache
 from django.db import models
diff --git a/passerelle/apps/csvdatasource/models.py b/passerelle/apps/csvdatasource/models.py
index 68f95bcb..5030e5c8 100644
--- a/passerelle/apps/csvdatasource/models.py
+++ b/passerelle/apps/csvdatasource/models.py
@@ -19,6 +19,7 @@ import datetime
 import mimetypes
 import os
 import re
+import sys
 import tempfile
 from collections import OrderedDict
 
@@ -51,6 +52,25 @@ identifier_re = re.compile(r"^[^\d\W]\w*\Z", re.UNICODE)
 code_cache = OrderedDict()
 
 
+# Need for compatibility between xlrd==1.2.0 and python>3.7
+if sys.version_info >= (3, 8):
+    try:
+        import xlrd.xlsx
+    except ImportError:
+        pass
+    else:
+        if hasattr(xlrd.xlsx, 'ensure_elementtree_imported'):
+            old_ensure_elementtree_imported = xlrd.xlsx.ensure_elementtree_imported
+
+            def ensure_elementtree_imported(*args, **kwargs):
+                try:
+                    return old_ensure_elementtree_imported(*args, **kwargs)
+                finally:
+                    xlrd.xlsx.Element_has_iter = True
+
+            xlrd.xlsx.ensure_elementtree_imported = ensure_elementtree_imported
+
+
 def get_code(expr):
     # limit size of code cache to 1024
     if len(code_cache) > 1024:
@@ -546,24 +566,3 @@ class TableRow(models.Model):
     class Meta:
         ordering = ('line_number',)
         unique_together = ('resource', 'line_number')
-
-
-# Need for compatibility between xlrd==1.2.0 and python>3.7
-import sys
-
-if sys.version_info >= (3, 8):
-    try:
-        import xlrd.xlsx
-    except ImportError:
-        pass
-    else:
-        if hasattr(xlrd.xlsx, 'ensure_elementtree_imported'):
-            old_ensure_elementtree_imported = xlrd.xlsx.ensure_elementtree_imported
-
-            def ensure_elementtree_imported(*args, **kwargs):
-                try:
-                    return old_ensure_elementtree_imported(*args, **kwargs)
-                finally:
-                    xlrd.xlsx.Element_has_iter = True
-
-            xlrd.xlsx.ensure_elementtree_imported = ensure_elementtree_imported
diff --git a/passerelle/utils/__init__.py b/passerelle/utils/__init__.py
index 8946e333..8b6bc267 100644
--- a/passerelle/utils/__init__.py
+++ b/passerelle/utils/__init__.py
@@ -45,6 +45,11 @@ from urllib3.util.retry import Retry
 
 from passerelle.base.signature import check_query, check_url
 
+# legacy import, other modules keep importing to_json from passerelle.utils
+from .jsonresponse import to_json  # noqa F401 pylint: disable=unused-import
+from .sftp import SFTP, SFTPField  # noqa F401 pylint: disable=unused-import
+from .soap import SOAPClient, SOAPTransport  # noqa F401 pylint: disable=unused-import
+
 mark_safe_lazy = lazy(mark_safe, str)
 
 
@@ -445,9 +450,3 @@ def batch(iterable, size):
             yield chain([next(batchiter)], batchiter)
         except StopIteration:
             return
-
-
-# legacy import, other modules keep importing to_json from passerelle.utils
-from .jsonresponse import to_json
-from .sftp import SFTP, SFTPField
-from .soap import SOAPClient, SOAPTransport
diff --git a/passerelle/utils/jsonresponse.py b/passerelle/utils/jsonresponse.py
index 8890ee07..325de98a 100644
--- a/passerelle/utils/jsonresponse.py
+++ b/passerelle/utils/jsonresponse.py
@@ -20,7 +20,6 @@ from django.utils.encoding import force_text
 from django.utils.log import log_response
 from requests import HTTPError, RequestException
 
-from passerelle.utils import log_http_request
 from passerelle.utils.conversion import exception_to_text
 
 DEFAULT_DEBUG = getattr(settings, 'JSONRESPONSE_DEFAULT_DEBUG', False)
@@ -123,6 +122,8 @@ class to_json:
         return self.api(f, args[1], *args, **kwargs)
 
     def api(self, f, req, *args, **kwargs):
+        from passerelle.utils import log_http_request
+
         logger = self.logger or logging.getLogger('passerelle.jsonresponse')
         try:
             resp = f(*args, **kwargs)
diff --git a/passerelle/wsgi.py b/passerelle/wsgi.py
index 895fbfcb..6710add0 100644
--- a/passerelle/wsgi.py
+++ b/passerelle/wsgi.py
@@ -15,12 +15,13 @@ framework.
 """
 import os
 
+from django.core.wsgi import get_wsgi_application
+
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passerelle.settings")
 
 # This application object is used by any WSGI server configured to use this
 # file. This includes Django's development server, if the WSGI_APPLICATION
 # setting points here.
-from django.core.wsgi import get_wsgi_application
 
 application = get_wsgi_application()
 
-- 
2.34.1

