Skip to content

Commit

Permalink
Pull data source names from integration config settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbernstein committed Mar 21, 2024
1 parent 5bd1cf1 commit 54dbf06
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 77 deletions.
37 changes: 26 additions & 11 deletions core/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from enum import Enum
from typing import TextIO

from sqlalchemy import and_, exists, or_, select, text, tuple_
from sqlalchemy import String, and_, exists, or_, select, text, tuple_
from sqlalchemy.orm import Query, Session, defer
from sqlalchemy.orm.attributes import flag_modified
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
Expand All @@ -34,6 +34,7 @@
Edition,
Identifier,
IntegrationConfiguration,
IntegrationLibraryConfiguration,
Library,
LicensePool,
LicensePoolDeliveryMechanism,
Expand Down Expand Up @@ -2769,13 +2770,6 @@ def suppress_work(self, library: Library, identifier: Identifier) -> None:
class GenerateInventoryReports(Script):
"""Generate inventory reports from queued report tasks"""

DATA_SOURCES = [
"Palace Marketplace",
"BiblioBoard",
"Palace Bookshelf",
"Unlimited Listens",
]

@classmethod
def arg_parser(cls, _db: Session | None) -> argparse.ArgumentParser: # type: ignore[override]
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -2811,7 +2805,28 @@ def process_task(self, task: DeferredTask):
date_str = current_time.strftime("%Y-%m-%d_%H:%M:%s")
attachments = {}

for data_source_name in self.DATA_SOURCES:
integrations = (
self._db.query(IntegrationConfiguration)
.join(IntegrationLibraryConfiguration)
.filter(IntegrationLibraryConfiguration.library_id == data.library_id)
.filter(
IntegrationConfiguration.settings_dict[
"include_in_inventory_report"
].astext.cast(String)
== "yes"
)
.filter(
IntegrationConfiguration.settings_dict["data_source"].astext.cast(
String
)
!= None
)
.all()
)

data_source_names = [i.settings_dict["data_source"] for i in integrations]

for data_source_name in data_source_names:
formatted_ds_name = data_source_name.lower().replace(" ", "_")
file_name = f"palace-inventory-report-{formatted_ds_name}-{date_str}"
# generate csv file
Expand Down Expand Up @@ -2849,7 +2864,7 @@ def generate_report(self, data_source_name: str, library_id: int) -> str:
writer = csv.writer(temp, delimiter=",")
rows = self._db.execute(
text(self.inventory_report_query()),
{"data_source_name": data_source_name, "library_id": library_id},
{"library_id": library_id, "data_source_name": data_source_name},
)
writer.writerow(rows.keys())
writer.writerows(rows)
Expand Down Expand Up @@ -2922,7 +2937,7 @@ def inventory_report_query(self) -> str:
ic.id = il.parent_id and
ic.id = collection_sharing.parent_id and
il.library_id = lib.id and
d.name = :data_source_name and
d.name = :data_source_name and
lib.id = :library_id
order by title, author
"""
Expand Down
123 changes: 57 additions & 66 deletions tests/core/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2584,6 +2584,11 @@ def test_do_run(self, db: DatabaseTransactionFixture):
collection = db.collection(
name="BiblioBoard Test collection", data_source_name="BiblioBoard"
)

# flag the collection as one that should be included in inventory report
collection.integration_configuration.settings_dict[
"include_in_inventory_report"
] = "yes"
collection.libraries = [library]
ds = collection.data_source
assert ds
Expand Down Expand Up @@ -2639,73 +2644,59 @@ def test_do_run(self, db: DatabaseTransactionFixture):
assert kwargs["receivers"] == [email] # type:ignore[unreachable]
assert kwargs["subject"].__contains__("Inventory Report")
attachments: dict = kwargs["attachments"]
csv_titles_strings = [
"biblioboard",
"palace_marketplace",
"unlimited_listens",
"palace_bookshelf",
]

def at_least_one_contains_the_other(list1: list[str], list2: list[str]) -> bool:
for s1 in list1:
for s2 in list2:
if s1.__contains__(s2):
return True
return False

assert at_least_one_contains_the_other(attachments.keys(), csv_titles_strings)

for key in attachments.keys():
value = attachments[key]
assert len(value) > 0
if str(key).__contains__("biblioboard"):
csv_file = StringIO(value)
reader = csv.reader(csv_file, delimiter=",")
first_row = None
row_count = 0

for row in reader:
row_count += 1
if not first_row:
first_row = row
row_headers = [
"title",
"author",
"identifier",
"language",
"publisher",
"format",
"collection_name",
"license_duration_days",
"license_expiration_date",
"initial_loan_count",
"consumed_loans",
"remaining_loans",
"allowed_concurrent_users",
"library_active_hold_count",
"library_active_loan_count",
"shared_active_hold_count",
"shared_active_loan_count",
]
for h in row_headers:
assert h in row
continue

assert row[first_row.index("title")] == title
assert row[first_row.index("author")] == author
assert row[first_row.index("shared_active_hold_count")] == "-1"
assert row[first_row.index("shared_active_loan_count")] == "-1"
assert row[first_row.index("initial_loan_count")] == str(
checkouts_available
)
assert row[first_row.index("consumed_loans")] == str(
checkouts_available - checkouts_left
)
assert row[first_row.index("allowed_concurrent_users")] == str(
terms_concurrency
)

assert row_count == 2
assert len(attachments) == 1
key = [*attachments.keys()][0]
assert "biblioboard" in key
value = attachments[key]
assert len(value) > 0
csv_file = StringIO(value)
reader = csv.reader(csv_file, delimiter=",")
first_row = None
row_count = 0

for row in reader:
row_count += 1
if not first_row:
first_row = row
row_headers = [
"title",
"author",
"identifier",
"language",
"publisher",
"format",
"collection_name",
"license_duration_days",
"license_expiration_date",
"initial_loan_count",
"consumed_loans",
"remaining_loans",
"allowed_concurrent_users",
"library_active_hold_count",
"library_active_loan_count",
"shared_active_hold_count",
"shared_active_loan_count",
]
for h in row_headers:
assert h in row
continue

assert row[first_row.index("title")] == title
assert row[first_row.index("author")] == author
assert row[first_row.index("shared_active_hold_count")] == "-1"
assert row[first_row.index("shared_active_loan_count")] == "-1"
assert row[first_row.index("initial_loan_count")] == str(
checkouts_available
)
assert row[first_row.index("consumed_loans")] == str(
checkouts_available - checkouts_left
)
assert row[first_row.index("allowed_concurrent_users")] == str(
terms_concurrency
)

assert row_count == 2


class TestDeleteOldDeferredTasks:
Expand Down

0 comments on commit 54dbf06

Please sign in to comment.