Skip to content

Commit c044715

Browse files
committed
enable reading section counts from the import data
1 parent 10fdff4 commit c044715

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

crowdsourcer/management/commands/import_volunteers.py

+43-23
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@
2323
GREEN = "\033[32m"
2424
NOBOLD = "\033[0m"
2525

26+
COLUMN_NAMES = (
27+
"first_name",
28+
"last_name",
29+
"email",
30+
"council_area",
31+
"type_of_volunteering",
32+
"assigned_section",
33+
)
34+
35+
USECOLS = (
36+
"First name",
37+
"Last name",
38+
"Email",
39+
"Council Area",
40+
"Type of Volunteering",
41+
"Assigned Section",
42+
)
43+
2644

2745
class Command(BaseCommand):
2846
help = "import volunteers"
@@ -45,24 +63,6 @@ class Command(BaseCommand):
4563
"local_climate_policy_programme": 15,
4664
}
4765

48-
column_names = [
49-
"first_name",
50-
"last_name",
51-
"email",
52-
"council_area",
53-
"type_of_volunteering",
54-
"assigned_section",
55-
]
56-
57-
usecols = [
58-
"First name",
59-
"Last name",
60-
"Email",
61-
"Council Area",
62-
"Type of Volunteering",
63-
"Assigned Section",
64-
]
65-
6666
def add_arguments(self, parser):
6767
parser.add_argument(
6868
"-q", "--quiet", action="store_true", help="Silence progress bars."
@@ -106,6 +106,12 @@ def add_arguments(self, parser):
106106
help="Marking session to use assignements with",
107107
)
108108

109+
parser.add_argument(
110+
"--section_count_in_data",
111+
action="store_true",
112+
help="use the section count in the csv and not the map",
113+
)
114+
109115
parser.add_argument(
110116
"--add_users", action="store_true", help="add users to database"
111117
)
@@ -120,6 +126,17 @@ def add_arguments(self, parser):
120126
help="run everything and then undo changes. Helpful to check if assigment weighting is good",
121127
)
122128

129+
def init(self, file, options):
130+
self.column_names = list(COLUMN_NAMES)
131+
self.usecols = list(USECOLS)
132+
133+
if file is None:
134+
file = self.volunteer_file
135+
136+
if options["section_count_in_data"]:
137+
self.column_names.append("section_count")
138+
self.usecols.append("How many sections?")
139+
123140
def get_df(self, filename):
124141
df = pd.read_csv(
125142
filename,
@@ -150,7 +167,10 @@ def set_authority_map(self, authority_map_file):
150167
for _, row in cols.iterrows():
151168
self.authority_map[row.bad_name] = row.good_name
152169

153-
def get_assignment_count(self, user_type):
170+
def get_assignment_count(self, user_type, row, section_count_in_data):
171+
if section_count_in_data:
172+
return row["section_count"]
173+
154174
user_type = user_type.lower().replace(" ", "_")
155175

156176
num_councils = self.num_council_map.get(user_type)
@@ -171,7 +191,9 @@ def add_users_and_assignments(self, df, response_type, session, rt, options):
171191
self.stdout.write(f"{YELLOW}No user type for {row['email']}{NOBOLD}")
172192
continue
173193

174-
num_councils = self.get_assignment_count(user_type)
194+
num_councils = self.get_assignment_count(
195+
user_type, row, options["section_count_in_data"]
196+
)
175197
if num_councils is None:
176198
self.stdout.write(
177199
f"{YELLOW}Don't know how many councils to assign for {row['email']}{NOBOLD}"
@@ -296,9 +318,7 @@ def handle(
296318
*args,
297319
**options,
298320
):
299-
if file is None:
300-
file = self.volunteer_file
301-
321+
self.init(file, options)
302322
self.set_cols(col_names)
303323
self.set_assignment_map(assignment_map)
304324
self.set_authority_map(authority_map)
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,How many sections?
2+
First,Last,first_last@example.org,Aberdeenshire Council,Scorecards Volunteering,Transport,3

crowdsourcer/tests/test_import_volunteers_script.py

+27
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,30 @@ def test_council_map(self):
414414
self.assertTrue("Aberdeenshire Council" not in councils)
415415
self.assertTrue("Adur District Council" not in councils)
416416
self.assertTrue("Aberdeen City Council" in councils)
417+
418+
def test_assignment_limits_in_file(self):
419+
data_file = (
420+
pathlib.Path(__file__).parent.resolve()
421+
/ "data"
422+
/ "volunteers_with_limit.csv"
423+
)
424+
self.add_extra_councils()
425+
426+
self.assertEquals(Assigned.objects.count(), 0)
427+
self.call_command(
428+
"import_volunteers",
429+
session="Default",
430+
file=data_file,
431+
add_users=True,
432+
make_assignments=True,
433+
section_count_in_data=True,
434+
)
435+
self.assertEquals(User.objects.count(), 1)
436+
self.assertEquals(Marker.objects.count(), 1)
437+
self.assertEquals(Assigned.objects.count(), 3)
438+
self.assertEquals(
439+
Assigned.objects.filter(
440+
marking_session__label="Default", section__title="Transport"
441+
).count(),
442+
3,
443+
)

0 commit comments

Comments
 (0)