@@ -23,12 +23,13 @@ def add_arguments(self, parser):
2323 def handle (self , * args , ** options ):
2424 credential_type_id = options ['credential_type_id' ]
2525
26- # Track affected topics for targeted reindexing
27- affected_topics = []
26+ # Pre-flight check and confirmation before opening Solr queue
27+ affected_topics = self . _check_and_confirm_deletion ( * args , ** options )
2828
29+ # If we get here, user confirmed deletion - proceed with operations
2930 queue = SolrQueue ()
3031 with queue :
31- affected_topics = self .delete_credential_type ( * args , ** options )
32+ self ._perform_deletion ( affected_topics , * args , ** options )
3233
3334 # Wait for queue to drain while still in context
3435 eject = 20
@@ -50,10 +51,8 @@ def handle(self, *args, **options):
5051 self .stdout .write ("Performing targeted search index refresh ..." )
5152 self ._refresh_affected_indexes (credential_type_id , affected_topics )
5253
53- def delete_credential_type (self , * args , ** options ):
54- start_time = time .perf_counter ()
55-
56- # get Credential Type ID from input parameters
54+ def _check_and_confirm_deletion (self , * args , ** options ):
55+ """Check credential type exists and get user confirmation"""
5756 credential_type_id = options ['credential_type_id' ]
5857 self .stdout .write ("Deleting credential_type_id: " + credential_type_id )
5958
@@ -65,7 +64,7 @@ def delete_credential_type(self, *args, **options):
6564 credential_type = CredentialType .objects .get (id = credential_type_id )
6665 except CredentialType .DoesNotExist :
6766 self .stdout .write (" ... credential_type_id not found in OrgBook." )
68- return list ( affected_topics )
67+ raise SystemExit ( 1 )
6968
7069 # Find all credentials for this credential type
7170 credentials = Credential .objects .filter (
@@ -113,20 +112,37 @@ def delete_credential_type(self, *args, **options):
113112 ).strip ()
114113 except (EOFError , KeyboardInterrupt ):
115114 self .stdout .write ("\n \n Operation cancelled by user." )
116- return list ( affected_topics )
115+ raise SystemExit ( 0 )
117116
118117 if confirmation .lower () not in ['y' , 'yes' ]:
119118 self .stdout .write (
120119 "Operation cancelled. No changes were made."
121120 )
122- return list ( affected_topics )
121+ raise SystemExit ( 0 )
123122 else :
124123 self .stdout .write (
125124 "\n --force flag provided, skipping confirmation."
126125 )
127126
128127 self .stdout .write ("\n Proceeding with credential type deletion..." )
129128 self .stdout .write ("=" * 60 + "\n " )
129+
130+ return list (affected_topics )
131+
132+ def _perform_deletion (self , affected_topics , * args , ** options ):
133+ """Perform the actual credential type deletion"""
134+ start_time = time .perf_counter ()
135+ credential_type_id = options ['credential_type_id' ]
136+
137+ # Re-fetch the credential type and related data (since confirmation
138+ # happened earlier)
139+ credential_type = CredentialType .objects .get (id = credential_type_id )
140+ credentials = Credential .objects .filter (
141+ credential_type = credential_type
142+ )
143+
144+ # Convert to set for efficient updates (we received it as a list)
145+ affected_topics = set (affected_topics )
130146
131147 # delete credentials from wallet first
132148 if credentials .exists ():
0 commit comments