1
from django.core.management.base import BaseCommand, CommandError
2
from django.contrib.auth.models import User
5
class Command(BaseCommand):
6
help = "Change a user's password for django.contrib.auth."
8
requires_model_validation = False
10
def _get_pass(self, prompt="Password: "):
11
p = getpass.getpass(prompt=prompt)
13
raise CommandError("aborted")
16
def handle(self, *args, **options):
18
raise CommandError("need exactly one or zero arguments for username")
23
username = getpass.getuser()
26
u = User.objects.get(username=username)
27
except User.DoesNotExist:
28
raise CommandError("user '%s' does not exist" % username)
30
print "Changing password for user '%s'" % u.username
34
p1, p2 = 1, 2 # To make them initially mismatch.
35
while p1 != p2 and count < MAX_TRIES:
37
p2 = self._get_pass("Password (again): ")
39
print "Passwords do not match. Please try again."
42
if count == MAX_TRIES:
43
raise CommandError("Aborting password change for user '%s' after %s attempts" % (username, count))
48
return "Password changed successfully for user '%s'" % u.username