Skip to content

Commit

Permalink
handle sheets having GSS code directly in them
Browse files Browse the repository at this point in the history
No need for a council lookup so do not populate it and get the GSS code
directly from the data sheet
  • Loading branch information
struan committed Dec 11, 2024
1 parent 31200b1 commit 5e93754
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions crowdsourcer/management/commands/import_national_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,16 @@ def clear_existing_answers(self, q, details):
Response.objects.filter(question=q, response_type=self.rt).delete()

def popuplate_council_lookup(self):
df = self.get_df("council names", {})
lookup = {}
for _, row in df.iterrows():
lookup[row["local-authority-code"]] = row["gss-code"]
try:
df = self.get_df("council names", {})
for _, row in df.iterrows():
lookup[row["local-authority-code"]] = row["gss-code"]
except ValueError:
self.print_info(
f"{YELLOW}No council names tab found so not populating council lookup{NOBOLD}",
1,
)

self.council_lookup = lookup

Expand Down Expand Up @@ -187,29 +193,35 @@ def import_answers(self, user, rt, df, q, details):
if row[skip_check["col"]] == skip_check["val"]:
continue

council_col = details.get("gss_col", details.get("council_col", ""))
gss_col = details.get("gss_col", "Local Authority Code")
if row.get(gss_col):
gss = row[gss_col]
code = gss
else:
council_col = details.get("gss_col", details.get("council_col", ""))

code = row.get(
"local authority code",
row.get(
"local authority council code",
code = row.get(
"local authority code",
row.get(
"Local authority council code",
row.get("local-authority-code", ""),
"local authority council code",
row.get(
"Local authority council code",
row.get("local-authority-code", ""),
),
),
),
)
)

if (
(code == "" or pd.isna(code))
and row.get("manually added local-authority-code", None) is not None
) and not pd.isna(row["manually added local-authority-code"]):
code = row["manually added local-authority-code"]
if (
(code == "" or pd.isna(code))
and row.get("manually added local-authority-code", None)
is not None
) and not pd.isna(row["manually added local-authority-code"]):
code = row["manually added local-authority-code"]

gss = self.council_lookup.get(
code,
None,
)
gss = self.council_lookup.get(
code,
None,
)

if gss is None:
value = row[council_col]
Expand Down

0 comments on commit 5e93754

Please sign in to comment.