Skip to content

Commit 5ff80f1

Browse files
add record count field to extract model
1 parent ea5f11d commit 5ff80f1

File tree

8 files changed

+28
-34
lines changed

8 files changed

+28
-34
lines changed

manage_breast_screening/notifications/management/commands/create_appointments.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ def handle(self, *args, **options):
5454

5555
data_frame = self.raw_data_to_data_frame(blob_content)
5656

57-
extract = Extract.objects.create(sequence_number = self.get_data_frame_sequence_number(blob_content),
57+
file_headers = self.get_file_header(blob_content)
58+
59+
extract = Extract.objects.create(sequence_number = int(file_headers[1].strip()),
5860
filename = blob.name,
59-
bso_code = data_frame.iloc[0]["BSO"])
60-
61+
bso_code = data_frame.iloc[0]["BSO"],
62+
record_count = file_headers[4])
63+
6164
for idx, row in data_frame.iterrows():
6265
if self.is_not_holding_clinic(row):
6366
clinic, clinic_created = self.find_or_create_clinic(row)
@@ -68,26 +71,13 @@ def handle(self, *args, **options):
6871
row, clinic, extract
6972
)
7073

71-
extract.appointments.add(appt) if appt is not None else None
74+
extract.rows.add(appt) if appt is not None else None
7275

7376
logger.info("Processed %s rows from %s", len(data_frame), blob.name)
7477

7578
def is_not_holding_clinic(self, row):
7679
return row.get("Holding Clinic") != "Y"
7780

78-
def get_data_frame_sequence_number(self, raw_data: str) -> str:
79-
data_frame = pandas.read_table(
80-
io.StringIO(raw_data),
81-
dtype="str",
82-
encoding="ASCII",
83-
engine="python",
84-
header=0,
85-
keep_default_na=False,
86-
sep="|",
87-
skipfooter=1,
88-
)
89-
return data_frame.columns.values[1]
90-
9181
def raw_data_to_data_frame(self, raw_data: str) -> pandas.DataFrame:
9282
return pandas.read_table(
9383
io.StringIO(raw_data),
@@ -99,6 +89,9 @@ def raw_data_to_data_frame(self, raw_data: str) -> pandas.DataFrame:
9989
sep="|",
10090
skipfooter=1,
10191
)
92+
93+
def get_file_header(self, raw_data: str) -> pandas.Series:
94+
return raw_data.split("\n")[0].replace('"', '').split('|')
10295

10396
def find_or_create_clinic(self, row: pandas.Series) -> tuple[Clinic, bool]:
10497
return Clinic.objects.get_or_create(

manage_breast_screening/notifications/migrations/0022_extract.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 5.2.7 on 2025-11-19 08:28
1+
# Generated by Django 5.2.8 on 2025-11-21 15:41
22

33
import uuid
44
from django.db import migrations, models
@@ -19,7 +19,8 @@ class Migration(migrations.Migration):
1919
('filename', models.CharField(max_length=255)),
2020
('bso_code', models.CharField(max_length=255)),
2121
('sequence_number', models.IntegerField()),
22-
('appointments', models.ManyToManyField(blank=True, to='notifications.appointment')),
22+
('record_count', models.IntegerField()),
23+
('rows', models.ManyToManyField(blank=True, to='notifications.appointment')),
2324
],
2425
),
2526
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0021_alter_clinic_code_clinic_notificatio_code_55dbdb_idx_and_more
1+
0022_extract

manage_breast_screening/notifications/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,12 @@ class Extract(models.Model):
224224
"""
225225

226226
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
227-
appointments = models.ManyToManyField("notifications.Appointment", blank=True)
227+
rows = models.ManyToManyField("notifications.Appointment", blank=True)
228228
created_at = models.DateTimeField(auto_now_add=True)
229229
filename = models.CharField(max_length=255, null=False)
230230
bso_code = models.CharField(max_length=255, null=False)
231231
sequence_number = models.IntegerField(null=False)
232+
record_count = models.IntegerField(null=False)
232233
constraints = [
233234
models.UniqueConstraint(
234235
fields=["bso_code", "sequence_number"], name="unique_code_bso_code"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000003"
1+
"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"
22
"NBSSAPPT_FLDS"|"Sequence"|"BSO"|"Action"|"Clinic Code"|"Holding Clinic"|"Status"|"Attended Not Scr"|"Appointment ID"|"NHS Num"|"Epsiode Type"|"Episode Start"|"BatchID"|"Screen or Asses"|"Screen Appt num"|"Booked By"|"Cancelled By"|"Appt Date"|"Appt Time"|"Location"|"Clinic Name"|"Clinic Name (Let)"|"Clinic Address 1"|"Clinic Address 2"|"Clinic Address 3"|"Clinic Address 4"|"Clinic Address 5"|"Postcode"|"Action Timestamp"
33
"NBSSAPPT_DATA"|"000001"|"KMK"|"B"|"BU003"|"N"|"B"|"N"|"BU003-67215-RA1-DN-Z2222-1"|"9449304424"|"S"|"20250102"|"KMKS02441"|"S"|""|"C"|"H"|"20250110"|"0845"|"MKGH"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"MILTON KEYNES HOSPITAL"|"STANDING WAY"|"MILTON KEYNES"|"MK6 5LD"|"MK6 5LD"|"20250123-121433"
4-
"NBSSAPPT_END"|"00000013"|"20250128"|"17:09:22"|"000003"
4+
"NBSSAPPT_END"|"00000013"|"20250128"|"17:09:22"|"000001"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000003"
1+
"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"
22
"NBSSAPPT_FLDS"|"Sequence"|"BSO"|"Action"|"Clinic Code"|"Holding Clinic"|"Status"|"Attended Not Scr"|"Appointment ID"|"NHS Num"|"Epsiode Type"|"Episode Start"|"BatchID"|"Screen or Asses"|"Screen Appt num"|"Booked By"|"Cancelled By"|"Appt Date"|"Appt Time"|"Location"|"Clinic Name"|"Clinic Name (Let)"|"Clinic Address 1"|"Clinic Address 2"|"Clinic Address 3"|"Clinic Address 4"|"Clinic Address 5"|"Postcode"|"Action Timestamp"
33
"NBSSAPPT_DATA"|"000001"|"KMK"|"C"|"BU003"|"N"|"C"|"N"|"BU003-67215-RA1-DN-Z2222-1"|"9449304424"|"S"|"20250102"|"KMKS02441"|"S"|""|"C"|"H"|"20250110"|"0845"|"MKGH"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"MILTON KEYNES HOSPITAL"|"STANDING WAY"|"MILTON KEYNES"|"MK6 5LD"|"MK6 5LD"|"20250123-121433"
4-
"NBSSAPPT_END"|"00000013"|"20250128"|"17:09:22"|"000003"
4+
"NBSSAPPT_END"|"00000013"|"20250128"|"17:09:22"|"000001"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000003"
1+
"NBSSAPPT_HDR"|"00000013"|"20250128"|"170922"|"000001"
22
"NBSSAPPT_FLDS"|"Sequence"|"BSO"|"Action"|"Clinic Code"|"Holding Clinic"|"Status"|"Attended Not Scr"|"Appointment ID"|"NHS Num"|"Epsiode Type"|"Episode Start"|"BatchID"|"Screen or Asses"|"Screen Appt num"|"Booked By"|"Cancelled By"|"Appt Date"|"Appt Time"|"Location"|"Clinic Name"|"Clinic Name (Let)"|"Clinic Address 1"|"Clinic Address 2"|"Clinic Address 3"|"Clinic Address 4"|"Clinic Address 5"|"Postcode"|"Action Timestamp"
33
"NBSSAPPT_DATA"|"000001"|"KMK"|"C"|"BU003"|"N"|"D"|"N"|"BU003-67215-RA1-DN-Z2222-1"|"9449304424"|"S"|"20250102"|"KMKS02441"|"S"|""|"C"|"H"|"20250110"|"0845"|"MKGH"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"BREAST CARE UNIT"|"MILTON KEYNES HOSPITAL"|"STANDING WAY"|"MILTON KEYNES"|"MK6 5LD"|"MK6 5LD"|"20250123-121433"
4-
"NBSSAPPT_END"|"00000013"|"20250128"|"17:09:22"|"000003"
4+
"NBSSAPPT_END"|"00000013"|"20250128"|"17:09:22"|"000001"

manage_breast_screening/notifications/tests/management/commands/test_create_appointments.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from azure.storage.blob import BlobProperties
88
from django.core.management.base import CommandError
99

10-
from psycopg.errors import NotNullViolation
11-
1210
from manage_breast_screening.notifications.management.commands.create_appointments import (
1311
Command,
1412
)
@@ -129,7 +127,7 @@ def test_handle_creates_records(self):
129127
assert appointments[1].assessment is True
130128

131129
assert Extract.objects.count() == 1
132-
assert Extract.objects.first().appointments.count() == 2
130+
assert Extract.objects.first().rows.count() == 2
133131

134132
def test_handles_holding_clinics(self):
135133
"""Test does not create appointments for valid NBSS data marked as a Holding Clinic"""
@@ -340,22 +338,23 @@ def test_create_extract_and_cancel(self):
340338
Command().handle(**{"date_str": today_dirname})
341339

342340
assert Extract.objects.count() == 1
343-
assert Extract.objects.first().appointments.count() == 1
344-
assert Extract.objects.first().appointments
341+
assert Extract.objects.first().rows.count() == 1
342+
assert Extract.objects.first().rows
345343
assert Extract.objects.first().sequence_number == 13
346344
assert Extract.objects.first().bso_code == "KMK"
347345
assert Extract.objects.first().filename == f"{today_dirname}/ABC_20251118150721_APPT_101.dat"
346+
assert Extract.objects.first().record_count == 1
348347

349348
with stored_blob_data(today_dirname, [CANCELLED_SINGLE_APPOINTMENT_FILE]):
350349
Command().handle(**{"date_str": today_dirname})
351350

352351
assert Extract.objects.count() == 2
353352

354-
assert Extract.objects.all()[0].appointments.get().id == Extract.objects.all()[1].appointments.get().id
353+
assert Extract.objects.all()[0].rows.get().id == Extract.objects.all()[1].rows.get().id
355354

356355
with stored_blob_data(today_dirname, [COMPLETED_SINGLE_APPOINTMENT_FILE]):
357356
Command().handle(**{"date_str": today_dirname})
358357

359358
assert Extract.objects.count() == 3
360359

361-
assert Extract.objects.all()[0].appointments.get().id == Extract.objects.all()[2].appointments.get().id
360+
assert Extract.objects.all()[0].rows.get().id == Extract.objects.all()[2].rows.get().id

0 commit comments

Comments
 (0)