def get_graph_queryset(self): # First, set up graphs based on a users role graphs = Graph.objects.filter( topic__in=self.object.topic_set.all()) if not user_is_instructor(self.request.user): graphs = graphs.filter(needs_submit=False, is_published=True) # Then apply filtering based on query string params params = self.request.GET if len(params) == 0: graphs = graphs.filter(featured=True) return graphs.order_by('order') elif 'topic' in params: tid = None try: tid = int(params.get('topic', '')) except ValueError: return graphs.order_by('title') if tid: graphs = graphs.filter(topic=tid) return graphs.order_by('title')
def get_context_data(self, **kwargs): context = super(CohortListView, self).get_context_data(**kwargs) # This area should never be reached if this user isn't an instructor, # but just in case. if not user_is_instructor(self.request.user): return context # Create a clone of the sample course if this user has no # courses. if not Cohort.objects.filter( instructors__in=(self.request.user,)).exists(): try: sample_course = Cohort.objects.get(is_sample=True) except Cohort.DoesNotExist: sample_course = None if sample_course: cloned_sample = sample_course.clone() cloned_sample.instructors.clear() cloned_sample.instructors.add(self.request.user) context['cohorts'] = Cohort.objects.filter( instructors__in=(self.request.user,)) return context
def dispatch(self, *args, **kwargs): if not user_is_instructor(self.request.user): return HttpResponseForbidden() attach_cohort(self, **kwargs) if self.request.user not in self.cohort.instructors.all(): return HttpResponseForbidden() return super(CohortInstructorMixin, self).dispatch(self.request, *args, **kwargs)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) graph_list = self.get_graph_queryset() if user_is_instructor(self.request.user): topics = self.object.topic_set.all() graphs = Graph.objects.filter(topic__in=topics) else: topics = self.object.topic_set.annotate( num_graphs=Count('graph', filter=Q( graph__is_published=True) )).filter(num_graphs__gt=0) graphs = Graph.objects.filter( topic__in=topics, needs_submit=False, is_published=True) context['topic_list'] = topics context['graph_list'] = graph_list context['all_count'] = graphs.count() context['featured_count'] = graphs.filter(featured=True).count() # If there are no query string params, then set featured to true. # Set active_topic guard condition, and assign to an id if present in # the query string. params = self.request.GET context['featured'] = False context['active_topic'] = '' if len(params) == 0: context['featured'] = True elif 'topic' in params: tid = params.get('topic', '') try: context['active_topic'] = int(tid) except ValueError: pass return context
def test_func(self): return user_is_instructor(self.request.user)
def dispatch(self, request, *args, **kwargs): if not user_is_instructor(request.user): url = '/accounts/login/' return HttpResponseRedirect(url) return super(CohortListView, self).dispatch(request, *args, **kwargs)
def test_user_is_instructors(self): self.assertTrue(user_is_instructor(self.instructor)) self.assertFalse(user_is_instructor(self.student))
def tag(user): return user_is_instructor(user)