class PerformanceTemplateView(CourseTemplateWithNavView, CourseAPIMixin): """ Base view for course performance pages. """ assignment_type = None assignment_id = None assignment = None presenter = None # Translators: Do not translate UTC. update_message = _( 'Problem submission data was last updated %(update_date)s at %(update_time)s UTC.' ) secondary_nav_items = [ { 'name': 'graded_content', 'label': _('Graded Content'), 'view': 'courses:performance_graded_content' }, ] active_primary_nav_item = 'performance' page_title = _('Graded Content') active_secondary_nav_item = 'graded_content' def dispatch(self, request, *args, **kwargs): self.assignment_id = kwargs.get('assignment_id') try: return super(PerformanceTemplateView, self).dispatch(request, *args, **kwargs) except SlumberBaseException as e: # Return the appropriate response if a 404 occurred. response = getattr(e, 'response') if response is not None and response.status_code == 404: logger.info('Course API data not found for %s: %s', self.course_id, e) raise Http404 # Not a 404. Continue raising the error. logger.error( 'An error occurred while using Slumber to communicate with an API: %s', e) raise def _deslugify_assignment_type(self): """ Assignment type is slugified in the templates to avoid issues with our URL regex failing to match unknown assignment types. This method changes the assignment type to the human-friendly version. If no match is found, the assignment type is unchanged. """ for assignment_type in self.presenter.assignment_types(): if self.assignment_type == slugify(assignment_type): self.assignment_type = assignment_type break def get_context_data(self, **kwargs): context = super(PerformanceTemplateView, self).get_context_data(**kwargs) self.presenter = CoursePerformancePresenter(self.access_token, self.course_id) context['assignment_types'] = self.presenter.assignment_types() if self.assignment_id: assignment = self.presenter.assignment(self.assignment_id) if assignment: context['assignment'] = assignment context['assignment_name'] = assignment['name'] self.assignment = assignment self.assignment_type = assignment['assignment_type'] else: logger.info('Assignment %s not found.', self.assignment_id) raise Http404 if self.assignment_type: self._deslugify_assignment_type() assignments = self.presenter.assignments(self.assignment_type) context['js_data']['course']['assignments'] = assignments context['js_data']['course'][ 'assignmentsHaveSubmissions'] = self.presenter.has_submissions( assignments) context['js_data']['course'][ 'assignmentType'] = self.assignment_type context.update({ 'assignment_type': self.assignment_type, 'assignments': assignments, 'update_message': self.get_last_updated_message(self.presenter.last_updated) }) return context