Trigger a user password change

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from django.contrib.auth.models import User

# Save the original method
old_set_password = User.set_password

def set_password(user, raw_password):
    if user.id == None:
        # It's a new user. We must save the django user account first.
        user.save()

    #
    # Do something with the user obejct and the given raw_password ;)
    #

    # Use the original method to set the django User password:
    old_set_password(user, raw_password)

# Replace the method
User.set_password = set_password

More like this

  1. Database migration and dump/load script by akaihola 4 years, 10 months ago
  2. Expire page from cache by mattgrayson 3 years, 6 months ago
  3. Use crypt instead of sha1 as password hash algorithm by akaihola 4 years, 5 months ago
  4. Run model validation before saving a model instance by buriy 1 year ago
  5. "Autoconnect" model decorator, easy pre_save and post_save signal connection by bendavis78 1 year, 6 months ago

Comments

gamesbook (on October 27, 2010):

Its not clear how to make use of this snippet? What is required in order to integrate into Django (and the Admin site)?

#

jedie (on October 27, 2010):

In PyLucid i put this code into the userprofile model, see: http://github.com/jedie/PyLucid/blob/master/pylucid_project/apps/pylucid/models/userprofile.py

This monkey-patch can be put in every file witch would be one time imported.

#

(Forgotten your password?)