This commit is contained in:
Ross
2022-04-07 23:33:58 +01:00
parent 102d692db0
commit fa0ad1c33c
3 changed files with 60 additions and 0 deletions
View File
@@ -0,0 +1,60 @@
import re
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
import pydicom
import six
import os
class Command(BaseCommand):
help = 'Checks dicom files for anonymisation'
#def add_arguments(self, parser):
# parser.add_argument('poll_ids', nargs='+', type=int)
def handle(self, *args, **options):
#for poll_id in options['poll_ids']:
# try:
# poll = Poll.objects.get(pk=poll_id)
# except Poll.DoesNotExist:
# raise CommandError('Poll "%s" does not exist' % poll_id)
# poll.opened = False
# poll.save()
# self.stdout.write(self.style.SUCCESS('Successfully closed poll "%s"' % poll_id))
files = get_all_media()
for file in files:
ds = pydicom.dcmread(file, force=True)
site_codes = ("ref", "rk9", "ra9", "rh8", "rbz", "rba")
for code in site_codes:
for s in (ds.PatientsID, ds.AccessionNumber):
if code in s:
self.stdout.write(self.style.WARNING(f"Bad dicom {file}"))
def get_all_media(exclude=None):
"""
Get all media from MEDIA_ROOT
"""
if not exclude:
exclude = []
media = set()
for root, dirs, files in os.walk(six.text_type(settings.MEDIA_ROOT)):
for name in files:
path = os.path.abspath(os.path.join(root, name))
relpath = os.path.relpath(path, settings.MEDIA_ROOT)
for e in exclude:
if re.match(r'^%s$' % re.escape(e).replace('\\*', '.*'), relpath):
break
else:
media.add(path)
return media