@@ -54,15 +54,11 @@ def handle(self, *args, **options):
5454 if self .is_not_holding_clinic (row ):
5555 clinic , clinic_created = self .find_or_create_clinic (row )
5656 if clinic_created :
57- self . stdout . write ( f" { clinic } created" )
57+ logger . info ( "%s created", clinic )
5858
5959 appt , appt_created = self .update_or_create_appointment (
6060 row , clinic
6161 )
62- logger .info (
63- "%s %s" , appt , ("created" if appt_created else "updated" )
64- )
65-
6662 logger .info ("Processed %s rows from %s" , len (data_frame ), blob .name )
6763 logger .info ("Create Appointments command finished successfully" )
6864 except Exception as e :
@@ -108,48 +104,51 @@ def update_or_create_appointment(
108104 appointment = Appointment .objects .filter (nbss_id = row ["Appointment ID" ]).first ()
109105
110106 if self .is_new_booking (row , appointment ):
111- return (
112- Appointment .objects .create (
113- nbss_id = row ["Appointment ID" ],
114- nhs_number = row ["NHS Num" ],
115- number = row ["Screen Appt num" ],
116- batch_id = self .handle_aliased_column ("Batch ID" , "BatchID" , row ),
117- clinic = clinic ,
118- episode_started_at = datetime .strptime (
119- row ["Episode Start" ], "%Y%m%d"
120- ).replace (tzinfo = TZ_INFO ),
121- episode_type = self .handle_aliased_column (
122- "Episode Type" , "Epsiode Type" , row
123- ),
124- starts_at = self .appointment_date_and_time (row ),
125- status = row ["Status" ],
126- booked_by = row ["Booked By" ],
127- booked_at = self .workflow_action_date_and_time (
128- row ["Action Timestamp" ]
129- ),
130- assessment = (
131- self .handle_aliased_column (
132- "Screen or Assess" , "Screen or Asses" , row
133- )
134- == "A"
135- ),
107+ new_appointment = Appointment .objects .create (
108+ nbss_id = row ["Appointment ID" ],
109+ nhs_number = row ["NHS Num" ],
110+ number = row ["Screen Appt num" ],
111+ batch_id = self .handle_aliased_column ("Batch ID" , "BatchID" , row ),
112+ clinic = clinic ,
113+ episode_started_at = datetime .strptime (
114+ row ["Episode Start" ], "%Y%m%d"
115+ ).replace (tzinfo = TZ_INFO ),
116+ episode_type = self .handle_aliased_column (
117+ "Episode Type" , "Epsiode Type" , row
118+ ),
119+ starts_at = self .appointment_date_and_time (row ),
120+ status = row ["Status" ],
121+ booked_by = row ["Booked By" ],
122+ booked_at = self .workflow_action_date_and_time (row ["Action Timestamp" ]),
123+ assessment = (
124+ self .handle_aliased_column (
125+ "Screen or Assess" , "Screen or Asses" , row
126+ )
127+ == "A"
136128 ),
137- True ,
138129 )
130+ logger .info ("%s created" , new_appointment )
131+ return (new_appointment , True )
139132 elif self .is_cancelling_existing_appointment (row , appointment ):
140133 appointment .status = AppointmentStatusChoices .CANCELLED .value
141134 appointment .cancelled_by = row ["Cancelled By" ]
142135 appointment .cancelled_at = self .workflow_action_date_and_time (
143136 row ["Action Timestamp" ]
144137 )
145138 appointment .save ()
139+ logger .info ("%s cancelled" , appointment )
146140 elif self .is_completed_appointment (row , appointment ):
147141 appointment .status = row ["Status" ]
148142 appointment .attended_not_screened = row ["Attended Not Scr" ]
149143 appointment .completed_at = self .workflow_action_date_and_time (
150144 row ["Action Timestamp" ]
151145 )
152146 appointment .save ()
147+ logger .info ("%s marked completed (%s)" , appointment , row .get ("Status" ))
148+ elif appointment is None :
149+ logger .info (
150+ "No Appointment record found for NBSS ID: %s" , row .get ("Appointment ID" )
151+ )
153152
154153 return (appointment , False )
155154
0 commit comments