Ticket #6273: django-passwd.diff

File django-passwd.diff, 1.4 KB (added by toxik, 8 years ago)
  • django/core/management/commands/passwd.py

     
     1from django.core.management.base import BaseCommand, CommandError
     2from django.contrib.auth.models import User
     3import getpass
     4
     5class Command(BaseCommand):
     6    help = "Clone of the UNIX program ``passwd'', for django.contrib.auth."
     7
     8    requires_model_validation = False
     9
     10    def handle(self, *args, **options):
     11        if len(args) > 1:
     12            raise CommandError("need exactly one or zero arguments for username")
     13
     14        if args:
     15            username, = args
     16        else:
     17            username = getpass.getuser()
     18
     19        try:
     20            u = User.objects.get(username=username)
     21        except User.DoesNotExist:
     22            raise CommandError("user %s does not exist" % username)
     23
     24        print "Changing password for user", u.username
     25        p1 = p2 = ""
     26        while "" in (p1, p2) or p1 != p2:
     27            p1 = getpass.getpass()
     28            p2 = getpass.getpass("Password (again): ")
     29            if p1 != p2:
     30                print "Passwords do not match, try again"
     31            elif "" in (p1, p2):
     32                raise CommandError("aborted")
     33
     34        u.set_password(p1)
     35        u.save()
     36
     37        return "Password changed successfully for user", u.username
Back to Top