(self, *args, **options)
| 37 | ) |
| 38 | |
| 39 | def handle(self, *args, **options): |
| 40 | if options["username"]: |
| 41 | username = options["username"] |
| 42 | else: |
| 43 | username = getpass.getuser() |
| 44 | |
| 45 | try: |
| 46 | u = UserModel._default_manager.using(options["database"]).get( |
| 47 | **{UserModel.USERNAME_FIELD: username} |
| 48 | ) |
| 49 | except UserModel.DoesNotExist: |
| 50 | raise CommandError("user '%s' does not exist" % username) |
| 51 | |
| 52 | self.stdout.write("Changing password for user '%s'" % u) |
| 53 | |
| 54 | MAX_TRIES = 3 |
| 55 | count = 0 |
| 56 | p1, p2 = 1, 2 # To make them initially mismatch. |
| 57 | password_validated = False |
| 58 | while (p1 != p2 or not password_validated) and count < MAX_TRIES: |
| 59 | p1 = self._get_pass() |
| 60 | p2 = self._get_pass("Password (again): ") |
| 61 | if p1 != p2: |
| 62 | self.stdout.write("Passwords do not match. Please try again.") |
| 63 | count += 1 |
| 64 | # Don't validate passwords that don't match. |
| 65 | continue |
| 66 | try: |
| 67 | validate_password(p2, u) |
| 68 | except ValidationError as err: |
| 69 | self.stderr.write("\n".join(err.messages)) |
| 70 | count += 1 |
| 71 | else: |
| 72 | password_validated = True |
| 73 | |
| 74 | if count == MAX_TRIES: |
| 75 | raise CommandError( |
| 76 | "Aborting password change for user '%s' after %s attempts" % (u, count) |
| 77 | ) |
| 78 | |
| 79 | u.set_password(p1) |
| 80 | u.save() |
| 81 | |
| 82 | return "Password changed successfully for user '%s'" % u |
nothing calls this directly
no test coverage detected