changeset 814:b14dbbb8eda9

fix sample of ReceivedInstanceCallback for Python
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Feb 2022 08:38:50 +0100
parents 9615fde79f54
children a56c4128c9a2
files Sphinx/source/plugins/python/received-instance-callback.py
diffstat 1 files changed, 10 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Sphinx/source/plugins/python/received-instance-callback.py	Wed Feb 16 18:07:15 2022 +0100
+++ b/Sphinx/source/plugins/python/received-instance-callback.py	Wed Feb 23 08:38:50 2022 +0100
@@ -3,6 +3,7 @@
 from pydicom import dcmread, dcmwrite
 from pydicom.filebase import DicomFileLike
 
+import orthanc
 
 # from https://pydicom.github.io/pydicom/stable/auto_examples/memory_dataset.html
 def write_dataset_to_bytes(dataset):
@@ -10,24 +11,24 @@
         memory_dataset = DicomFileLike(buffer)
         dcmwrite(memory_dataset, dataset)
         memory_dataset.seek(0)
-
         return memory_dataset.read()
 
-
 def ReceivedInstanceCallback(receivedDicom):
-
     dataset = dcmread(BytesIO(receivedDicom))
 
     if dataset.PatientID.startswith('001-'):
+        orthanc.LogWarning('Discard instance')
         return orthanc.ReceivedInstanceCallbackResult.DISCARD, None
 
-    if dataset.PatientID.startswith('002-'):
+    elif dataset.PatientID.startswith('002-'):
+        orthanc.LogWarning('Store source instance as it is')
         return orthanc.ReceivedInstanceCallbackResult.KEEP_AS_IS, None
 
-    dataset.PatientName = str(dataset.PatientName).upper()
-    dataset.PatientID = '002-' + dataset.PatientID
-    dataset.InstitutionName = "MY INSTITUTION"
-
-    return orthanc.ReceivedInstanceCallbackResult.MODIFIED, write_dataset_to_bytes(dataset)
+    else:
+        orthanc.LogWarning('Modify the source instance')
+        dataset.PatientName = str(dataset.PatientName).upper()
+        dataset.PatientID = '002-' + dataset.PatientID
+        dataset.InstitutionName = "MY INSTITUTION"
+        return orthanc.ReceivedInstanceCallbackResult.MODIFIED, write_dataset_to_bytes(dataset)
 
 orthanc.RegisterReceivedInstanceCallback(ReceivedInstanceCallback)