def wimp(self):
        """ Return the current WIMP, if there is one & it's appropriate to display them. """
        participant = self.object

        # Regardless of time of year, or any upcoming WS trips, admins always see WIMP
        if self.request.user.is_superuser:
            return wimp.current_wimp()

        # If there aren't any upcoming trips (or trips today), don't show WIMP
        # This will ensure that we hide the WIMP when:
        # - It's not Winter School
        # - Winter School just ended, but `is_currently_iap()` can't tell
        # - The weekend just ended, and we don't yet have a new WIMP
        ws_trips = models.Trip.objects.filter(
            program=enums.Program.WINTER_SCHOOL.value,
            trip_date__gte=date_utils.local_date(),
        )
        if not ws_trips:
            return None

        # Participants don't need to know the WIMP, only leaders/chairs do
        if not (participant.can_lead(enums.Program.WINTER_SCHOOL)
                or perm_utils.is_chair(self.request.user,
                                       enums.Activity.WINTER_SCHOOL)):
            return None

        return wimp.current_wimp()
    def test_single_wimp(self):
        wimp_par = factories.ParticipantFactory.create()
        wimp_group = Group.objects.get(name='WIMP')
        self.assertFalse(wimp_group.user_set.exists())

        wimp_group.user_set.add(wimp_par.user)
        self.assertCountEqual(wimp.active_wimps(), [wimp_par])
        self.assertEqual(wimp_par, wimp.current_wimp())
    def test_handle_edge_case_of_user_without_participant(self):
        no_par_user = factories.UserFactory.create()
        self.assertFalse(
            models.Participant.objects.filter(user_id=no_par_user.pk).exists())

        wimp_par = factories.ParticipantFactory.create()

        wimp_group = Group.objects.get(name='WIMP')
        wimp_group.user_set.add(no_par_user)
        wimp_group.user_set.add(wimp_par.user)
        self.assertCountEqual(wimp.active_wimps(), [wimp_par])
        self.assertEqual(wimp_par, wimp.current_wimp())
    def test_wimps_sorted_by_time_made_wimp(self):
        wimp_group = Group.objects.get(name='WIMP')

        # Participants are created in different orders
        second_wimp = factories.ParticipantFactory.create()
        first_wimp = factories.ParticipantFactory.create()
        third_wimp = factories.ParticipantFactory.create()

        wimp_group.user_set.add(first_wimp.user)
        wimp_group.user_set.add(second_wimp.user)
        wimp_group.user_set.add(third_wimp.user)

        self.assertEqual(list(wimp.active_wimps()),
                         [third_wimp, second_wimp, first_wimp])
        self.assertEqual(third_wimp, wimp.current_wimp())
 def test_no_active_wimps(self):
     self.assertFalse(Group.objects.get(name='WIMP').user_set.exists())
     self.assertCountEqual(wimp.active_wimps(), [])
     self.assertIsNone(wimp.current_wimp())