diff --git a/osf/metrics/reporters/institutional_users.py b/osf/metrics/reporters/institutional_users.py index 1e85b31a95f..cd0afb4a6cd 100644 --- a/osf/metrics/reporters/institutional_users.py +++ b/osf/metrics/reporters/institutional_users.py @@ -148,9 +148,10 @@ def _get_last_active(self): node_logs = self.user.logs.filter(created__lt=end_date).order_by('-created') preprint_logs = self.user.preprint_logs.filter(created__lt=end_date).order_by('-created') - latest_node_log_date = node_logs.first().created if node_logs.exists() else None - latest_preprint_log_date = preprint_logs.first().created if preprint_logs.exists() else None - dates = [date for date in [latest_node_log_date, latest_preprint_log_date] if date is not None] + dates = filter(bool, [ + node_logs.values_list('created', flat=True).first(), + preprint_logs.values_list('created', flat=True).first(), + ]) latest_activity_date = max(dates, default=None) diff --git a/osf_tests/metrics/reporters/test_institutional_users_reporter.py b/osf_tests/metrics/reporters/test_institutional_users_reporter.py index a0457fdcf1f..335b788fcab 100644 --- a/osf_tests/metrics/reporters/test_institutional_users_reporter.py +++ b/osf_tests/metrics/reporters/test_institutional_users_reporter.py @@ -52,10 +52,7 @@ def _assert_report_matches_setup(self, report: InstitutionalUserReport, setup: _ self.assertEqual(report.user_name, setup.user.fullname) self.assertEqual(report.department_name, setup.department_name) self.assertEqual(report.month_last_login, YearMonth.from_date(setup.user.date_last_login)) - if setup.user.month_last_active: - self.assertEqual(report.month_last_active, YearMonth.from_date(setup.user.month_last_active)) - else: - self.assertEqual(report.month_last_active, setup.user.month_last_active) + self.assertEqual(report.month_last_active, YearMonth.from_date(setup.month_last_active)) self.assertEqual(report.account_creation_date, YearMonth.from_date(setup.user.created)) self.assertEqual(report.orcid_id, setup.orcid_id) @@ -153,6 +150,7 @@ class _InstiUserSetup: department_name: str | None = None orcid_id: str | None = None user: osfdb.OSFUser = dataclasses.field(init=False) + month_last_active: YearMonth | None = dataclasses.field(init=False) def __post_init__(self): self.user = UserFactory( @@ -164,19 +162,15 @@ def __post_init__(self): ), ) self._add_affiliations(self._generate_counted_objects()) - recent_node_log = self.user.logs.order_by('-created').first() + node_logs = self.user.logs.order_by('-created') + preprint_logs = self.user.preprint_logs.order_by('-created') - recent_preprint_log = self.user.preprint_logs.order_by('-created').first() + dates = filter(bool, [ + node_logs.values_list('created', flat=True).first(), + preprint_logs.values_list('created', flat=True).first(), + ]) - recent_node_log_date = recent_node_log.created if recent_node_log else None - recent_preprint_log_date = recent_preprint_log.created if recent_preprint_log else None - - dates = [date for date in [recent_node_log_date, recent_preprint_log_date] if date is not None] - - if dates: - self.user.month_last_active = max(dates) - else: - self.user.month_last_active = None + self.month_last_active = max(dates, default=None) def affiliate_user(self): self.user.add_or_update_affiliated_institution(