Skip to content

Commit

Permalink
Create delete-identifiers.py
Browse files Browse the repository at this point in the history
Reference: #513
  • Loading branch information
rushirajnenuji committed Dec 13, 2023
1 parent 3296787 commit 7ced84a
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ezidapp/management/commands/delete-identifiers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import csv
import logging
from django.core.management.base import BaseCommand
from django.db.models import Q
import django.conf
import django.db
import django.db.transaction
from ezidapp.models.identifier import Identifier
from ezidapp.management.commands.proc_base import AsyncProcessingCommand
import impl.enqueue

log = logging.getLogger(__name__)

class Command(AsyncProcessingCommand):
help = "Expunge identifiers listed in a CSV file"

def __init__(self):
super().__init__()

def add_arguments(self, parser):
parser.add_argument('csv_file', type=str, help='Path to the CSV file containing identifiers in the first column.')
parser.add_argument('--update-external-services', action='store_true', dest='update_external_services',
help='Update external services after expunging identifiers.')

def run(self, *args, **kwargs):
csv_file_path = kwargs['csv_file']
update_external_services = kwargs['update_external_services']

with open(csv_file_path, 'r') as csv_file:
identifier_reader = csv.reader(csv_file)
identifiers = [row[0] for row in identifier_reader]

qs = Identifier.objects.filter(identifier__in=identifiers).only("identifier")

if not qs:
log.info("No identifiers found for deletion.")
return

for si in qs:
with django.db.transaction.atomic():
impl.enqueue.enqueue(si, "delete", updateExternalServices=update_external_services)
si.delete()

log.info("Identifiers successfully expunged. Script completed")

0 comments on commit 7ced84a

Please sign in to comment.