Skip to content

Commit

Permalink
Make report tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdilauro committed Sep 12, 2023
1 parent d1e303c commit d5ea5d4
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions core/jobs/playtime_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ def do_run(self):


class PlaytimeEntriesEmailReportsScript(Script):
REPORT_DATE_FORMAT = "%Y-%m-%d"

@classmethod
def arg_parser(cls):
# The default start and end dates encompass the previous three months.
default_start, default_until = previous_months(number_of_months=3)
# The default `start` and `until` dates encompass the previous three months.
# We convert them to strings here so that they are handled the same way
# as non-default dates specified as arguments.
default_start, default_until = (
date.isoformat() for date in previous_months(number_of_months=3)
)

parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
Expand All @@ -95,8 +101,7 @@ def arg_parser(cls):
def do_run(self):
"""Produce a report for the given (or default) date range."""
parsed = self.parse_command_line()
utc = pytz.utc
start, until = utc.localize(parsed.start), utc.localize(parsed.until)
start, until = pytz.utc.localize(parsed.start), pytz.utc.localize(parsed.until)

# Let the database do the math for us
result = (
Expand All @@ -120,8 +125,20 @@ def do_run(self):
)
)

formatted_start_date = start.strftime(self.REPORT_DATE_FORMAT)
formatted_until_date = until.strftime(self.REPORT_DATE_FORMAT)
report_date_label = f"{formatted_start_date} - {formatted_until_date}"
email_subject = (
f"Playtime Summaries {formatted_start_date} - {formatted_until_date}"
)
attachment_name = (
f"playtime-summary-{formatted_start_date}-{formatted_until_date}"
)

# Write to a temporary file so we don't overflow the memory
with TemporaryFile("w+", prefix=f"playtimereport{until}", suffix="csv") as temp:
with TemporaryFile(
"w+", prefix=f"playtimereport{formatted_until_date}", suffix="csv"
) as temp:
# Write the data as a CSV
writer = csv.writer(temp)
writer.writerow(
Expand All @@ -136,7 +153,7 @@ def do_run(self):
)
title = edition and edition.title
row = (
f"{start} - {until}",
report_date_label,
urn,
collection_name,
library_name,
Expand All @@ -153,10 +170,10 @@ def do_run(self):
)
if recipient:
EmailManager.send_email(
f"Playtime Summaries {start} - {until}",
email_subject,
receivers=[recipient],
text="",
attachments={f"playtime-summary-{start}-{until}": temp.read()},
attachments={attachment_name: temp.read()},
)
else:
self.log.error("No reporting email found, logging complete report.")
Expand Down

0 comments on commit d5ea5d4

Please sign in to comment.