@@ -119,9 +119,9 @@ def add_arguments(self, parser):
119
119
)
120
120
121
121
parser .add_argument (
122
- "--council_restrictions " ,
122
+ "--preferred_councils " ,
123
123
action = "store_true" ,
124
- help = "the data contains a column with council restrictions , e.g Welsh councils." ,
124
+ help = "the data contains a column with councils to select from first , e.g Welsh councils." ,
125
125
)
126
126
127
127
parser .add_argument (
@@ -156,8 +156,8 @@ def init(self, file, options):
156
156
self .column_names .append ("assignment_count" )
157
157
self .usecols .append ("How many sections?" )
158
158
159
- if options ["council_restrictions " ]:
160
- self .column_names .append ("council_restrictions " )
159
+ if options ["preferred_councils " ]:
160
+ self .column_names .append ("preferred_councils " )
161
161
self .usecols .append ("Nations/London" )
162
162
163
163
if options ["debug" ]:
@@ -170,8 +170,8 @@ def get_df(self, filename):
170
170
)
171
171
df .columns = self .column_names
172
172
173
- if "council_restrictions " in df .columns :
174
- df ["council_restrictions " ] = df ["council_restrictions " ].astype (str )
173
+ if "preferred_councils " in df .columns :
174
+ df ["preferred_councils " ] = df ["preferred_councils " ].astype (str )
175
175
176
176
return df
177
177
@@ -330,18 +330,18 @@ def add_users_and_assignments(self, df, response_type, session, rt, options):
330
330
331
331
included_types = []
332
332
included_countries = []
333
- if options ["council_restrictions " ]:
334
- restrictions = row ["council_restrictions " ].split ("," )
333
+ if options ["preferred_councils " ]:
334
+ preferred = row ["preferred_councils " ].split ("," )
335
335
336
- for r in restrictions :
337
- if r .lower () in [
336
+ for p in preferred :
337
+ if p .lower () in [
338
338
"scotland" ,
339
339
"england" ,
340
340
"wales" ,
341
341
"northern ireland" ,
342
342
]:
343
- included_countries .append (r .lower ())
344
- elif r == "London" :
343
+ included_countries .append (p .lower ())
344
+ elif p == "London" :
345
345
included_types .append ("LBO" )
346
346
347
347
councils_to_assign = PublicAuthority .objects .filter (
@@ -350,25 +350,53 @@ def add_users_and_assignments(self, df, response_type, session, rt, options):
350
350
Q (id__in = assigned_councils ) | Q (type = "COMB" ) | Q (do_not_mark = True )
351
351
)
352
352
353
+ preferred_councils = councils_to_assign
354
+ has_preferred = False
353
355
if len (included_types ) > 0 and len (included_countries ) > 0 :
354
- councils_to_assign = councils_to_assign .filter (
356
+ has_preferred = True
357
+ preferred_councils = preferred_councils .filter (
355
358
Q (country__in = included_countries ) | Q (type__in = included_types )
356
359
)
357
360
elif len (included_types ) > 0 :
358
- councils_to_assign = councils_to_assign .filter (type__in = included_types )
361
+ has_preferred = True
362
+ preferred_councils = preferred_councils .filter (type__in = included_types )
359
363
elif len (included_countries ) > 0 :
360
- councils_to_assign = councils_to_assign .filter (
364
+ has_preferred = True
365
+ preferred_councils = preferred_councils .filter (
361
366
country__in = included_countries
362
367
)
363
368
364
- councils_to_assign = councils_to_assign [:num_councils ]
369
+ if has_preferred :
370
+ preferred_councils = preferred_councils [:num_councils ]
371
+ councils_left = num_councils - preferred_councils .count ()
372
+ councils_to_assign = councils_to_assign .exclude (
373
+ id__in = preferred_councils
374
+ )[:councils_left ]
375
+ council_count = preferred_councils .count () + councils_to_assign .count ()
376
+ else :
377
+ councils_to_assign = councils_to_assign [:num_councils ]
378
+ council_count = councils_to_assign .count ()
365
379
366
- if councils_to_assign . count () == 0 :
380
+ if council_count == 0 :
367
381
self .stdout .write (
368
382
f"{ YELLOW } No councils left in { s .title } for { u .email } { NOBOLD } "
369
383
)
370
384
371
385
if options ["make_assignments" ] is True :
386
+ if self .debug :
387
+ self .stdout .write (
388
+ f"{ YELLOW } Assigning { council_count } councils in { s .title } to { u .email } { NOBOLD } "
389
+ )
390
+
391
+ if has_preferred :
392
+ for council in preferred_councils :
393
+ a , created = Assigned .objects .update_or_create (
394
+ user = u ,
395
+ section = s ,
396
+ authority = council ,
397
+ marking_session = session ,
398
+ response_type = rt ,
399
+ )
372
400
for council in councils_to_assign :
373
401
a , created = Assigned .objects .update_or_create (
374
402
user = u ,
@@ -415,7 +443,7 @@ def handle(
415
443
).count ()
416
444
for section in Section .objects .filter (marking_session = session ).all ():
417
445
assigned = Assigned .objects .filter (
418
- section = section , response_type = rt
446
+ section = section , response_type = rt , marking_session = session
419
447
).count ()
420
448
if assigned != council_count :
421
449
self .stdout .write (
0 commit comments