@@ -194,81 +194,95 @@ def form_valid(self, form):
194
194
stage = get_object_or_404 (ResponseType , type = data ["stage" ])
195
195
is_multi = question .question_type == "multiple_choice"
196
196
197
+ counts = {"updated" : 0 , "added" : 0 , "deleted" : 0 }
197
198
with transaction .atomic ():
198
199
for index , row in form .responses_df .iterrows ():
199
200
answer = row ["answer" ].strip ()
200
- if answer == "-" :
201
- continue
202
-
203
- if is_multi :
204
- answers = answer .split ("|" )
205
-
206
201
authority = PublicAuthority .objects .get (
207
202
name = row ["authority" ],
208
203
marking_session = self .request .current_session ,
209
204
)
210
- if not is_multi :
211
- option = Option .objects .get (question = question , description = answer )
205
+
206
+ if answer != "-" :
207
+ if is_multi :
208
+ answers = answer .split ("|" )
209
+
210
+ if not is_multi :
211
+ option = Option .objects .get (
212
+ question = question , description = answer
213
+ )
212
214
213
215
try :
214
216
response = Response .objects .get (
215
217
question = question , response_type = stage , authority = authority
216
218
)
217
- changed = False
218
- opts = {}
219
- for col in [
220
- "page_number" ,
221
- "evidence" ,
222
- "public_notes" ,
223
- "private_notes" ,
224
- ]:
225
- val = row [col ]
226
- if pd .isna (val ):
227
- val = None
228
- if col == "private_notes" :
229
- val = ""
230
- if val != getattr (response , col ):
231
- opts [col ] = val
219
+ if answer == "-" :
220
+ response .delete ()
221
+ counts ["deleted" ] += 1
222
+ else :
223
+ changed = False
224
+ opts = {}
225
+ for col in [
226
+ "page_number" ,
227
+ "evidence" ,
228
+ "public_notes" ,
229
+ "private_notes" ,
230
+ ]:
231
+ val = row [col ]
232
+ if pd .isna (val ):
233
+ val = None
234
+ if col == "private_notes" :
235
+ val = ""
236
+ if val != getattr (response , col ):
237
+ opts [col ] = val
238
+ changed = True
239
+ if not is_multi and response .option != option :
232
240
changed = True
233
- if not is_multi and response .option != option :
234
- changed = True
235
- opts ["option" ] = option
241
+ opts ["option" ] = option
242
+
243
+ if changed :
244
+ counts ["updated" ] += 1
245
+ response .user = self .request .user
246
+ for k , v in opts .items ():
247
+ setattr (response , k , v )
248
+ response .save ()
249
+
250
+ if is_multi :
251
+ response .multi_option .clear ()
252
+ for a in answers :
253
+ option = Option .objects .get (
254
+ question = question , description = a
255
+ )
256
+ response .multi_option .add (option .id )
236
257
237
- if changed :
238
- response .user = self .request .user
239
- for k , v in opts .items ():
240
- setattr (response , k , v )
241
- response .save ()
258
+ except Response .DoesNotExist :
259
+ if answer != "-" :
260
+ counts ["added" ] += 1
261
+ opts = {
262
+ "question" : question ,
263
+ "response_type" : stage ,
264
+ "authority" : authority ,
265
+ "user" : self .request .user ,
266
+ }
267
+ for col in ["page_number" , "evidence" , "public_notes" ]:
268
+ if pd .isna (row [col ]) is False :
269
+ opts [col ] = row [col ]
270
+ if not is_multi :
271
+ opts ["option" ] = option
272
+
273
+ response = Response .objects .create (** opts )
242
274
243
275
if is_multi :
244
- response .multi_option .clear ()
245
276
for a in answers :
246
277
option = Option .objects .get (
247
278
question = question , description = a
248
279
)
249
280
response .multi_option .add (option .id )
250
281
251
- except Response .DoesNotExist :
252
- opts = {
253
- "question" : question ,
254
- "response_type" : stage ,
255
- "authority" : authority ,
256
- "user" : self .request .user ,
257
- }
258
- for col in ["page_number" , "evidence" , "public_notes" ]:
259
- if pd .isna (row [col ]) is False :
260
- opts [col ] = row [col ]
261
- if not is_multi :
262
- opts ["option" ] = option
263
-
264
- response = Response .objects .create (** opts )
265
-
266
- if is_multi :
267
- for a in answers :
268
- option = Option .objects .get (
269
- question = question , description = a
270
- )
271
- response .multi_option .add (option .id )
272
-
273
282
messages .add_message (self .request , messages .SUCCESS , "Question updated!" )
283
+ messages .add_message (
284
+ self .request ,
285
+ messages .INFO ,
286
+ f"{ counts ['updated' ]} updated, { counts ['added' ]} added, { counts ['deleted' ]} deleted" ,
287
+ )
274
288
return super ().form_valid (form )
0 commit comments