Add attribute sort helper function

This commit is contained in:
JonnyWong16 2020-10-08 19:53:57 -07:00
parent 881f37f731
commit 3be9c84f2b
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
2 changed files with 9 additions and 2 deletions

View file

@ -1643,8 +1643,7 @@ class Export(object):
if self.file_format == 'csv':
csv_data = helpers.flatten_dict(result)
csv_headers = [field.split('.') for field in set().union(*csv_data)]
csv_headers = ['.'.join(field) for field in sorted(csv_headers, key=lambda s: (len(s), s))]
csv_headers = sorted(set().union(*csv_data), key=helpers.sort_attrs)
with open(filepath, 'w', encoding='utf-8', newline='') as outfile:
writer = csv.DictWriter(outfile, csv_headers)
writer.writeheader()

View file

@ -1236,6 +1236,14 @@ def bool_true(value, return_none=False):
return False
def sort_attrs(attr):
if isinstance(attr, (list, tuple)):
a = attr[0].split('.')
else:
a = attr.split('.')
return len(a), a
def get_attrs_to_dict(obj, attrs):
d = {}