From e40c171610e1ded34c5d85b7045292f311ccb042 Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Sat, 16 Mar 2019 00:32:18 -0700 Subject: [PATCH] Split off reusable date validator into separate function --- todo/data/import_example.csv | 2 +- todo/operations/csv_importer.py | 45 ++++++++++++++++----------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/todo/data/import_example.csv b/todo/data/import_example.csv index 54821ee..1a68723 100644 --- a/todo/data/import_example.csv +++ b/todo/data/import_example.csv @@ -1,4 +1,4 @@ Title,Group,Task List,Created By,Created Date,Due Date,Completed,Assigned To,Note,Priority Make dinner,Scuba Divers,Web project,shacker,,2019-06-14,No,,Please check with mgmt first,3 Bake bread,Scuba Divers,Example List,mr_random,2012-03-14,,Yes,,, -Bring dessert,Scuba Divers,Web project,user1,2015-06-24,,,user1,Every generation throws a hero up the pop charts,77 \ No newline at end of file +Bring dessert,Scuba Divers,Web project,user1,2015-06-248,,,user1,Every generation throws a hero up the pop charts,77 \ No newline at end of file diff --git a/todo/operations/csv_importer.py b/todo/operations/csv_importer.py index b402fa9..9da4c84 100644 --- a/todo/operations/csv_importer.py +++ b/todo/operations/csv_importer.py @@ -98,7 +98,9 @@ class CSVImporter: def validate_row(self, row): """Perform data integrity checks and set default values. Returns a valid object for insertion, or False. - Errors are stored for later display.""" + Errors are stored for later display. Intentionally not broken up into separate validator functions because + there are interdpendencies, such as checking for existing `creator` in one place and then using + that creator for group membership check in others.""" row_errors = [] @@ -155,28 +157,17 @@ class CSVImporter: row_errors.append(msg) # ####################### - # Validate Due Date - dd = row.get("Due Date") - if dd: - try: - row["Due Date"] = datetime.datetime.strptime(dd, "%Y-%m-%d") - except ValueError: - msg = f"Could not convert Due Date {dd} to python date" - row_errors.append(msg) - else: - row["Created Date"] = None # Override default empty string '' value - - # ####################### - # Validate Created Date - cd = row.get("Created Date") - if cd: - try: - row["Created Date"] = datetime.datetime.strptime(cd, "%Y-%m-%d") - except ValueError: - msg = f"Could not convert Created Date {cd} to python date" - row_errors.append(msg) - else: - row["Created Date"] = None # Override default empty string '' value + # Validate Dates + datefields = ["Due Date", "Created Date"] + for datefield in datefields: + datestring = row.get(datefield) + if datestring: + valid_date = self.validate_date(datestring) + if valid_date: + row[datefield] = valid_date + else: + msg = f"Could not convert {datefield} {datestring} to valid date instance" + row_errors.append(msg) # ####################### # Group membership checks have passed @@ -195,3 +186,11 @@ class CSVImporter: # No errors: return row + + def validate_date(self, datestring): + """Inbound date string from CSV translates to a valid python date.""" + try: + date_obj = datetime.datetime.strptime(datestring, "%Y-%m-%d") + return date_obj + except ValueError: + return False