Coverage for users\management\commands\hash_passwords.py: 0.0%

15 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-08-30 20:58 +0200

1from django.core.management.base import BaseCommand 

2from users.models import Users 

3 

4 

5class Command(BaseCommand): 

6 help = 'Hash all plain-text passwords in the Users table' 

7 

8 def handle(self, *args, **options): 

9 users = Users.objects.all() 

10 updated_count = 0 

11 

12 for user in users: 

13 if user.password and not user.password.startswith('pbkdf2_sha256$'): 

14 # Texto en claro, se necesita hashear 

15 original_password = user.password 

16 user.set_password(original_password) 

17 user.save() 

18 updated_count += 1 

19 self.stdout.write( 

20 self.style.SUCCESS(f'Hashed password for user: {user.username}') 

21 ) 

22 

23 self.stdout.write( 

24 self.style.SUCCESS(f'\nSuccessfully hashed {updated_count} passwords') 

25 )