Skip to content

Commit a5647f5

Browse files
committed
allow volunteer import data to have council restrictions
so you can limit people to e.g. Welsh councils.
1 parent c044715 commit a5647f5

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

crowdsourcer/management/commands/import_volunteers.py

+40-3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def add_arguments(self, parser):
112112
help="use the section count in the csv and not the map",
113113
)
114114

115+
parser.add_argument(
116+
"--council_restrictions",
117+
action="store_true",
118+
help="the data contains a column with council restrictions, e.g Welsh councils.",
119+
)
120+
115121
parser.add_argument(
116122
"--add_users", action="store_true", help="add users to database"
117123
)
@@ -137,6 +143,10 @@ def init(self, file, options):
137143
self.column_names.append("section_count")
138144
self.usecols.append("How many sections?")
139145

146+
if options["council_restrictions"]:
147+
self.column_names.append("council_restrictions")
148+
self.usecols.append("Nations/London")
149+
140150
def get_df(self, filename):
141151
df = pd.read_csv(
142152
filename,
@@ -281,13 +291,40 @@ def add_users_and_assignments(self, df, response_type, session, rt, options):
281291
own_council_list = list(own_council.values_list("id", flat=True))
282292
assigned_councils = assigned_councils + own_council_list
283293

294+
included_types = []
295+
included_countries = []
296+
if options["council_restrictions"]:
297+
restrictions = row["council_restrictions"].split(",")
298+
299+
for r in restrictions:
300+
if r.lower() in [
301+
"scotland",
302+
"england",
303+
"wales",
304+
"northern ireland",
305+
]:
306+
included_countries.append(r.lower())
307+
elif r == "London":
308+
included_types.append("LBO")
309+
284310
councils_to_assign = PublicAuthority.objects.filter(
285311
marking_session=session
286312
).exclude(
287313
Q(id__in=assigned_councils) | Q(type="COMB") | Q(do_not_mark=True)
288-
)[
289-
:num_councils
290-
]
314+
)
315+
316+
if len(included_types) > 0 and len(included_countries) > 0:
317+
councils_to_assign = councils_to_assign.filter(
318+
Q(country__in=included_countries) | Q(type__in=included_types)
319+
)
320+
elif len(included_types) > 0:
321+
councils_to_assign = councils_to_assign.filter(type__in=included_types)
322+
elif len(included_countries) > 0:
323+
councils_to_assign = councils_to_assign.filter(
324+
country__in=included_countries
325+
)
326+
327+
councils_to_assign = councils_to_assign[:num_councils]
291328

292329
if councils_to_assign.count() == 0:
293330
self.stdout.write(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
First name,Last name,Email,Council Area,Type of Volunteering,Assigned Section,Nations/London
2+
First,Last,first_last@example.org,Aberdeenshire Council,Scorecards Volunteering,Transport,"Scotland,London"

crowdsourcer/tests/test_import_volunteers_script.py

+63
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,66 @@ def test_assignment_limits_in_file(self):
441441
).count(),
442442
3,
443443
)
444+
445+
def test_council_restrictions(self):
446+
data_file = (
447+
pathlib.Path(__file__).parent.resolve()
448+
/ "data"
449+
/ "volunteers_with_council_restriction.csv"
450+
)
451+
self.add_extra_councils()
452+
453+
self.assertEquals(Assigned.objects.count(), 0)
454+
self.call_command(
455+
"import_volunteers",
456+
session="Default",
457+
file=data_file,
458+
add_users=True,
459+
make_assignments=True,
460+
council_restrictions=True,
461+
)
462+
self.assertEquals(User.objects.count(), 1)
463+
self.assertEquals(Marker.objects.count(), 1)
464+
self.assertEquals(Assigned.objects.count(), 1)
465+
self.assertEquals(
466+
Assigned.objects.filter(
467+
marking_session__label="Default", section__title="Transport"
468+
).count(),
469+
1,
470+
)
471+
472+
a = Assigned.objects.filter(
473+
marking_session__label="Default", section__title="Transport"
474+
).first()
475+
self.assertEquals(a.authority.name, "Aberdeen City Council")
476+
477+
# need to remove them all as won't assign to people with existing assignments
478+
Assigned.objects.all().delete()
479+
480+
ob = PublicAuthority.objects.get(unique_id="E90008")
481+
ob.type = "LBO"
482+
ob.save()
483+
484+
self.call_command(
485+
"import_volunteers",
486+
session="Default",
487+
file=data_file,
488+
add_users=True,
489+
make_assignments=True,
490+
council_restrictions=True,
491+
)
492+
self.assertEquals(User.objects.count(), 1)
493+
self.assertEquals(Marker.objects.count(), 1)
494+
self.assertEquals(Assigned.objects.count(), 2)
495+
self.assertEquals(
496+
Assigned.objects.filter(
497+
marking_session__label="Default", section__title="Transport"
498+
).count(),
499+
2,
500+
)
501+
502+
a = Assigned.objects.filter(
503+
marking_session__label="Default", section__title="Transport"
504+
)
505+
self.assertEquals(a[0].authority.name, "Aberdeen City Council")
506+
self.assertEquals(a[1].authority.name, "Old Borsetshire")

0 commit comments

Comments
 (0)