Bail on incorrect headers
This commit is contained in:
parent
37a698c77b
commit
aa9bdacb18
3 changed files with 67 additions and 55 deletions
|
@ -37,11 +37,13 @@ class Command(BaseCommand):
|
|||
|
||||
# Report successes, failures and summaries
|
||||
print()
|
||||
if results.get("upserts"):
|
||||
for upsert_msg in results.get("upserts"):
|
||||
print(upsert_msg)
|
||||
|
||||
# Stored errors has the form:
|
||||
# self.errors = [{3: ["Incorrect foo", "Non-existent bar"]}, {7: [...]}]
|
||||
if results.get("errors"):
|
||||
for error_dict in results.get("errors"):
|
||||
for k, error_list in error_dict.items():
|
||||
print(f"\nSkipped CSV row {k}:")
|
||||
|
@ -49,5 +51,6 @@ class Command(BaseCommand):
|
|||
print(f"- {msg}")
|
||||
|
||||
print()
|
||||
if results.get("summaries"):
|
||||
for summary_msg in results.get("summaries"):
|
||||
print(summary_msg)
|
||||
|
|
|
@ -17,9 +17,11 @@ class CSVImporter:
|
|||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.error_msgs = []
|
||||
self.upsert_msgs = []
|
||||
self.summary_msgs = []
|
||||
self.results = {
|
||||
"errors": [],
|
||||
"upserts": [],
|
||||
"summaries": [],
|
||||
}
|
||||
self.line_count = 0
|
||||
self.upsert_count = 0
|
||||
|
||||
|
@ -39,6 +41,13 @@ class CSVImporter:
|
|||
# fileobj comes from browser upload (in-memory)
|
||||
csv_reader = csv.DictReader(codecs.iterdecode(fileobj, "utf-8"))
|
||||
|
||||
# DI check: Do we have expected header row?
|
||||
header = csv_reader.fieldnames
|
||||
expected = ['Title', 'Group', 'Task List', 'Created Date', 'Due Date', 'Completed', 'Created By', 'Assigned To', 'Note', 'Priority']
|
||||
if not header == expected:
|
||||
self.results.get('summaries').append(f"Inbound data does not have expected columns.\nShould be: {expected}")
|
||||
return self.results
|
||||
|
||||
for row in csv_reader:
|
||||
self.line_count += 1
|
||||
|
||||
|
@ -64,18 +73,13 @@ class CSVImporter:
|
|||
f'Upserted task {obj.id}: "{obj.title}"'
|
||||
f' in list "{obj.task_list}" (group "{obj.task_list.group}")'
|
||||
)
|
||||
self.upsert_msgs.append(msg)
|
||||
self.results.get("upserts").append(msg)
|
||||
|
||||
self.summary_msgs.append(f"\nProcessed {self.line_count} CSV rows")
|
||||
self.summary_msgs.append(f"Upserted {self.upsert_count} rows")
|
||||
self.summary_msgs.append(f"Skipped {self.line_count - self.upsert_count} rows")
|
||||
self.results.get('summaries').append(f"\nProcessed {self.line_count} CSV rows")
|
||||
self.results.get('summaries').append(f"Upserted {self.upsert_count} rows")
|
||||
self.results.get('summaries').append(f"Skipped {self.line_count - self.upsert_count} rows")
|
||||
|
||||
_res = {
|
||||
"errors": self.error_msgs,
|
||||
"upserts": self.upsert_msgs,
|
||||
"summaries": self.summary_msgs,
|
||||
}
|
||||
return _res
|
||||
return self.results
|
||||
|
||||
def validate_row(self, row):
|
||||
"""Perform data integrity checks and set default values. Returns a valid object for insertion, or False.
|
||||
|
@ -174,7 +178,7 @@ class CSVImporter:
|
|||
|
||||
# #######################
|
||||
if row_errors:
|
||||
self.error_msgs.append({self.line_count: row_errors})
|
||||
self.results.get("errors").append({self.line_count: row_errors})
|
||||
return False
|
||||
|
||||
# No errors:
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
</div>
|
||||
<div class="card-body">
|
||||
|
||||
{% if results.summaries %}
|
||||
<p>
|
||||
<b>Summary:</b>
|
||||
</p>
|
||||
|
@ -29,7 +30,9 @@
|
|||
<li>{{ line }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
{% if results.upserts %}
|
||||
<p>
|
||||
<b>Upserts (tasks created or updated):</b>
|
||||
</p>
|
||||
|
@ -38,7 +41,9 @@
|
|||
<li>{{ line }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
{% if results.errors %}
|
||||
<p>
|
||||
<b>Errors (tasks NOT created or updated):</b>
|
||||
</p>
|
||||
|
@ -52,9 +57,9 @@
|
|||
{% endfor %}
|
||||
</ul>
|
||||
{% endfor %}
|
||||
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
|
||||
</div>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue