From bd05b787bd7daede9810d0c769d94f4a41a70af4 Mon Sep 17 00:00:00 2001 From: Theodor Tolstoy Date: Mon, 2 May 2022 11:12:50 +0200 Subject: [PATCH] fix utc corrections. closes #223 --- .../migration_tasks/loans_migrator.py | 15 +++-------- .../transaction_migration/legacy_loan.py | 27 ++++++++++++------- tests/test_legacy_loan.py | 25 ++++++++++++++--- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/folio_migration_tools/migration_tasks/loans_migrator.py b/src/folio_migration_tools/migration_tasks/loans_migrator.py index e4f8ad10..0ef56fbb 100644 --- a/src/folio_migration_tools/migration_tasks/loans_migrator.py +++ b/src/folio_migration_tools/migration_tasks/loans_migrator.py @@ -13,6 +13,9 @@ import requests from dateutil import parser as du_parser +from folio_uuid.folio_namespaces import FOLIONamespaces +from pydantic import BaseModel + from folio_migration_tools.circulation_helper import CirculationHelper from folio_migration_tools.custom_dict import InsensitiveDictReader from folio_migration_tools.helper import Helper @@ -25,8 +28,6 @@ from folio_migration_tools.transaction_migration.transaction_result import ( TransactionResult, ) -from folio_uuid.folio_namespaces import FOLIONamespaces -from pydantic import BaseModel class LoansMigrator(MigrationTaskBase): @@ -588,16 +589,6 @@ def change_due_date(self, folio_loan, legacy_loan): logging.info(exception) return False, None, None - def make_loan_utc(self, legacy_loan: LegacyLoan): - if self.task_configuration.utc_difference != 0: - legacy_loan.due_date = legacy_loan.due_date + timedelta( - hours=self.task_configuration.utc_difference - ) - legacy_loan.out_date = legacy_loan.out_date + timedelta( - hours=self.task_configuration.utc_difference - ) - self.migration_report.add_general_statistics("Adjusted out and due dates to UTC") - def timings(t0, t0func, num_objects): avg = num_objects / (time.time() - t0) diff --git a/src/folio_migration_tools/transaction_migration/legacy_loan.py b/src/folio_migration_tools/transaction_migration/legacy_loan.py index 7d3c318c..ed45d067 100644 --- a/src/folio_migration_tools/transaction_migration/legacy_loan.py +++ b/src/folio_migration_tools/transaction_migration/legacy_loan.py @@ -49,19 +49,24 @@ def __init__(self, legacy_loan_dict, utc_difference=0, row=0): self.patron_barcode: str = legacy_loan_dict["patron_barcode"].strip() self.due_date: datetime = temp_date_due self.out_date: datetime = temp_date_out + self.make_utc() + self.correct_for_1_day_loans() + self.renewal_count = int(legacy_loan_dict["renewal_count"]) + self.next_item_status = legacy_loan_dict.get("next_item_status", "").strip() + if self.next_item_status not in legal_statuses: + self.errors.append(("Not an allowed status", self.next_item_status)) + + def correct_for_1_day_loans(self): try: - self.make_loan_utc() if self.due_date <= self.out_date: if self.due_date.hour == 0: self.due_date = self.due_date.replace(hour=23, minute=59) if self.out_date.hour == 0: self.out_date = self.out_date.replace(hour=0, minute=1) + if self.due_date <= self.out_date: + raise ValueError("Due date is before out date") except Exception: self.errors.append(("Time alignment issues", "both dates")) - self.renewal_count = int(legacy_loan_dict["renewal_count"]) - self.next_item_status = legacy_loan_dict.get("next_item_status", "").strip() - if self.next_item_status not in legal_statuses: - self.errors.append(("Not an allowed status", self.next_item_status)) def to_dict(self): return { @@ -73,7 +78,11 @@ def to_dict(self): "next_item_status": self.next_item_status, } - def make_loan_utc(self): - if self.utc_difference != 0: - self.due_date = self.due_date + timedelta(hours=self.utc_difference) - self.out_date = self.out_date + timedelta(hours=self.utc_difference) + def make_utc(self): + try: + hours_to_add = -1 * self.utc_difference + if self.utc_difference != 0: + self.due_date = self.due_date + timedelta(hours=hours_to_add) + self.out_date = self.out_date + timedelta(hours=hours_to_add) + except Exception: + self.errors.append(("UTC correction issues", "both dates")) diff --git a/tests/test_legacy_loan.py b/tests/test_legacy_loan.py index e8736782..eecc2493 100644 --- a/tests/test_legacy_loan.py +++ b/tests/test_legacy_loan.py @@ -22,16 +22,16 @@ def test_init_tz(): loan_dict = { "item_barcode": "the barcode with trailing space ", "patron_barcode": " the barcode with leading space", - "due_date": "20220113 22:00", - "out_date": "20220113 20:00", + "due_date": "20220113 16:00", + "out_date": "20220113 14:00", "renewal_count": "1", "next_item_status": "Checked out", } legacy_loan = LegacyLoan(loan_dict, -6) assert legacy_loan.patron_barcode == "the barcode with leading space" assert legacy_loan.item_barcode == "the barcode with trailing space" - assert legacy_loan.due_date.isoformat() == "2022-01-13T16:00:00" - assert legacy_loan.out_date.isoformat() == "2022-01-13T14:00:00" + assert legacy_loan.due_date.isoformat() == "2022-01-13T22:00:00" + assert legacy_loan.out_date.isoformat() == "2022-01-13T20:00:00" assert legacy_loan.renewal_count > 0 @@ -48,3 +48,20 @@ def test_init_tz_2(): assert legacy_loan.due_date.isoformat() == "2019-02-22T23:59:00" assert legacy_loan.out_date.isoformat() == "2019-02-22T10:53:00" assert legacy_loan.renewal_count > 0 + + +def test_init_tz_3(): + loan_dict = { + "item_barcode": "the barcode with trailing space ", + "patron_barcode": " the barcode with leading space", + "due_date": "20220113 16:00", + "out_date": "20220113 14:00", + "renewal_count": "1", + "next_item_status": "Checked out", + } + legacy_loan = LegacyLoan(loan_dict, 6) + assert legacy_loan.patron_barcode == "the barcode with leading space" + assert legacy_loan.item_barcode == "the barcode with trailing space" + assert legacy_loan.due_date.isoformat() == "2022-01-13T10:00:00" + assert legacy_loan.out_date.isoformat() == "2022-01-13T08:00:00" + assert legacy_loan.renewal_count > 0