refactor: pyupgrade in passowrd_policy (#26950)

This commit is contained in:
M. Zulqarnain
2021-03-16 14:37:15 +05:00
committed by GitHub
parent dfe6f21e4a
commit 0ca2295e8f
6 changed files with 23 additions and 27 deletions

View File

@@ -3,7 +3,6 @@ Configuration for password_policy Django app
"""
import logging
import six
from dateutil.parser import parse as parse_date
from django.apps import AppConfig
from django.conf import settings
@@ -25,14 +24,14 @@ class PasswordPolicyConfig(AppConfig):
plugin_app = {
PluginSettings.CONFIG: {
ProjectType.LMS: {
SettingsType.PRODUCTION: {PluginSettings.RELATIVE_PATH: u'settings.production'},
SettingsType.COMMON: {PluginSettings.RELATIVE_PATH: u'settings.common'},
SettingsType.DEVSTACK: {PluginSettings.RELATIVE_PATH: u'settings.devstack'},
SettingsType.PRODUCTION: {PluginSettings.RELATIVE_PATH: 'settings.production'},
SettingsType.COMMON: {PluginSettings.RELATIVE_PATH: 'settings.common'},
SettingsType.DEVSTACK: {PluginSettings.RELATIVE_PATH: 'settings.devstack'},
},
ProjectType.CMS: {
SettingsType.PRODUCTION: {PluginSettings.RELATIVE_PATH: u'settings.production'},
SettingsType.COMMON: {PluginSettings.RELATIVE_PATH: u'settings.common'},
SettingsType.DEVSTACK: {PluginSettings.RELATIVE_PATH: u'settings.devstack'},
SettingsType.PRODUCTION: {PluginSettings.RELATIVE_PATH: 'settings.production'},
SettingsType.COMMON: {PluginSettings.RELATIVE_PATH: 'settings.common'},
SettingsType.DEVSTACK: {PluginSettings.RELATIVE_PATH: 'settings.devstack'},
}
}
}
@@ -55,8 +54,8 @@ class PasswordPolicyConfig(AppConfig):
"""
deadline = config.get(setting)
try:
if isinstance(deadline, six.string_types):
if isinstance(deadline, str):
config[setting] = parse_date(deadline)
except (ValueError, OverflowError):
log.exception(u"Could not parse %s password policy rollout value of '%s'.", setting, deadline)
log.exception("Could not parse %s password policy rollout value of '%s'.", setting, deadline)
config[setting] = None

View File

@@ -73,7 +73,7 @@ def enforce_compliance_on_login(user, password):
if now >= deadline: # lint-amnesty, pylint: disable=no-else-raise
raise NonCompliantPasswordException(
HTML(_(
u'{strong_tag_open}We recently changed our password requirements{strong_tag_close}{break_line_tag}'
'{strong_tag_open}We recently changed our password requirements{strong_tag_close}{break_line_tag}'
'Your current password does not meet the new security requirements. We just sent a password-reset '
'message to the email address associated with this account. Thank you for helping us keep your data '
'safe.'
@@ -86,17 +86,17 @@ def enforce_compliance_on_login(user, password):
else:
raise NonCompliantPasswordWarning(
HTML(_(
u'{strong_tag_open}Required Action: Please update your password{strong_tag_close}{break_line_tag}'
u'As of {deadline}, {platform_name} will require all learners to have complex passwords. Your current '
u'password does not meet these requirements. To reset your password, go to to '
u'{anchor_tag_open}Account Settings{anchor_tag_close}.'
'{strong_tag_open}Required Action: Please update your password{strong_tag_close}{break_line_tag}'
'As of {deadline}, {platform_name} will require all learners to have complex passwords. Your current '
'password does not meet these requirements. To reset your password, go to to '
'{anchor_tag_open}Account Settings{anchor_tag_close}.'
)).format(
strong_tag_open=HTML('<strong>'),
strong_tag_close=HTML('</strong>'),
break_line_tag=HTML('<br/>'),
platform_name=settings.PLATFORM_NAME,
deadline=strftime_localized(deadline, DEFAULT_SHORT_DATE_FORMAT),
anchor_tag_open=HTML(u'<a href="{account_settings_url}">').format(
anchor_tag_open=HTML('<a href="{account_settings_url}">').format(
account_settings_url=settings.LMS_ROOT_URL + "/account/settings"
),
anchor_tag_close=HTML('</a>')

View File

@@ -1,10 +1,6 @@
"""
Forms for the password policy app.
"""
import six
from django.contrib import messages
from django.contrib.admin.forms import AdminAuthenticationForm
from django.forms import ValidationError
@@ -21,16 +17,16 @@ class PasswordPolicyAwareAdminAuthForm(AdminAuthenticationForm):
"""
Overrides the clean method to allow for the enforcement of password policy requirements.
"""
cleaned_data = super(PasswordPolicyAwareAdminAuthForm, self).clean() # lint-amnesty, pylint: disable=super-with-arguments
cleaned_data = super().clean()
if password_policy_compliance.should_enforce_compliance_on_login():
try:
password_policy_compliance.enforce_compliance_on_login(self.user_cache, cleaned_data['password'])
except password_policy_compliance.NonCompliantPasswordWarning as e:
# Allow login, but warn the user that they will be required to reset their password soon.
messages.warning(self.request, six.text_type(e))
messages.warning(self.request, str(e))
except password_policy_compliance.NonCompliantPasswordException as e:
# Prevent the login attempt.
raise ValidationError(six.text_type(e)) # lint-amnesty, pylint: disable=raise-missing-from
raise ValidationError(str(e)) # lint-amnesty, pylint: disable=raise-missing-from
return cleaned_data

View File

@@ -4,10 +4,11 @@ Test password policy settings
import datetime
from unittest.mock import patch
from dateutil.parser import parse as parse_date
from django.conf import settings
from django.test import TestCase, override_settings
from mock import patch
import openedx.core.djangoapps.password_policy as password_policy
from openedx.core.djangoapps.password_policy.apps import PasswordPolicyConfig

View File

@@ -3,12 +3,12 @@ Test password policy utilities
"""
from datetime import datetime, timedelta
from unittest.mock import patch
import pytest
import pytz
from dateutil.parser import parse as parse_date
from django.test import TestCase, override_settings
from mock import patch
from openedx.core.djangoapps.password_policy.compliance import (NonCompliantPasswordException,
NonCompliantPasswordWarning,

View File

@@ -1,9 +1,9 @@
"""
Test password policy forms
"""
import pytest
import mock
from unittest import mock
import pytest
from django.forms import ValidationError
from django.test import TestCase
from django.test.utils import override_settings
@@ -20,7 +20,7 @@ class PasswordPolicyAwareAdminAuthFormTests(TestCase):
Tests the custom form for enforcing password policy rules
"""
def setUp(self):
super(PasswordPolicyAwareAdminAuthFormTests, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments
super().setUp()
self.auth_form = PasswordPolicyAwareAdminAuthForm()
self.user = UserFactory.create(username='test_user', password='test_password', is_staff=True)
self.auth_form.cleaned_data = {