Skip to content

Commit

Permalink
Welcome desk fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Theophile-Madet committed Nov 20, 2024
1 parent 78a871e commit 2294272
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 108 deletions.
8 changes: 8 additions & 0 deletions tapir/shifts/services/frozen_status_history_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,11 @@ def _annotate_shift_user_data_queryset_with_is_frozen_at_datetime_after_refactor
),
),
)

@classmethod
def annotate_share_owner_queryset_with_is_frozen_at_datetime(
cls, queryset: QuerySet, at_datetime: datetime.datetime = None
):
return cls.annotate_shift_user_data_queryset_with_is_frozen_at_datetime(
queryset, at_datetime, "user__shift_user_data"
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def optimize_queryset_for_this_service(
queryset, reference_date
)
)
queryset = FrozenStatusHistoryService.annotate_share_owner_queryset_with_is_frozen_at_datetime(
queryset, reference_time
)
return queryset

@classmethod
Expand Down Expand Up @@ -77,11 +80,11 @@ def should_show_investing_reason(share_owner: ShareOwner, reference_time, **_):
return InvestingStatusService.is_investing(share_owner, reference_time)

@staticmethod
def should_show_frozen_reason(share_owner: ShareOwner, reference_date, **_):
def should_show_frozen_reason(share_owner: ShareOwner, reference_time, **_):
return (
share_owner.user is not None
and FrozenStatusHistoryService.is_frozen_at_datetime(
share_owner.user.shift_user_data, reference_date
share_owner, reference_time
)
)

Expand Down
28 changes: 12 additions & 16 deletions tapir/welcomedesk/services/welcome_desk_warnings_service.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import datetime

from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _

from tapir.coop.models import ShareOwner
from tapir.shifts.models import ShiftAttendanceMode
from tapir.shifts.services.shift_attendance_mode_service import (
ShiftAttendanceModeService,
)
from tapir.welcomedesk.utils import get_display_name_for_welcome_desk


class WelcomeDeskWarningsService:
@classmethod
def optimize_queryset_for_this_service(
cls, queryset: QuerySet[ShareOwner]
cls, queryset: QuerySet[ShareOwner], reference_time: datetime.datetime
) -> QuerySet[ShareOwner]:
return queryset.prefetch_related(
queryset = queryset.prefetch_related(
"user",
"user__shift_user_data",
"user__shift_attendance_templates",
"user__shift_user_data__shift_exemptions",
)
queryset = ShiftAttendanceModeService.annotate_share_owner_queryset_with_attendance_mode_at_datetime(
queryset, reference_time
)

return queryset

@classmethod
def build_warnings(cls, share_owner: ShareOwner, request_user) -> list[str]:
possible_warnings = {
cls.should_show_abcd_shift_registration_warning: _(
"%(name)s is not registered to an ABCD shift yet. Make sure they plan to do it!"
),
cls.should_show_welcome_session_warning: _(
"%(name)s has not attended a welcome session yet. Make sure they plan to do it!"
),
Expand All @@ -36,16 +42,6 @@ def build_warnings(cls, share_owner: ShareOwner, request_user) -> list[str]:
if check(share_owner=share_owner)
]

@staticmethod
def should_show_abcd_shift_registration_warning(share_owner: ShareOwner) -> bool:
return (
share_owner.user
and share_owner.user.shift_user_data.attendance_mode
== ShiftAttendanceMode.REGULAR
and len(share_owner.user.shift_attendance_templates.all()) == 0
and not share_owner.user.shift_user_data.is_currently_exempted_from_shifts()
)

@staticmethod
def should_show_welcome_session_warning(share_owner: ShareOwner) -> bool:
return not share_owner.attended_welcome_session
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ def test_shouldShowFrozenReason_memberIsFrozen_returnsTrue(
self, mock_is_frozen_at_datetime: Mock
):
share_owner = Mock()
shift_user_data = Mock()
share_owner.user.shift_user_data = shift_user_data
reference_date = datetime.datetime.today()
mock_is_frozen_at_datetime.return_value = True

Expand All @@ -82,17 +80,13 @@ def test_shouldShowFrozenReason_memberIsFrozen_returnsTrue(
share_owner, reference_date
)
)
mock_is_frozen_at_datetime.assert_called_once_with(
shift_user_data, reference_date
)
mock_is_frozen_at_datetime.assert_called_once_with(share_owner, reference_date)

@patch.object(FrozenStatusHistoryService, "is_frozen_at_datetime")
def test_shouldShowFrozenReason_memberIsNotFrozen_returnsFalse(
self, mock_is_frozen_at_datetime: Mock
):
share_owner = Mock()
shift_user_data = Mock()
share_owner.user.shift_user_data = shift_user_data
reference_date = datetime.datetime.today()
mock_is_frozen_at_datetime.return_value = False

Expand All @@ -101,9 +95,7 @@ def test_shouldShowFrozenReason_memberIsNotFrozen_returnsFalse(
share_owner, reference_date
)
)
mock_is_frozen_at_datetime.assert_called_once_with(
shift_user_data, reference_date
)
mock_is_frozen_at_datetime.assert_called_once_with(share_owner, reference_date)

@patch.object(MembershipPauseService, "has_active_pause")
def test_shouldShowPausedReason_memberIsPaused_returnsTrue(
Expand Down
80 changes: 1 addition & 79 deletions tapir/welcomedesk/tests/test_welcome_desk_warnings_service.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import datetime
from unittest.mock import patch, Mock

from tapir.accounts.tests.factories.factories import TapirUserFactory
from tapir.coop.tests.factories import ShareOwnerFactory
from tapir.shifts.models import (
ShiftAttendanceMode,
ShiftAttendanceTemplate,
ShiftExemption,
)
from tapir.shifts.tests.factories import ShiftTemplateFactory
from tapir.utils.tests_utils import TapirFactoryTestBase
from tapir.welcomedesk.services.welcome_desk_warnings_service import (
WelcomeDeskWarningsService,
Expand All @@ -33,89 +26,18 @@ def test_shouldShowWelcomeSessionWarning_memberDidntGoToWelcomeSession_returnsTr
WelcomeDeskWarningsService.should_show_welcome_session_warning(share_owner)
)

def test_shouldShowAbcdShiftRegistrationWarning_memberHasNoAccount_returnsFalse(
self,
):
share_owner = ShareOwnerFactory.build()
self.assertFalse(
WelcomeDeskWarningsService.should_show_abcd_shift_registration_warning(
share_owner
)
)

def test_shouldShowAbcdShiftRegistrationWarning_memberIsFlying_returnsFalse(
self,
):
tapir_user = TapirUserFactory.create()
tapir_user.shift_user_data.attendance_mode = ShiftAttendanceMode.FLYING
self.assertFalse(
WelcomeDeskWarningsService.should_show_abcd_shift_registration_warning(
tapir_user.share_owner
)
)

def test_shouldShowAbcdShiftRegistrationWarning_memberIsRegisteredToAnAbcdShift_returnsFalse(
self,
):
tapir_user = TapirUserFactory.create()
tapir_user.shift_user_data.attendance_mode = ShiftAttendanceMode.REGULAR
shift_template = ShiftTemplateFactory.create()
ShiftAttendanceTemplate.objects.create(
user=tapir_user, slot_template=shift_template.slot_templates.first()
)
self.assertFalse(
WelcomeDeskWarningsService.should_show_abcd_shift_registration_warning(
tapir_user.share_owner
)
)

def test_shouldShowAbcdShiftRegistrationWarning_memberHasAnExemption_returnsFalse(
self,
):
tapir_user = TapirUserFactory.create()
tapir_user.shift_user_data.attendance_mode = ShiftAttendanceMode.REGULAR
ShiftExemption.objects.create(
shift_user_data=tapir_user.shift_user_data,
start_date=datetime.date.today() - datetime.timedelta(days=2),
end_date=None,
)
self.assertFalse(
WelcomeDeskWarningsService.should_show_abcd_shift_registration_warning(
tapir_user.share_owner
)
)

def test_shouldShowAbcdShiftRegistrationWarning_memberIsNotRegisteredToAnAbcdShift_returnsTrue(
self,
):
tapir_user = TapirUserFactory.create()
tapir_user.shift_user_data.attendance_mode = ShiftAttendanceMode.REGULAR
self.assertTrue(
WelcomeDeskWarningsService.should_show_abcd_shift_registration_warning(
tapir_user.share_owner
)
)

@patch.object(
WelcomeDeskWarningsService, "should_show_abcd_shift_registration_warning"
)
@patch.object(WelcomeDeskWarningsService, "should_show_welcome_session_warning")
def test_buildWarnings_default_callsAllChecks(
self,
mock_should_show_welcome_session_warning: Mock,
mock_should_show_abcd_shift_registration_warning: Mock,
):
share_owner = ShareOwnerFactory.build()
request_user = TapirUserFactory.create()
mock_should_show_welcome_session_warning.return_value = True
mock_should_show_abcd_shift_registration_warning.return_value = True

warnings = WelcomeDeskWarningsService.build_warnings(share_owner, request_user)

self.assertEqual(2, len(warnings))
self.assertEqual(1, len(warnings))
mock_should_show_welcome_session_warning.assert_called_once_with(
share_owner=share_owner
)
mock_should_show_abcd_shift_registration_warning.assert_called_once_with(
share_owner=share_owner
)
2 changes: 1 addition & 1 deletion tapir/welcomedesk/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get(self, request):
@staticmethod
def optimize_queryset(queryset, reference_time, reference_date):
queryset = WelcomeDeskWarningsService.optimize_queryset_for_this_service(
queryset
queryset, reference_time
)
queryset = (
WelcomeDeskReasonsCannotShopService.optimize_queryset_for_this_service(
Expand Down

0 comments on commit 2294272

Please sign in to comment.