Skip to content

Commit

Permalink
Task: CVP Column Update #405 (#408)
Browse files Browse the repository at this point in the history
* Changed portfolio column to Use Case and Class, updated validate_field_values function to check for multiple values in a string

* Making delimiter for splitting values in a function a kwarg argument
  • Loading branch information
JulianForeman authored Nov 12, 2024
1 parent 21814e5 commit 3e68241
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
15 changes: 9 additions & 6 deletions django/api/constants/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ class CVPDataColumns(Enum):
VEHICLE_CATEGORY = "Vehicle Category"
DRIVE_TYPE = "Drive Type"
VEHICLE_TYPE = "Vehicle Type"
PORTFOLIO = "Portfolio"
ROAD_CLASS = "Class"
USE_CASE = "Use Case"
MAKE_AND_MODEL = "Vehicle Make and Model"
ECONOMIC_REGION = "Economic Region"
START_DATE = "Start Date"
Expand Down Expand Up @@ -483,7 +484,8 @@ class CVPDataColumnMapping(Enum):
vehicle_category = "Vehicle Category"
drive_type = "Drive Type"
vehicle_type = "Vehicle Type"
portfolio = "Portfolio"
road_class = "Class"
use_case = "Use Case"
make_and_model = "Vehicle Make and Model"
economic_region = "Economic Region"
start_date = "Start Date"
Expand Down Expand Up @@ -713,7 +715,8 @@ class CVPDataColumnMapping(Enum):
"vehicle_category": str,
"drive_type": str,
"vehicle_type": str,
"portfolio": str,
"road_class": str,
"use_case": str,
"make_and_model": str,
"economic_region": str,
"start_date": datetime.date,
Expand Down Expand Up @@ -761,7 +764,7 @@ class CVPDataColumnMapping(Enum):
"sheet_name": "ARC Data",
"preparation_functions": [prepare_arc_project_tracking],
"validation_functions": [
{'function': validate_field_values, "columns": [], "kwargs": {"indices_offset":2, "fields_and_values": ARC_VALID_FIELD_VALUES}},
{'function': validate_field_values, "columns": [], "kwargs": {"indices_offset":2, "fields_and_values": ARC_VALID_FIELD_VALUES, "delimiter": ","}},
{"function": region_checker, "columns": ['Economic Region'], "kwargs": {"indices_offset":2}},
]
},
Expand Down Expand Up @@ -825,7 +828,7 @@ class CVPDataColumnMapping(Enum):
{"function": typo_checker, "columns": ["Applicant Name"], "kwargs": {"cutoff": 0.8, "indices_offset": 2}},
{"function": location_checker, "columns": ["City"], "kwargs": {"columns_to_features_map": {"City": LOCALITY_FEATURES_MAP}, "indices_offset":2}},
{"function": email_validator, "columns": ["Email"], "kwargs": {"indices_offset":2, "get_resolver": get_google_resolver}},
{"function": validate_field_values, "columns": [], "kwargs": {"indices_offset":2, "fields_and_values": GER_VALID_FIELD_VALUES}},
{"function": validate_field_values, "columns": [], "kwargs": {"indices_offset":2, "fields_and_values": GER_VALID_FIELD_VALUES, "delimiter": ","}},
]
},
"CVP Data": {
Expand All @@ -834,7 +837,7 @@ class CVPDataColumnMapping(Enum):
"column_mapping": CVPDataColumnMapping,
"sheet_name": "Data",
"preparation_functions": [prepare_cvp_data],
"validation_functions": [{"function": validate_field_values, "columns": [], "kwargs": {"indices_offset":2, "fields_and_values": CVP_DATA_VALID_FIELD_VALUES}},]
"validation_functions": [{"function": validate_field_values, "columns": [], "kwargs": {"indices_offset":2, "fields_and_values": CVP_DATA_VALID_FIELD_VALUES, "delimiter": ","}},]
},

}
6 changes: 3 additions & 3 deletions django/api/constants/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@
'Status': ['Approved', 'Completed', 'Terminated', 'Not Approved', 'Application Withdrawn'],
'Vehicles Deployed': ['Yes', 'No'],
'Vehicle Category': ['On-Road', 'Off-Road', 'Marine', 'Rail', 'Aviation'],
'Portfolio': [
'Road - 8', 'Rail', 'Marine', 'Road - 8C', 'School Bus',
'Loader', 'Road - 6', 'Fork Lift', 'Recreation'
'Class': [
'Road - 3', 'Road - 4', 'Road - 5', 'Road - 6', 'Road - 7',
'Road - 8', 'Road - 8C'
],
'Economic Region': [
'Nechako', 'Northeast', 'North Coast', 'Cariboo', 'Vancouver Island/Coast',
Expand Down
27 changes: 27 additions & 0 deletions django/api/migrations/0039_auto_20241031_2123.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.25 on 2024-10-31 21:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0038_addregions'),
]

operations = [
migrations.RemoveField(
model_name='cvpdata',
name='portfolio',
),
migrations.AddField(
model_name='cvpdata',
name='road_class',
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name='cvpdata',
name='use_case',
field=models.CharField(blank=True, max_length=100, null=True),
),
]
8 changes: 6 additions & 2 deletions django/api/models/cvp_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ class CVPData(Auditable):
blank=False, null=False, max_length=100
)

portfolio = models.CharField(
blank=False, null=False, max_length=100
road_class = models.CharField(
blank=True, null=True, max_length=100
)

use_case = models.CharField(
blank=True, null=True, max_length=100
)

make_and_model = models.CharField(
Expand Down
17 changes: 11 additions & 6 deletions django/api/services/spreadsheet_uploader_prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,22 +353,27 @@ def email_validator(df, *columns, **kwargs):
}
return result



def validate_field_values(df, *columns, **kwargs):
allowed_values = kwargs.get("fields_and_values")
invalid_values = []

result = {}
delimiter = kwargs.get("delimiter")
for column in df.columns:
if column in allowed_values:
indices = []
series = df[column]
for index, value in series.items():
if str(value).upper() not in (item.upper() for item in allowed_values[column]) and value != '' and value is not None and not pd.isna(value):
indices.append(index + kwargs.get("indices_offset", 0))
if str(value) not in invalid_values:
invalid_values.append(str(value))
if delimiter is not None:
items = [item.strip() for item in value.split(delimiter)]

for item in items:
if str(item).upper() not in (valid.upper() for valid in allowed_values[column]) and item != '' and item is not None and not pd.isna(item):
if index + kwargs.get("indices_offset", 0) not in indices:
indices.append(index + kwargs.get("indices_offset", 0))
if str(item) not in invalid_values:
invalid_values.append(str(item))

if indices:
result[column] = {
', '.join(invalid_values) + " - is not in the list of expected values": {
Expand Down

0 comments on commit 3e68241

Please sign in to comment.