From c73a0b2c3a338af3bd40b28395df84d8b14de620 Mon Sep 17 00:00:00 2001 From: Ben Lopatin Date: Mon, 28 Oct 2024 14:43:32 -0400 Subject: [PATCH] Extract make_random_password --- HISTORY.rst | 7 +++++++ docs/custom_usage.rst | 8 +++++--- src/organizations/__init__.py | 2 +- src/organizations/backends/defaults.py | 23 +++++++++++++++++++---- tox.ini | 13 +++++++------ 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 87a378b..0744ec6 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,13 @@ History ======= +2.5.0 +----- + +* Drop Python 3.8 support +* Add Python 3.13 support +* Add Django 5.1 support + 2.4.1 ----- diff --git a/docs/custom_usage.rst b/docs/custom_usage.rst index cbb44d8..96be703 100644 --- a/docs/custom_usage.rst +++ b/docs/custom_usage.rst @@ -171,7 +171,7 @@ with the contents of a freshly generated UUID. In the example accounts app you would create a file named `backends.py`.:: - from organizations.backends.defaults import InvitationBackend + from organizations.backends.defaults import InvitationBackend, make_random_password class CustomInvitations(InvitationBackend): @@ -179,8 +179,10 @@ In the example accounts app you would create a file named `backends.py`.:: try: user = self.user_model.objects.get(email=email) except self.user_model.DoesNotExist: - user = self.user_model.objects.create(email=email, - password=self.user_model.objects.make_random_password()) + user = self.user_model.objects.create( + email=email, + password=make_random_password(), + ) user.is_active = False user.save() self.send_invitation(user, sender, **kwargs) diff --git a/src/organizations/__init__.py b/src/organizations/__init__.py index 8824ff5..24304d2 100644 --- a/src/organizations/__init__.py +++ b/src/organizations/__init__.py @@ -2,4 +2,4 @@ __author__ = "Ben Lopatin" __email__ = "ben@benlopatin.com" -__version__ = "2.4.1" +__version__ = "2.5.0" diff --git a/src/organizations/backends/defaults.py b/src/organizations/backends/defaults.py index 18571f8..8ec9838 100644 --- a/src/organizations/backends/defaults.py +++ b/src/organizations/backends/defaults.py @@ -19,6 +19,7 @@ from django.template import loader from django.urls import path from django.urls import reverse +from django.utils.crypto import get_random_string from django.utils.translation import gettext as _ from organizations.backends.forms import UserRegistrationForm @@ -28,6 +29,20 @@ from organizations.utils import model_field_attr +def make_random_password( + length=10, + allowed_chars="abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789", +): + """ + Generate a random password with the given length and given + allowed_chars. The default value of allowed_chars does not have "I" or + "O" or letters and digits that look similar -- just to avoid confusion. + + (Pulled directly from since-deprecated manager method in Django source) + """ + return get_random_string(length, allowed_chars) + + class BaseBackend: """ Base backend class for registering and inviting users to an organization @@ -215,7 +230,7 @@ def register_by_email(self, email, sender=None, request=None, **kwargs): user = self.user_model.objects.create( username=self.get_username(), email=email, - password=self.user_model.objects.make_random_password(), + password=make_random_password(), ) user.is_active = False user.save() @@ -248,7 +263,7 @@ def create_view(self, request): user = self.user_model.objects.create( username=self.get_username(), email=form.cleaned_data["email"], - password=self.user_model.objects.make_random_password(), + password=make_random_password(), ) user.is_active = False user.save() @@ -317,11 +332,11 @@ def invite_by_email(self, email, sender=None, request=None, **kwargs): user = self.user_model.objects.create( username=self.get_username(), email=email, - password=self.user_model.objects.make_random_password(), + password=make_random_password(), ) else: user = self.user_model.objects.create( - email=email, password=self.user_model.objects.make_random_password() + email=email, password=make_random_password() ) user.is_active = False user.save() diff --git a/tox.ini b/tox.ini index dec1011..2c86431 100644 --- a/tox.ini +++ b/tox.ini @@ -1,17 +1,18 @@ [tox] envlist = flake8, - py{38,39,310}-django{32}, - py{38,39}-django{42}, - py{310,311,312}-django{42,51} + py{39,310}-django{32}, + py{39}-django{42}, + py{310,311,312}-django{42} + py{310,311,312,313}-django{51} [gh-actions] python = - 3.8: py38 3.9: py39 3.10: py310 3.11: py311 3.12: py312 + 3.13: py313 [build-system] build-backend = "hatchling.build" @@ -23,11 +24,11 @@ setenv = PYTHONPATH = {toxinidir}:{toxinidir}/organizations commands = pytest {posargs} --cov=organizations basepython = - py38: python3.8 py39: python3.9 py310: python3.10 py311: python3.11 py312: python3.12 + py313: python3.13 deps = hatch>=1.7.0 django32: Django>=3.2,<4 @@ -38,7 +39,7 @@ extras = tests [testenv:flake8] basepython=python3 deps= - flake8==3.8.3 + flake8==3.12.7 commands= flake8 src/organizations tests