1414"""Command subclasses and argparse helpers for django-ca."""
1515
1616import abc
17- import argparse
18- import typing
19- from collections .abc import Callable
2017from datetime import UTC , datetime , timedelta
21- from typing import Any
18+ from typing import TYPE_CHECKING , Any , ClassVar
2219
2320from cryptography import x509
2421from cryptography .x509 .oid import AuthorityInformationAccessOID
4441 ConfigurableExtensionType ,
4542)
4643
47- if typing .TYPE_CHECKING :
44+ if TYPE_CHECKING :
45+ from celery .local import Proxy # celery.local is defined in our stubs
4846 from django_stubs_ext import StrOrPromise
4947
5048
@@ -381,7 +379,7 @@ class BaseSignCertCommand(UsePrivateKeyMixin, BaseSignCommand, metaclass=abc.ABC
381379 """Base class for commands signing certificates (sign_cert, resign_cert)."""
382380
383381 add_extensions_help = "" # concrete classes should set this
384- subject_help : typing . ClassVar [str ] # concrete classes should set this
382+ subject_help : ClassVar [str ] # concrete classes should set this
385383
386384 def add_base_args (self , parser : CommandParser , no_default_ca : bool = False ) -> ArgumentGroup :
387385 """Add common arguments for signing certificates."""
@@ -584,10 +582,10 @@ class GenerateCommandBase(UsePrivateKeyMixin, BaseCommand):
584582 """Base class for commands to generate CRLs and OCSP keys."""
585583
586584 what : str
587- single_task : Callable [... , Any ]
588- multiple_task : Callable [... , Any ]
585+ single_task : "Proxy[[UseCertificateAuthorityTaskArgs] , Any]"
586+ multiple_task : "Proxy[[UseCertificateAuthoritiesTaskArgs] , Any]"
589587
590- def add_arguments (self , parser : argparse . ArgumentParser ) -> None :
588+ def add_arguments (self , parser : CommandParser ) -> None :
591589 parser .add_argument (
592590 "serials" ,
593591 metavar = "SERIAL" ,
@@ -614,14 +612,23 @@ def handle(self, serials: list[str], exclude: list[str], force: bool, **options:
614612 raise CommandError ("Cannot name serials and exclude list at the same time." )
615613
616614 if len (serials ) == 1 :
617- ca , key_backend_options = self .get_key_backend_options (serials [0 ], options )
618-
619- data = UseCertificateAuthorityTaskArgs (
620- serial = ca .serial ,
621- force = force ,
622- key_backend_options = key_backend_options .model_dump (mode = "json" , exclude_unset = True ),
623- )
624- run_task (self .single_task , data )
615+ try :
616+ ca , key_backend_options = self .get_key_backend_options (serials [0 ], options )
617+
618+ single_data = UseCertificateAuthorityTaskArgs (
619+ serial = ca .serial ,
620+ force = force ,
621+ key_backend_options = key_backend_options .model_dump (mode = "json" , exclude_unset = True ),
622+ )
623+
624+ run_task (self .single_task , single_data )
625+ except Exception as ex :
626+ # Exceptions only happen if Celery is not used or the connection to the broker fails
627+ raise CommandError (ex ) from ex
625628 else :
626- data = UseCertificateAuthoritiesTaskArgs (serials = serials , exclude = exclude , force = force )
627- run_task (self .multiple_task , data )
629+ multiple_data = UseCertificateAuthoritiesTaskArgs (serials = serials , exclude = exclude , force = force )
630+ try :
631+ run_task (self .multiple_task , multiple_data )
632+ except Exception as ex : # pragma: no cover # does not usually happen
633+ # Exceptions only happen if Celery is not used or the connection to the broker fails
634+ raise CommandError (ex ) from ex
0 commit comments