Beispiel #1
0
 def test_leader_on_trip_creator(self):
     trip = TripFactory()
     self.assertTrue(
         perm_utils.leader_on_trip(trip.creator, trip,
                                   creator_allowed=True))
     self.assertFalse(
         perm_utils.leader_on_trip(trip.creator,
                                   trip,
                                   creator_allowed=False))
Beispiel #2
0
 def _can_view(trip, request):
     """Leaders, chairs, and a trip WIMP can view this page."""
     return (perm_utils.in_any_group(request.user, ['WIMP'])
             or (trip.wimp and request.participant == trip.wimp)
             or perm_utils.leader_on_trip(request.participant, trip, True)
             or perm_utils.chair_or_admin(request.user,
                                          trip.required_activity_enum()))
Beispiel #3
0
def view_trip(trip, participant, user):
    # For efficiency, the trip should be called with:
    #      select_related('info')
    #      prefetch_related('leaders', 'leaders__leaderrating_set')

    def get_signups(model=models.SignUp):
        """ Signups, with related fields used in template preselected. """
        signups = model.objects.filter(trip=trip)
        signups = signups.select_related('participant', 'trip')
        return signups.select_related('participant__lotteryinfo')

    context = {
        'trip': trip,
        'is_trip_leader': perm_utils.leader_on_trip(participant, trip),
        'viewing_participant': participant,
        'user': user
    }

    signups = get_signups(models.SignUp)
    context['par_signup'] = signups.filter(participant=participant).first()
    wl_signups = trip.waitlist.signups.select_related('participant',
                                                      'participant__lotteryinfo')
    context['signups'] = {
        'waitlist': wl_signups,
        'off_trip': signups.filter(on_trip=False).exclude(pk__in=wl_signups),
        'on_trip': signups.filter(on_trip=True),
        'leader': get_signups(models.LeaderSignUp)
    }
    context['has_notes'] = (bool(trip.notes) or
                            any(s.notes for s in signups) or
                            any(s.notes for s in context['signups']['leader']))
    return context
Beispiel #4
0
def view_trip(trip, participant, user):
    # For efficiency, the trip should be called with:
    #      select_related('info')
    #      prefetch_related('leaders', 'leaders__leaderrating_set')

    def get_signups(model=models.SignUp):
        """ Signups, with related fields used in template preselected. """
        signups = model.objects.filter(trip=trip)
        signups = signups.select_related('participant', 'trip')
        return signups.select_related('participant__lotteryinfo')

    context = {
        'trip': trip,
        'is_trip_leader': perm_utils.leader_on_trip(participant, trip),
        'viewing_participant': participant,
        'user': user,
    }

    signups = get_signups(models.SignUp)
    context['par_signup'] = signups.filter(participant=participant).first()
    wl_signups = trip.waitlist.signups.select_related(
        'participant', 'participant__lotteryinfo'
    )
    context['signups'] = {
        'waitlist': wl_signups,
        'off_trip': signups.filter(on_trip=False).exclude(pk__in=wl_signups),
        'on_trip': signups.filter(on_trip=True),
        'leader': get_signups(models.LeaderSignUp),
    }
    context['has_notes'] = (
        bool(trip.notes)
        or any(s.notes for s in signups)
        or any(s.notes for s in context['signups']['leader'])
    )
    return context
Beispiel #5
0
def trip_info(context, trip, show_participants_if_no_itinerary=False):
    participant = context['viewing_participant']

    # After a sufficiently long waiting period, hide medical information
    # (We could need medical info a day or two after a trip was due back)
    # Some trips last for multiple days (trip date is Friday, return is Sunday)
    # Because we only record a single trip date, give a few extra days' buffer
    is_old_trip = date_utils.local_date() > (trip.trip_date +
                                             timedelta(days=5))

    return {
        'trip':
        trip,
        'participants': (trip.signed_up_participants.filter(
            signup__on_trip=True).select_related(
                'emergency_info__emergency_contact')),
        'trip_leaders':
        (trip.leaders.select_related('emergency_info__emergency_contact')),
        'cars':
        get_cars(trip),
        'show_participants_if_no_itinerary':
        show_participants_if_no_itinerary,
        'hide_sensitive_info':
        is_old_trip,
        'is_trip_leader':
        perm_utils.leader_on_trip(participant, trip),
    }
Beispiel #6
0
 def dispatch(self, request, *args, **kwargs):
     """ Only allow creator, leaders of the trip, and chairs. """
     trip = self.get_object()
     chair = perm_utils.chair_or_admin(request.user, trip.activity)
     trip_leader = perm_utils.leader_on_trip(request.participant, trip, True)
     if not (chair or trip_leader):
         return render(request, 'not_your_trip.html', {'trip': trip})
     return super().dispatch(request, *args, **kwargs)
Beispiel #7
0
 def _can_view(self, trip, request):
     """ Leaders, chairs, and a trip WIMP can view this page. """
     return (
         perm_utils.in_any_group(request.user, ['WIMP'])
         or (trip.wimp and request.participant == trip.wimp)
         or perm_utils.leader_on_trip(request.participant, trip, True)
         or perm_utils.chair_or_admin(request.user, trip.activity)
     )
Beispiel #8
0
def trip_edit_buttons(trip, participant, user, hide_approve=False):
    return {
        'trip': trip,
        'is_chair': perm_utils.chair_or_admin(user, trip.activity),
        'is_creator': trip.creator == participant,
        'is_trip_leader': perm_utils.leader_on_trip(participant, trip, False),
        'hide_approve': hide_approve  # Hide approval even if user is a chair
    }
    def get_context_data(self, **kwargs):
        """ Get a trip info form for display as readonly. """
        context_data = super().get_context_data(**kwargs)
        trip = self.object
        participant = self.request.participant
        context_data['is_trip_leader'] = perm_utils.leader_on_trip(participant, trip)

        return context_data
Beispiel #10
0
 def dispatch(self, request, *args, **kwargs):
     """ Only allow creator, leaders of the trip, and chairs. """
     trip = self.get_object()
     chair = perm_utils.chair_or_admin(request.user, trip.activity)
     trip_leader = perm_utils.leader_on_trip(request.participant, trip,
                                             True)
     if not (chair or trip_leader):
         return render(request, 'not_your_trip.html', {'trip': trip})
     return super().dispatch(request, *args, **kwargs)
Beispiel #11
0
def _allowed_to_modify_trip(trip: models.Trip, request: HttpRequest) -> bool:
    activity_enum = trip.required_activity_enum()
    if activity_enum:
        is_chair = perm_utils.chair_or_admin(request.user, activity_enum)
    else:  # (There is no required activity, so no chairs. Allow superusers, though)
        is_chair = request.user.is_superuser

    participant: models.Participant = request.participant  # type: ignore
    trip_leader = perm_utils.leader_on_trip(participant, trip, True)
    return is_chair or trip_leader
Beispiel #12
0
def trip_edit_buttons(trip, participant, user, hide_approve=False):
    available_at = date_utils.itinerary_available_at(trip.trip_date)
    return {
        'trip': trip,
        'is_chair': perm_utils.chair_or_admin(user, trip.activity),
        'is_creator': trip.creator == participant,
        'is_trip_leader': perm_utils.leader_on_trip(participant, trip, False),
        'hide_approve': hide_approve,  # Hide approval even if user is a chair
        'itinerary_available_at': available_at,
        'available_today': available_at.date() == date_utils.local_date(),
        'info_form_available': date_utils.local_now() >= available_at,
    }
Beispiel #13
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        trip = self.object

        context['leader_on_trip'] = perm_utils.leader_on_trip(
            self.request.participant, trip, True)
        context['can_admin'] = context[
            'leader_on_trip'] or perm_utils.chair_or_admin(
                self.request.user, trip.required_activity_enum())
        if context['can_admin'] or perm_utils.is_leader(self.request.user):
            context['rentals_by_par'] = list(self.rentals_by_participant(trip))
        return context
Beispiel #14
0
def trip_edit_buttons(trip, participant, user, hide_approve=False):
    available_at = date_utils.itinerary_available_at(trip.trip_date)
    return {
        'trip': trip,
        'is_chair': perm_utils.chair_or_admin(user, trip.activity),
        'is_creator': trip.creator == participant,
        'is_trip_leader': perm_utils.leader_on_trip(participant, trip, False),
        'hide_approve': hide_approve,  # Hide approval even if user is a chair
        'itinerary_available_at': available_at,
        'available_today': available_at.date() == date_utils.local_date(),
        'info_form_available': date_utils.local_now() >= available_at
    }
Beispiel #15
0
    def get_context_data(self, **kwargs):
        """ Get a trip info form for display as readonly. """
        trip = self.object
        participant = self.request.participant
        context_data = self.get_trip_info(trip)
        context_data['is_trip_leader'] = perm_utils.leader_on_trip(participant, trip)
        context_data['info_form'] = self.get_info_form(trip)

        # After a sufficiently long waiting period, hide medical information
        # (We could need medical info a day or two after a trip was due back)
        # Some trips last for multiple days (trip date is Friday, return is Sunday)
        # Because we only record a single trip date, give a few extra days' buffer
        is_past_trip = local_date() > (trip.trip_date + timedelta(days=5))
        context_data['hide_sensitive_info'] = is_past_trip
        return context_data
Beispiel #16
0
    def dispatch(self, request, *args, **kwargs):
        """ Only allow creator, leaders of the trip, and chairs. """
        trip = self.get_object()

        activity_enum = trip.required_activity_enum()
        if activity_enum:
            is_chair = perm_utils.chair_or_admin(request.user, activity_enum)
        else:  # (There is no required activity, so no chairs. Allow superusers, though)
            is_chair = request.user.is_superuser

        trip_leader = perm_utils.leader_on_trip(request.participant, trip,
                                                True)
        if not (is_chair or trip_leader):
            return render(request, 'not_your_trip.html', {'trip': trip})
        return super().dispatch(request, *args, **kwargs)
Beispiel #17
0
 def dispatch(self, request, *args, **kwargs):
     trip = self.get_object()
     if not perm_utils.leader_on_trip(request.participant, trip, False):
         return render(request, 'not_your_trip.html', {'trip': trip})
     return super().dispatch(request, *args, **kwargs)
Beispiel #18
0
 def test_leader_on_trip(self):
     trip = TripFactory()
     self.assertFalse(perm_utils.leader_on_trip(trip.creator, trip))
     trip.leaders.add(trip.creator)
     self.assertTrue(perm_utils.leader_on_trip(trip.creator, trip))