Skip to content

Commit

Permalink
clean-up queries and use datacless for setup tool
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed Oct 18, 2024
1 parent 1cdb1d7 commit e3669b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
7 changes: 4 additions & 3 deletions osf/metrics/reporters/institutional_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
25 changes: 11 additions & 14 deletions osf_tests/metrics/reporters/test_institutional_users_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ 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))
if setup.month_last_active:
self.assertEqual(report.month_last_active, YearMonth.from_date(setup.month_last_active))
else:
self.assertEqual(report.month_last_active, setup.user.month_last_active)
self.assertEqual(report.month_last_active, setup.month_last_active)

self.assertEqual(report.account_creation_date, YearMonth.from_date(setup.user.created))
self.assertEqual(report.orcid_id, setup.orcid_id)
Expand Down Expand Up @@ -153,6 +153,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(
Expand All @@ -164,19 +165,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(
Expand Down

0 comments on commit e3669b7

Please sign in to comment.