def get_context_data(self, **kwargs): """Get any existing application and rating.""" context = super().get_context_data(**kwargs) context['activity_enum'] = enums.Activity(self.activity) context['year'] = self.application_year existing = self.get_queryset().filter(participant=self.par) accepting_apps = models.LeaderApplication.accepting_applications( self.activity) context['accepting_applications'] = accepting_apps if existing: app = existing.order_by('-time_created').first() context['application'] = app # TODO: Move this validation into the form/route too. can_apply = accepting_apps and models.LeaderApplication.can_reapply( app) context['can_apply'] = can_apply else: context['can_apply'] = accepting_apps context[ 'climbing_form_url'] = models.ClimbingLeaderApplication.google_form_url( participant=self.request.participant, embedded=True, ) return context
def dispatch(self, request, *args, **kwargs): try: activity_enum = enums.Activity(kwargs.get('activity')) except ValueError: raise Http404 # pylint: disable=raise-missing-from if not perm_utils.chair_or_admin(request.user, activity_enum): raise PermissionDenied if not models.LeaderApplication.can_apply_for_activity(self.activity): context = { 'missing_form': True, 'activity': self.activity, 'activity_enum': enums.Activity(self.activity), } return render(request, self.template_name, context) return super().dispatch(request, *args, **kwargs)
def dispatch(self, request, *args, **kwargs): try: activity_enum = enums.Activity(kwargs.get('activity')) except ValueError: raise Http404 # pylint: disable=raise-missing-from if not perm_utils.is_chair(request.user, activity_enum): raise PermissionDenied return super().dispatch(request, *args, **kwargs)
def num_chairs(self): """ Return the number of chairs for this activity. """ # It's important that this remain a property (dynamically requested, not stored at init) # This way, views that want to get activity from self.kwargs can inherit from the mixin if not hasattr(self, '_num_chairs'): activity_enum = enums.Activity(self.activity) self._num_chairs = perm_utils.num_chairs(activity_enum) return self._num_chairs
def dispatch(self, request, *args, **kwargs): try: activity_enum = enums.Activity(kwargs.get('activity')) except ValueError: raise Http404 if not perm_utils.chair_or_admin(request.user, activity_enum): raise PermissionDenied return super().dispatch(request, *args, **kwargs)
def dispatch(self, request, *args, **kwargs): """Redirect if anonymous, but deny permission if not a chair.""" try: activity_enum = enums.Activity(self.activity) except ValueError: raise Http404 # pylint: disable=raise-missing-from if not perm_utils.chair_or_admin(request.user, activity_enum): raise PermissionDenied return super().dispatch(request, *args, **kwargs)
def activity_descriptors(participant, user): """ Yield a description for each activity the participant is a leader. Mentions if the person is a leader or a chair, but does not give their specific ranking. """ active_ratings = participant.leaderrating_set.filter(active=True) for activity in active_ratings.values_list('activity', flat=True): activity_enum = enums.Activity(activity) position = 'chair' if is_chair(user, activity_enum, False) else 'leader' yield f"{activity_enum.label} {position}"
def get_context_data(self, **kwargs): # Super calls DetailView's `get_context_data` so we'll manually add form context = super().get_context_data(**kwargs) apps = context['leader_applications'] context['num_chairs'] = self.num_chairs context['needs_rec'] = self.needs_rec(apps) context['needs_rating'] = self.needs_rating(apps) context['pending'] = context['needs_rating'] or context['needs_rec'] context['activity_enum'] = enums.Activity(self.kwargs['activity']) accepting_apps = models.LeaderApplication.accepting_applications(self.activity) context['new_applications_disabled'] = not accepting_apps context['apps_by_year'] = self._group_applications_by_year(apps) return context
def form_valid(self, form): """ Ensure the leader can assign ratings, then apply assigned rating. Any existing ratings for this activity will be marked as inactive. """ activity_enum = enums.Activity(form.cleaned_data['activity']) participant = form.cleaned_data['participant'] ratings_utils.deactivate_ratings(participant, activity_enum.value) rating = form.save(commit=False) rating.creator = self.request.participant msg = "Gave {} rating of '{}'".format(participant, rating.rating) messages.success(self.request, msg) return super().form_valid(form)
def form_valid(self, form): """Ensure the leader can assign ratings, then apply assigned rating. Any existing ratings for this activity will be marked as inactive. """ # TODO: Consider removing `activity` from the form and just using URL? activity_enum = enums.Activity(form.cleaned_data['activity']) participant = form.cleaned_data['participant'] ratings_utils.deactivate_ratings(participant, activity_enum.value) rating = form.save(commit=False) rating.creator = self.request.participant messages.success(self.request, f"Gave {participant} rating: '{rating.rating}'") return super().form_valid(form)
def activity_enum(self): return enums.Activity(self.kwargs['activity'])
def activity_chairs(self) -> QuerySet[User]: """Return the chairs for this activity.""" # It's important that this remain a method, not stored at init. # This way, views that want to get activity from self.kwargs can inherit from the mixin return perm_utils.activity_chairs(enums.Activity(self.activity))