# Path: account/models.py
# class PasswordHistory(models.Model):
#     """
#     Contains single password history for user.
#     """
#     class Meta:
#         verbose_name = _("password history")
#         verbose_name_plural = _("password histories")
# 
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="password_history", on_delete=models.CASCADE)
#     password = models.CharField(max_length=255)  # encrypted password
#     timestamp = models.DateTimeField(default=timezone.now)  # password creation time


import datetime
import pytz
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from account.models import PasswordHistory
    help = "Create password history for all users without existing history."

    def add_arguments(self, parser):
        parser.add_argument(
            "-d", "--days",
            type=int,
            nargs="?",
            default=10,
            help="age of current password (in days)"
        )
        parser.add_argument(
            "-f", "--force",
            action="store_true",
            help="create new password history for all users, regardless of existing history"
        )

    def handle(self, *args, **options):
        User = get_user_model()
        users = User.objects.all()
        if not options["force"]:
            users = users.filter(password_history=None)

        if not users:
            return "No users found without password history"

        days = options["days"]
        timestamp = datetime.datetime.now(tz=pytz.UTC) - datetime.timedelta(days=days)

        # Create new PasswordHistory on `timestamp`
        PasswordHistory.objects.bulk_create(

# Path: account/models.py
# class PasswordHistory(models.Model):
#     """
#     Contains single password history for user.
#     """
#     class Meta:
#         verbose_name = _("password history")
#         verbose_name_plural = _("password histories")
# 
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="password_history", on_delete=models.CASCADE)
#     password = models.CharField(max_length=255)  # encrypted password
#     timestamp = models.DateTimeField(default=timezone.now)  # password creation time


# Path: account/management/commands/user_password_history.py
import datetime
import pytz
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from account.models import PasswordHistory

    help = "Create password history for all users without existing history."

    def add_arguments(self, parser):
        parser.add_argument(
            "-d", "--days",
            type=int,
            nargs="?",
            default=10,
            help="age of current password (in days)"
        )
        parser.add_argument(
            "-f", "--force",
            action="store_true",
            help="create new password history for all users, regardless of existing history"
        )

    def handle(self, *args, **options):
        User = get_user_model()
        users = User.objects.all()
        if not options["force"]:
            users = users.filter(password_history=None)

        if not users:
            return "No users found without password history"

        days = options["days"]
        timestamp = datetime.datetime.now(tz=pytz.UTC) - datetime.timedelta(days=days)

        # Create new PasswordHistory on `timestamp`
        PasswordHistory.objects.bulk_create(

            [PasswordHistory(user=user, timestamp=timestamp) for user in users]