From ae4aef150899ad3be6402fd37c90345b6713d8c2 Mon Sep 17 00:00:00 2001 From: Markku Riekkinen Date: Fri, 5 Mar 2021 14:02:43 +0200 Subject: [PATCH] Pick the latest visible course instance in the redirect If the course has only hidden instances and the user is course staff, then the latest hidden instance is used in the redirect. Otherwise, the latest visible instance is always used. Fix https://github.com/apluslms/a-plus/issues/791 --- course/views.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/course/views.py b/course/views.py index 039029df8..c79591804 100644 --- a/course/views.py +++ b/course/views.py @@ -113,11 +113,22 @@ def get_resource_objects(self): course = get_object_or_404(Course, url=self._get_kwarg(self.course_kw)) course_instances = CourseInstance.objects.filter(course=course).order_by('-starting_time') - if course_instances: - self.course_instance = course_instances[0] + latest_visible = None + latest_hidden = None + for instance in course_instances: + if instance.visible_to_students: + latest_visible = instance + break + else: + latest_hidden = latest_hidden or instance + + if latest_visible: + self.course_instance = latest_visible + elif latest_hidden and latest_hidden.is_course_staff(self.request.user): + self.course_instance = latest_hidden else: raise Http404(_("There are no course instances for this course.")) - + def get(self, request, *args, **kwargs): return self.redirect(self.course_instance.url)