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

Add suport for XLSX imports #898

Merged
merged 1 commit into from Dec 2, 2016
Merged

Add suport for XLSX imports #898

merged 1 commit into from Dec 2, 2016

Conversation

bjohare
Copy link
Contributor

@bjohare bjohare commented Nov 4, 2016

Proposed changes in this pull request

Fix #832 Data Import: Add support for XLSX imports

  • Add XLSX imports
  • Add import of Locations or Parties separately
  • Add selection of location_type field in import
  • Add selection of party_type field in import
  • Add suport for 1-M relations in import

When should this PR be merged

Can be merged immediately.

Risks

Low risk

Follow up actions

Adds dependency on pandas 0.19.0, re-provisioning required


class Importer():

def __init__(self, project):
def __init__(self, project=None, delimiter=None, quotechar=None):
Copy link
Member

Choose a reason for hiding this comment

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

You could write this method as:

def __init__(self, project=None, delimiter=',' quotechar='"'):
    self.project = project
    self._schema_attrs = {}
    self.delimiter = delimiter
    self.quotechar = quotechar

Makes it a bit easier to read.

Also is project actually optional here? If not we should not define it as an optional keyword argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

row, content_types, attributes,
attr_map, tenure_type
)
except Exception as e:
Copy link
Member

Choose a reason for hiding this comment

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

What exceptions do you expect here. We should only catch those instead of Exception, which covers all exceptions that could possibly happen. That means if something is unexpectedly going wrong in this bloc, we will have a very hard time debugging the problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored to catch ValidationErrors only.

# handle select_multiple fields
if (attribute.attr_type.name == 'select_multiple'):
val = [v.strip() for v in val.split(',')]
content_types[content_type][
Copy link
Member

Choose a reason for hiding this comment

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

This fits into the line's character limit. Maybe we should make this one line for better readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

content_types[content_type][
'attributes'][
attribute.name] = val
party = Party.objects.create(
Copy link
Member

Choose a reason for hiding this comment

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

The next few commands statements all fit into one line. Having these in one line will make it easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

'tenure_type.label']
final.drop(drop_cols, inplace=True, axis=1)
return final.to_csv(index=False, index_label=False)
except Exception:
Copy link
Member

Choose a reason for hiding this comment

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

Again, we should catch generic exceptions, only those that we actually expect in the block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored to catch KeyError's only here.

url = ''
while not url:
temp_url = upload_to + '/' + random_id() + ext
try:
Copy link
Member

Choose a reason for hiding this comment

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

Why don't you use if not Resource.objects.filter(file__contains=temp_url).exists() here? Saves you two lines and is easier to read...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@bjohare bjohare force-pushed the enhancement/#832 branch 3 times, most recently from 2e1e61f to e2c61f4 Compare November 30, 2016 11:10
@bjohare bjohare force-pushed the enhancement/#832 branch 2 times, most recently from 2f7d4e0 to 6c012c7 Compare December 2, 2016 11:55
* Add XLSX imports
* Add import of Locations or Parties separately
* Add selection of location_type field in import
* Add selection of party_type field in import
* Add suport for 1-M relations in import
@oliverroick oliverroick merged commit be55f0e into master Dec 2, 2016
@oliverroick oliverroick deleted the enhancement/#832 branch December 2, 2016 17:31
@oliverroick oliverroick removed this from Ready in Reviewing PRs Mar 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants