ck-ntuh-net/mysite/mrn2hash.py

49 lines
1,009 B
Python
Raw Normal View History

2024-12-11 08:15:42 +00:00
import base64
import hashlib
import os
import pandas as pd
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()
from django.db.models import Count
from ck.models import *
def hashptid(mrn, hosp='NTUH'):
ptsalt = (mrn+hosp).upper().encode()
hash_in_bytes = hashlib.md5(ptsalt)
md5 = hash_in_bytes.hexdigest()
hash = base64.b32encode(hash_in_bytes.digest())[:8].decode()
# hash32 = base64.b32encode(hash_in_bytes)[:8].decode()
# hash10 = str(int(hashlib.md5(ptsalt).hexdigest(), 16))[-8:]
return md5, hash
PATIENTS = []
2025-03-28 22:32:50 +00:00
for p in Patient.objects.all().order_by('medical_records'):
2024-12-11 08:15:42 +00:00
md5, hash = hashptid(p.medical_records)
print (p.medical_records, hash)
PATIENTS.append({
'ChartNo': p.medical_records,
'hash': hash,
2025-03-28 22:32:50 +00:00
'gender': p.gender,
'birthday': p.birthday,
2024-12-11 08:15:42 +00:00
})
df = pd.DataFrame(PATIENTS)
df.to_excel('hash.xlsx')