Skip to content
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

CSV file import #6561

Merged
merged 11 commits into from
Jul 28, 2021
Merged

CSV file import #6561

merged 11 commits into from
Jul 28, 2021

Conversation

abigley
Copy link
Contributor

@abigley abigley commented Jun 7, 2021

Fixes: #6560

An example / first draft of a solution for #6560 .

In the current implementation, if users try to use both the CSVFileField and the CSVDataField, the CSVDataField will be ignored and only the objects in the file will be imported. It should be unusual that both fields are populated, but let me know if you prefer to handle that case differently, such as by raising a validation error. The csv file is checked for errors (incorrect formatting, objects already exist, invalid columns) before importing.

This solution involves some duplication between CSVFileField and CSVDataField. If you prefer, I could remove most of the duplication by refactoring them to both inherit from a single class - say, CSVParsingField.

@abigley abigley changed the title Csv feature CSV file import Jun 7, 2021
Copy link
Member

@jeremystretch jeremystretch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great start, thank you for your contribution. Just needs a bit of cleaning up before we can merge it.

netbox/netbox/views/generic.py Outdated Show resolved Hide resolved

if form.is_valid():
logger.debug("Form validation was successful")

try:
# Iterate through CSV data and bind each row to a new model form instance.
with transaction.atomic():
headers, records = form.cleaned_data['csv']
if request.FILES:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a validation method to the form to check for both csv and upload_csv set, and raise a ValidationError if so as a sanity check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a method for this within the _import_form definition

netbox/utilities/forms/fields.py Show resolved Hide resolved
@abigley abigley marked this pull request as draft June 10, 2021 18:14
@abigley abigley marked this pull request as ready for review June 14, 2021 19:27
Copy link

@jlost jlost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@jlost
Copy link

jlost commented Jul 26, 2021

@jeremystretch This would be useful to me, and it looks like all requested changes have been addressed. Could you give this another peek when you have a chance?

@jeremystretch
Copy link
Member

Might need a bit of cleanup (e.g. stripping whitespace from uploaded files) but I can take care of that. Thanks!

@jeremystretch jeremystretch merged commit ea0de4b into netbox-community:develop Jul 28, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CSV file import
3 participants