Skip to content

Commit

Permalink
Removed all references to the UpdateShiftUserDataLogEntry of the "old…
Browse files Browse the repository at this point in the history
… style" (with attendance_mode), since now they have been updated to the "new style" (with is_frozen)
  • Loading branch information
Theophile-Madet committed Dec 10, 2024
1 parent e658726 commit 32e3206
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 373 deletions.
7 changes: 0 additions & 7 deletions tapir/shifts/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import datetime

from tapir.utils.shortcuts import get_timezone_aware_datetime

cycle_start_dates = [
datetime.date(year=2022, month=4, day=11),
]
Expand All @@ -18,8 +16,3 @@
FEATURE_FLAG_FLYING_MEMBERS_REGISTRATION_REMINDER = (
"feature_flags.shifts.flying_members_registration_reminder"
)

ATTENDANCE_MODE_REFACTOR_DATE = datetime.date(year=2024, month=11, day=11)
ATTENDANCE_MODE_REFACTOR_DATETIME = get_timezone_aware_datetime(
ATTENDANCE_MODE_REFACTOR_DATE, datetime.time(hour=0, minute=0)
)
94 changes: 11 additions & 83 deletions tapir/shifts/services/frozen_status_history_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
from django.db.models.functions import Coalesce
from django.utils import timezone

from tapir.shifts.config import ATTENDANCE_MODE_REFACTOR_DATETIME
from tapir.shifts.models import (
ShiftUserData,
ShiftAttendanceMode,
UpdateShiftUserDataLogEntry,
)
from tapir.utils.shortcuts import ensure_datetime
Expand Down Expand Up @@ -59,77 +57,6 @@ def annotate_shift_user_data_queryset_with_is_frozen_at_datetime(
queryset = queryset.annotate(
**{cls.ANNOTATION_IS_FROZEN_DATE_CHECK: Value(at_datetime)}
)
if at_datetime < ATTENDANCE_MODE_REFACTOR_DATETIME:
return cls._annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor(
queryset, at_datetime, attendance_mode_prefix
)
return cls._annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor(
queryset, at_datetime, attendance_mode_prefix
)

@classmethod
def _annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor(
cls,
queryset: QuerySet,
at_datetime: datetime.datetime = None,
attendance_mode_prefix=None,
):
queryset = queryset.annotate(
attendance_mode_from_log_entry=Subquery(
UpdateShiftUserDataLogEntry.objects.filter(
user_id=OuterRef("user_id"),
created_date__gte=at_datetime,
old_values__attendance_mode__isnull=False,
)
.order_by("created_date")
.values("old_values__attendance_mode")[:1],
output_field=CharField(),
)
)

queryset = queryset.annotate(
is_frozen_from_log_entry=Subquery(
UpdateShiftUserDataLogEntry.objects.filter(
user_id=OuterRef("user_id"),
created_date__gte=at_datetime,
)
.order_by("created_date")
.values("old_values__is_frozen")[:1],
output_field=CharField(),
)
)

queryset = queryset.annotate(
is_frozen_at_date=Case(
When(
attendance_mode_from_log_entry=ShiftAttendanceMode.FROZEN,
then=Value(True),
),
When(
is_frozen_from_log_entry="True",
then=Value(True),
),
When(
is_frozen_from_log_entry="False",
then=Value(False),
),
default=(
"is_frozen"
if not attendance_mode_prefix
else f"{attendance_mode_prefix}__is_frozen"
),
)
)

return queryset

@classmethod
def _annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor(
cls,
queryset: QuerySet,
at_datetime: datetime.datetime = None,
attendance_mode_prefix=None,
):
queryset = queryset.annotate(
is_frozen_from_log_entry_as_string=Subquery(
UpdateShiftUserDataLogEntry.objects.filter(
Expand All @@ -151,19 +78,20 @@ def _annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor
)
)

queryset = queryset.annotate(
is_frozen_at_date=Coalesce(
"is_frozen_from_log_entry_as_bool",
(
"is_frozen"
if attendance_mode_prefix is None
else f"{attendance_mode_prefix}__is_frozen"
return queryset.annotate(
**{
cls.ANNOTATION_IS_FROZEN_DATE_CHECK: Value(at_datetime),
cls.ANNOTATION_IS_FROZEN_AT_DATE: Coalesce(
"is_frozen_from_log_entry_as_bool",
(
"is_frozen"
if attendance_mode_prefix is None
else f"{attendance_mode_prefix}__is_frozen"
),
),
),
}
)

return queryset

@classmethod
def annotate_share_owner_queryset_with_is_frozen_at_datetime(
cls, queryset: QuerySet, at_datetime: datetime.datetime = None
Expand Down
18 changes: 0 additions & 18 deletions tapir/shifts/services/frozen_status_management_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from tapir.shifts.emails.unfreeze_notification_email import UnfreezeNotificationEmail
from tapir.shifts.models import (
ShiftUserData,
ShiftAttendanceMode,
UpdateShiftUserDataLogEntry,
ShiftAttendanceTemplate,
ShiftAccountEntry,
Expand Down Expand Up @@ -179,20 +178,3 @@ def unfreeze_and_send_notification_email(
)
email = UnfreezeNotificationEmail()
email.send_to_tapir_user(actor=actor, recipient=shift_user_data.user)

@staticmethod
def _get_last_attendance_mode_before_frozen(shift_user_data: ShiftUserData):
last_freeze_log_entry = (
UpdateShiftUserDataLogEntry.objects.filter(
new_values__attendance_mode=ShiftAttendanceMode.FROZEN,
user=shift_user_data.user,
)
.order_by("-created_date")
.first()
)

return (
last_freeze_log_entry.old_values["attendance_mode"]
if last_freeze_log_entry
else ShiftAttendanceMode.FLYING
)
162 changes: 8 additions & 154 deletions tapir/shifts/tests/test_frozen_status_history_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

from tapir.accounts.models import TapirUser
from tapir.accounts.tests.factories.factories import TapirUserFactory
from tapir.shifts import config
from tapir.shifts.models import (
ShiftUserData,
UpdateShiftUserDataLogEntry,
ShiftAttendanceMode,
)
from tapir.shifts.services.frozen_status_history_service import (
FrozenStatusHistoryService,
Expand Down Expand Up @@ -92,123 +90,8 @@ def test_isFrozenAtDatetime_annotatedNotFrozen_returnsFalse(self):
)
)

@patch.object(
FrozenStatusHistoryService,
"_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor",
)
@patch.object(
FrozenStatusHistoryService,
"_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor",
)
def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetime_givenDateIsBeforeRefactor_useCorrectAnnotationMethod(
self,
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor: Mock,
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor: Mock,
):
expected_result = Mock()
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor.return_value = (
expected_result
)
reference_datetime = (
config.ATTENDANCE_MODE_REFACTOR_DATETIME - datetime.timedelta(days=1)
)

actual_result = FrozenStatusHistoryService.annotate_shift_user_data_queryset_with_is_frozen_at_datetime(
ShiftUserData.objects.none(),
reference_datetime,
)

self.assertEqual(actual_result, expected_result)
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor.assert_called_once()
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor.assert_not_called()

@patch.object(
FrozenStatusHistoryService,
"_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor",
)
@patch.object(
FrozenStatusHistoryService,
"_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor",
)
def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetime_givenDateIsAfterRefactor_useCorrectAnnotationMethod(
self,
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor: Mock,
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor: Mock,
):
expected_result = Mock()
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor.return_value = (
expected_result
)
reference_datetime = (
config.ATTENDANCE_MODE_REFACTOR_DATETIME + datetime.timedelta(days=1)
)

actual_result = FrozenStatusHistoryService.annotate_shift_user_data_queryset_with_is_frozen_at_datetime(
ShiftUserData.objects.none(),
reference_datetime,
)

self.assertEqual(actual_result, expected_result)
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor.assert_not_called()
mock_annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor.assert_called_once()

def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetimeBeforeRefactor_noRelevantLogEntry_annotatesCurrentValue(
self,
):
tapir_user: TapirUser = TapirUserFactory.create()
tapir_user.shift_user_data.is_frozen = False
tapir_user.shift_user_data.save()
reference_datetime = timezone.now()
log_entry_in_the_past = UpdateShiftUserDataLogEntry.objects.create(
user=tapir_user,
old_values={"attendance_mode": ShiftAttendanceMode.FROZEN},
new_values={},
)
log_entry_in_the_past.created_date = reference_datetime - datetime.timedelta(
days=1
)
log_entry_in_the_past.save()

log_entry_irrelevant = UpdateShiftUserDataLogEntry.objects.create(
user=tapir_user,
old_values={"shift_partner": "12"},
new_values={"shift_partner": "13"},
)
log_entry_irrelevant.created_date = reference_datetime + datetime.timedelta(
days=1
)
log_entry_irrelevant.save()

queryset = FrozenStatusHistoryService._annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor(
ShiftUserData.objects.all(), reference_datetime
)

shift_user_data = queryset.first()
self.assertEqual(
getattr(
shift_user_data,
FrozenStatusHistoryService.ANNOTATION_IS_FROZEN_AT_DATE,
),
False,
)

def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetimeBeforeRefactor_hasRelevantLogEntry_annotatesLogEntryValue(
self,
):
tapir_user: TapirUser = TapirUserFactory.create()
tapir_user.shift_user_data.is_frozen = False
tapir_user.shift_user_data.save()
reference_datetime = timezone.now()
relevant_log_entry = UpdateShiftUserDataLogEntry.objects.create(
user=tapir_user,
old_values={"attendance_mode": ShiftAttendanceMode.FROZEN},
new_values={},
)
relevant_log_entry.created_date = reference_datetime + datetime.timedelta(
days=5
)
relevant_log_entry.save()

@staticmethod
def create_irrelevant_log_entry(tapir_user, reference_datetime):
not_relevant_log_entry = UpdateShiftUserDataLogEntry.objects.create(
user=tapir_user,
old_values={"shift_partner": 182},
Expand All @@ -219,20 +102,7 @@ def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetimeBeforeRefactor_hasRe
)
not_relevant_log_entry.save()

queryset = FrozenStatusHistoryService._annotate_shift_user_data_queryset_with_is_frozen_at_datetime_before_refactor(
ShiftUserData.objects.all(), reference_datetime
)

shift_user_data = queryset.first()
self.assertEqual(
getattr(
shift_user_data,
FrozenStatusHistoryService.ANNOTATION_IS_FROZEN_AT_DATE,
),
True,
)

def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetimeAfterRefactor_noRelevantLogEntry_annotatesCurrentValue(
def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetime_noRelevantLogEntry_annotatesCurrentValue(
self,
):
tapir_user: TapirUser = TapirUserFactory.create()
Expand All @@ -249,17 +119,9 @@ def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetimeAfterRefactor_noRele
)
log_entry_in_the_past.save()

not_relevant_log_entry = UpdateShiftUserDataLogEntry.objects.create(
user=tapir_user,
old_values={"shift_partner": 182},
new_values={"shift_partner": 25},
)
not_relevant_log_entry.created_date = reference_datetime + datetime.timedelta(
days=3
)
not_relevant_log_entry.save()
self.create_irrelevant_log_entry(tapir_user, reference_datetime)

queryset = FrozenStatusHistoryService._annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor(
queryset = FrozenStatusHistoryService.annotate_shift_user_data_queryset_with_is_frozen_at_datetime(
ShiftUserData.objects.all(), reference_datetime
)

Expand All @@ -272,7 +134,7 @@ def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetimeAfterRefactor_noRele
False,
)

def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetimeAfterRefactor_hasRelevantLogEntry_annotatesLogEntryValue(
def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetime_hasRelevantLogEntry_annotatesLogEntryValue(
self,
):
tapir_user: TapirUser = TapirUserFactory.create()
Expand All @@ -289,17 +151,9 @@ def test_annotateShiftUserDataQuerysetWithIsFrozenAtDatetimeAfterRefactor_hasRel
)
log_entry_in_the_past.save()

not_relevant_log_entry = UpdateShiftUserDataLogEntry.objects.create(
user=tapir_user,
old_values={"shift_partner": 182},
new_values={"shift_partner": 25},
)
not_relevant_log_entry.created_date = reference_datetime + datetime.timedelta(
days=3
)
not_relevant_log_entry.save()
self.create_irrelevant_log_entry(tapir_user, reference_datetime)

queryset = FrozenStatusHistoryService._annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor(
queryset = FrozenStatusHistoryService.annotate_shift_user_data_queryset_with_is_frozen_at_datetime(
ShiftUserData.objects.all(), reference_datetime
)

Expand Down
Loading

0 comments on commit 32e3206

Please sign in to comment.