From 6fa896f4a229951e046ab3e83860cd3444e03ac1 Mon Sep 17 00:00:00 2001 From: Akhilesh Sharma Date: Wed, 4 Dec 2024 10:51:30 +0530 Subject: [PATCH] [change] Refactored code by running openwisp-qa-format --- CHANGES.rst | 4 ++-- README.rst | 4 ++-- docs/developer/extending.rst | 8 ++------ docs/user/settings.rst | 8 ++------ openwisp_radius/api/views.py | 6 +++--- openwisp_radius/base/forms.py | 4 +++- openwisp_radius/base/models.py | 8 ++++++-- openwisp_radius/receivers.py | 1 + .../tests/static/test_batch_utf16_file2.csv | 2 +- openwisp_radius/tests/test_saml/utils.py | 6 +++--- openwisp_radius/tests/test_utils.py | 19 ++++++++++++++----- openwisp_radius/utils.py | 12 +++++++----- 12 files changed, 46 insertions(+), 36 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 5e88d17c..fdd3aeae 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,8 +8,8 @@ Features ~~~~~~~~ - Added integration with `OpenWISP Monitoring - `_ to - collect and visualize metrics for user-signups and RADIUS traffic. + `_ + to collect and visualize metrics for user-signups and RADIUS traffic. - Added support for `Change of Authorization (CoA) `_. - Added `MonthlyTrafficCounter diff --git a/README.rst b/README.rst index 2232c150..9e6bd574 100644 --- a/README.rst +++ b/README.rst @@ -46,8 +46,8 @@ verification `generation of new users for events `_, `social login -`_, and much -more. +`_, and +much more. It can be used as a standalone application or integrated with the rest of `OpenWISP `_. It can also be used as a `base system diff --git a/docs/developer/extending.rst b/docs/developer/extending.rst index 95f0cb54..5927e07c 100644 --- a/docs/developer/extending.rst +++ b/docs/developer/extending.rst @@ -206,9 +206,7 @@ Once you have created the models, add the following to your OPENWISP_RADIUS_RADIUSGROUP_MODEL = "myradius.RadiusGroup" OPENWISP_RADIUS_RADIUSTOKEN_MODEL = "myradius.RadiusToken" OPENWISP_RADIUS_PHONETOKEN_MODEL = "myradius.PhoneToken" - OPENWISP_RADIUS_ORGANIZATIONRADIUSSETTINGS_MODEL = ( - "myradius.OrganizationRadiusSettings" - ) + OPENWISP_RADIUS_ORGANIZATIONRADIUSSETTINGS_MODEL = "myradius.OrganizationRadiusSettings" OPENWISP_RADIUS_REGISTEREDUSER_MODEL = "myradius.RegisteredUser" # You will need to change AUTH_USER_MODEL if you are extending openwisp_users @@ -315,9 +313,7 @@ resort to monkey patching, you can proceed as follows: RadiusGroupCheck = load_model("openwisp_radius", "RadiusGroupCheck") RadiusGroupReply = load_model("openwisp_radius", "RadiusGroupReply") RadiusUserGroup = load_model("openwisp_radius", "RadiusUserGroup") - OrganizationRadiusSettings = load_model( - "openwisp_radius", "OrganizationRadiusSettings" - ) + OrganizationRadiusSettings = load_model("openwisp_radius", "OrganizationRadiusSettings") User = get_user_model() admin.site.unregister(RadiusCheck) diff --git a/docs/user/settings.rst b/docs/user/settings.rst index 1c8dd4d7..c29f4242 100644 --- a/docs/user/settings.rst +++ b/docs/user/settings.rst @@ -539,9 +539,7 @@ means that the global setting specified in ``settings.py`` will be used. .. code-block:: python - { - "__all__": "https://{site}/{organization}/password/reset/confirm/{uid}/{token}" - } + {"__all__": "https://{site}/{organization}/password/reset/confirm/{uid}/{token}"} A dictionary representing the frontend URLs through which end users can complete the password reset operation. @@ -871,9 +869,7 @@ type in the API response for ``ChilliSpot-Max-Input-Octets`` attribute: .. code-block:: python - OPENWISP_RADIUS_RADIUS_ATTRIBUTES_TYPE_MAP = { - "ChilliSpot-Max-Input-Octets": "bytes" - } + OPENWISP_RADIUS_RADIUS_ATTRIBUTES_TYPE_MAP = {"ChilliSpot-Max-Input-Octets": "bytes"} .. _radius_social_login_settings: diff --git a/openwisp_radius/api/views.py b/openwisp_radius/api/views.py index 6bb21916..30190cad 100644 --- a/openwisp_radius/api/views.py +++ b/openwisp_radius/api/views.py @@ -155,9 +155,9 @@ def get(self, request, *args, **kwargs): if radbatch.strategy == 'prefix': pdf = generate_pdf(radbatch.pk) response = HttpResponse(content_type='application/pdf') - response[ - 'Content-Disposition' - ] = f'attachment; filename="{radbatch.name}.pdf"' + response['Content-Disposition'] = ( + f'attachment; filename="{radbatch.name}.pdf"' + ) response.write(pdf) return response else: diff --git a/openwisp_radius/base/forms.py b/openwisp_radius/base/forms.py index d6d28637..fadcee56 100644 --- a/openwisp_radius/base/forms.py +++ b/openwisp_radius/base/forms.py @@ -46,7 +46,9 @@ def clean(self): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if 'csvfile' in self.fields: - docs_link = 'https://openwisp.io/docs/stable/radius/user/importing_users.html' + docs_link = ( + 'https://openwisp.io/docs/stable/radius/user/importing_users.html' + ) help_text = f"Refer to the docs \ for more details on importing users from a CSV" self.fields['csvfile'].help_text = help_text diff --git a/openwisp_radius/base/models.py b/openwisp_radius/base/models.py index 68f1e3fc..5e816851 100644 --- a/openwisp_radius/base/models.py +++ b/openwisp_radius/base/models.py @@ -49,11 +49,11 @@ SmsMessage, find_available_username, generate_sms_token, + get_encoding_format, get_sms_default_valid_until, load_model, prefix_generate_users, validate_csvfile, - get_encoding_format ) from .validators import ipv6_network_validator, password_reset_url_validator @@ -952,7 +952,11 @@ def csvfile_upload( if not csvfile: csvfile = self.csvfile csv_data = csvfile.read() - csv_data = csv_data.decode(get_encoding_format(csv_data)) if isinstance(csv_data, bytes) else csv_data + csv_data = ( + csv_data.decode(get_encoding_format(csv_data)) + if isinstance(csv_data, bytes) + else csv_data + ) reader = csv.reader(StringIO(csv_data), delimiter=',') self.full_clean() self.save() diff --git a/openwisp_radius/receivers.py b/openwisp_radius/receivers.py index a3a999be..c19299d9 100644 --- a/openwisp_radius/receivers.py +++ b/openwisp_radius/receivers.py @@ -1,6 +1,7 @@ """ Receiver functions for django signals (eg: post_save) """ + import logging from celery.exceptions import OperationalError diff --git a/openwisp_radius/tests/static/test_batch_utf16_file2.csv b/openwisp_radius/tests/static/test_batch_utf16_file2.csv index 57961928..dcd4a3fc 100644 --- a/openwisp_radius/tests/static/test_batch_utf16_file2.csv +++ b/openwisp_radius/tests/static/test_batch_utf16_file2.csv @@ -1 +1 @@ -44D1FADD7379,cleartext$D0weL6L8,44D1FADD7379@umoja.com,EAPUSER1,USER1 \ No newline at end of file +44D1FADD7379,cleartext$D0weL6L8,44D1FADD7379@umoja.com,EAPUSER1,USER1 diff --git a/openwisp_radius/tests/test_saml/utils.py b/openwisp_radius/tests/test_saml/utils.py index 3c173780..27a57099 100644 --- a/openwisp_radius/tests/test_saml/utils.py +++ b/openwisp_radius/tests/test_saml/utils.py @@ -25,6 +25,6 @@ def add_outstanding_query(self, session_id, came_from): came_from, ) self.saml_session.save() - self.client.cookies[ - settings.SESSION_COOKIE_NAME - ] = self.saml_session.session_key + self.client.cookies[settings.SESSION_COOKIE_NAME] = ( + self.saml_session.session_key + ) diff --git a/openwisp_radius/tests/test_utils.py b/openwisp_radius/tests/test_utils.py index f8f9d207..0e5b60c7 100644 --- a/openwisp_radius/tests/test_utils.py +++ b/openwisp_radius/tests/test_utils.py @@ -2,7 +2,12 @@ from django.core.exceptions import ValidationError from django.test import override_settings -from ..utils import find_available_username, get_one_time_login_url, validate_csvfile, get_encoding_format +from ..utils import ( + find_available_username, + get_encoding_format, + get_one_time_login_url, + validate_csvfile, +) from . import FileMixin from .mixins import BaseTestCase @@ -23,7 +28,7 @@ def test_validate_file_format(self): 'Unrecognized file format, the supplied file does not look like a CSV file.' in error.exception.message ) - + def test_validate_utf16_file_format(self): utf_16_file_1_format_path = self._get_path('static/test_batch_utf16_file1.csv') assert validate_csvfile(open(utf_16_file_1_format_path, 'rb')) is None @@ -45,18 +50,22 @@ def test_get_encoding_format(self): # UTF-8 encoded data (b'hello world', 'utf-8', "UTF-8 encoded data"), # UTF-16 encoded data with BOM - (b'\xff\xfeh\x00e\x00l\x00l\x00o\x00 \x00w\x00o\x00r\x00l\x00d\x00', 'utf-16', "UTF-16 encoded data with BOM"), + ( + b'\xff\xfeh\x00e\x00l\x00l\x00o\x00 \x00w\x00o\x00r\x00l\x00d\x00', + 'utf-16', + "UTF-16 encoded data with BOM", + ), # Empty data (b'', 'utf-8', "Empty byte data"), # Invalid encoding data - (b'\x80\x81\x82', 'utf-8', "Invalid encoding data") + (b'\x80\x81\x82', 'utf-8', "Invalid encoding data"), ] for data, expected, description in test_cases: with self.subTest(description=description): result = get_encoding_format(data) self.assertEqual(result, expected) - + @override_settings(AUTHENTICATION_BACKENDS=[]) def test_get_one_time_login_url(self): login_url = get_one_time_login_url(None, None) diff --git a/openwisp_radius/utils.py b/openwisp_radius/utils.py index caa29727..701626ba 100644 --- a/openwisp_radius/utils.py +++ b/openwisp_radius/utils.py @@ -130,27 +130,29 @@ def find_available_username(username, users_list, prefix=False): return tmp - def get_encoding_format(csv_data): - # Explicit handling for UTF-16 encodings (check for BOM) if csv_data.startswith(b'\xff\xfe') or csv_data.startswith(b'\xfe\xff'): return 'utf-16' detected_encoding = chardet.detect(csv_data).get('encoding') - if detected_encoding == 'ascii': + if detected_encoding == 'ascii': return 'utf-8' if detected_encoding == 'utf-16le': return "utf-16le" - + return detected_encoding or 'utf-8' def validate_csvfile(csvfile): csv_data = csvfile.read() try: - csv_data = csv_data.decode(get_encoding_format(csv_data)) if isinstance(csv_data, bytes) else csv_data + csv_data = ( + csv_data.decode(get_encoding_format(csv_data)) + if isinstance(csv_data, bytes) + else csv_data + ) except UnicodeDecodeError: raise ValidationError( _(