Fix Django 5.x compatibility: replace removed make_random_password() method

Problem

The application is using User.objects.make_random_password() which was deprecated in Django 4.2+ and completely removed in Django 5.x. This causes an AttributeError when trying to reset passwords:

AttributeError: 'UserManager' object has no attribute 'make_random_password'

Updated: Further investigation revealed this is actually a Django 5.x compatibility issue where the method was completely removed, not just moved.

Root Cause

The make_random_password() method was:

  1. Django 4.2: Moved from User.objects.make_random_password() to User.make_random_password()
  2. Django 5.x: Completely removed from Django core

Solution

Replace User.objects.make_random_password() with get_random_string(12) from django.utils.crypto, which is the recommended approach for generating random passwords in modern Django.

Files Changed

  • ivatar/ivataraccount/views.py:
    • Added import for get_random_string
    • Updated password generation to use get_random_string(12)
  • ivatar/ivataraccount/test_views.py:
    • Added comprehensive test test_password_reset_w_confirmed_mail_no_password()
    • Test covers the specific failing scenario: user with confirmed email but no password set

Test Coverage

The new test specifically verifies:

  • Users with confirmed emails but unusable passwords (starting with "!")
  • Password reset functionality works without AttributeError
  • Random password is properly generated and set
  • Email is correctly assigned to user object
  • Reset email is sent successfully
  • All existing password reset tests continue to pass

Impact

  • Fixes password reset functionality for Django 5.x installations
  • Maintains backward compatibility with Django 4.2+
  • No breaking changes to existing behavior
  • Comprehensive test coverage prevents regression
Edited by Oliver Falk