-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HR data ingest #593
base: main
Are you sure you want to change the base?
HR data ingest #593
Conversation
payroll/services/ingest.py
Outdated
def hr_row_to_employee(hr_row: HrRow) -> Employee: | ||
employee = Employee() | ||
employee_fields = vars(employee) | ||
matching_fields = [field for field in hr_row._fields if field in employee_fields] | ||
for field in matching_fields: | ||
setattr(employee, field, getattr(hr_row, field)) | ||
return employee |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vars
is the wrong approach as it returns all the underlying class attributes and we wouldn't want a field to match on accident.
in django you can get the fields of a model through the Model._meta.get_fields()
API
https://docs.djangoproject.com/en/5.1/ref/models/meta/#retrieving-all-field-instances-of-a-model
however, in this case I think we should avoid iteration for the following reasons:
- binds the
HrRow
field names closely to theEmployee
fieldnames (this isn't that bad) - no opportunity to validate/parse/convert the values
- all fields will be attempted where an explicit list of which to update is safer if the csv data/model changes
i think the better approach would be to build up the Employee
model in a procedural way and validate data as we go along
emp = Employee()
# no need to worry about the field names not matching
emp.cost_centre_id = row.cost_centre_code
# now we can validate the data
emp.fte = float(row.fte)
...
Great work on this! Let's chat about my comments offline :) |
sam has made a mess of the code, sorry! haha
No description provided.