Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Fix date_last_active returning None too often
Browse files Browse the repository at this point in the history
It now correctly filters out None entries for claim_time and complete_time.
  • Loading branch information
TimJentzsch committed Feb 6, 2023
1 parent 350eb08 commit adbed68
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
6 changes: 4 additions & 2 deletions blossom/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ def date_last_active(self) -> Optional[datetime]:
This will give the time where the user last claimed or completed a post.
"""
recently_claimed = (
Submission.objects.filter(claimed_by=self).order_by("-claim_time").first()
Submission.objects.filter(claimed_by=self, claim_time__isnull=False)
.order_by("-claim_time")
.first()
)
recent_claim_time = recently_claimed.claim_time if recently_claimed else None

recently_completed = (
Submission.objects.filter(completed_by=self)
Submission.objects.filter(completed_by=self, complete_time__isnull=False)
.order_by("-complete_time")
.first()
)
Expand Down
46 changes: 45 additions & 1 deletion blossom/authentication/tests/test_blossomuser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from typing import Optional
from typing import List, Optional, Tuple
from unittest.mock import PropertyMock, patch

import pytest
Expand Down Expand Up @@ -269,3 +269,47 @@ def test_transcription_check_reason(
return_value=check_percentage,
):
assert user.transcription_check_reason() == expected


@pytest.mark.parametrize(
"submission_times, expected",
[
([(None, None)], None),
([(None, datetime(2023, 1, 10))], datetime(2023, 1, 10)),
(
[(None, None), (datetime(2023, 1, 10), datetime(2023, 1, 11))],
datetime(2023, 1, 11),
),
(
[
(datetime(2023, 1, 3), datetime(2023, 1, 4)),
(datetime(2023, 1, 10), datetime(2023, 1, 11)),
],
datetime(2023, 1, 11),
),
],
)
def test_date_last_active(
client: Client,
submission_times: List[Tuple[datetime, datetime]],
expected: Optional[datetime],
) -> None:
"""Ensure that the last activity date is calculated correctly."""
client, headers, user = setup_user_client(client, id=123, username="Test")

for idx, (claim_time, complete_time) in enumerate(submission_times):
create_submission(
id=idx + 100,
claimed_by=user,
completed_by=user,
claim_time=None
if claim_time is None
else claim_time.replace(tzinfo=pytz.UTC),
complete_time=None
if complete_time is None
else complete_time.replace(tzinfo=pytz.UTC),
)

expected = None if expected is None else expected.replace(tzinfo=pytz.UTC)

assert user.date_last_active() == expected

0 comments on commit adbed68

Please sign in to comment.