Skip to content

Commit

Permalink
Extract make_random_password
Browse files Browse the repository at this point in the history
  • Loading branch information
bennylope committed Oct 28, 2024
1 parent ed8542b commit c73a0b2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand Down
8 changes: 5 additions & 3 deletions docs/custom_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,18 @@ 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):
def invite_by_email(self, email, sender=None, request=None, **kwargs):
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)
Expand Down
2 changes: 1 addition & 1 deletion src/organizations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

__author__ = "Ben Lopatin"
__email__ = "ben@benlopatin.com"
__version__ = "2.4.1"
__version__ = "2.5.0"
23 changes: 19 additions & 4 deletions src/organizations/backends/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 7 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -38,7 +39,7 @@ extras = tests
[testenv:flake8]
basepython=python3
deps=
flake8==3.8.3
flake8==3.12.7
commands=
flake8 src/organizations tests

Expand Down

0 comments on commit c73a0b2

Please sign in to comment.