File: generate_password.py

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (36 lines) | stat: -rw-r--r-- 1,155 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf-8 -*-
import argparse
import string
import secrets
from typing import List

from django.core.management.base import BaseCommand
from django_extensions.management.utils import signalcommand


class Command(BaseCommand):
    help = "Generates a simple new password that can be used for a user password. "
    "Uses Python’s secrets module to generate passwords. Do not use this command to "
    "generate your most secure passwords."

    requires_system_checks: List[str] = []

    def add_arguments(self, parser):
        parser.add_argument(
            "-l", "--length", nargs="?", type=int, default=16, help="Password length."
        )
        parser.add_argument(
            "-c",
            "--complex",
            action=argparse.BooleanOptionalAction,
            help="More complex alphabet, includes punctuation",
        )

    @signalcommand
    def handle(self, *args, **options):
        length = options["length"]

        alphabet = string.ascii_letters + string.digits
        if options["complex"]:
            alphabet += string.punctuation
        return "".join(secrets.choice(alphabet) for i in range(length))