Skip to content

Commit

Permalink
Hide module model answers on lifesupport
Browse files Browse the repository at this point in the history
  • Loading branch information
murhum1 committed Jun 7, 2024
1 parent 0086e01 commit 62a4243
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions exercise/cache/basetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ class CachedDataBase(CourseInstanceProto, CacheBase, Generic[ModuleEntry, Learni
exercise_index: Dict[int, LearningObjectEntry]
paths: Dict[int, Dict[str, int]]
modules: List[ModuleEntry]
is_on_lifesupport: bool
lifesupport_start: Optional[datetime]
categories: Dict[int, CategoryEntry]
total: Totals

Expand Down Expand Up @@ -529,6 +531,8 @@ def _generate_data( # pylint: disable=too-many-locals

self.url = instance.url
self.course_url_kwargs = instance.course.get_url_kwargs()
self.is_on_lifesupport = instance.is_on_lifesupport()
self.lifesupport_start = instance.lifesupport_start

self.exercise_index = exercise_index = {}
self.module_index = module_index = {}
Expand Down
16 changes: 11 additions & 5 deletions exercise/cache/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def none_min(a: Optional[float], b: Optional[float]) -> Optional[float]:
return min(a,b)
return a or b

def next_timestamp(timestamps) -> Optional[float]:
now = timezone.now().timestamp()
future_timestamps = [ts for ts in timestamps if ts and ts > now]
return min(timestamps) if future_timestamps else None


def _add_to(target: Union[ModulePoints, CategoryPoints, Totals], entry: ExercisePoints) -> None:
target.feedback_revealed = target.feedback_revealed and entry.feedback_revealed
Expand Down Expand Up @@ -952,11 +957,12 @@ def add_points(children):
self.is_model_answer_revealed = True
else:
state = ModuleRevealState(self)
self.is_model_answer_revealed = reveal_rule.is_revealed(state)
if not self.is_model_answer_revealed:
reveal_time = reveal_rule.get_reveal_time(state)
timestamp = reveal_time and reveal_time.timestamp()
self._expires_on = none_min(self._expires_on, timestamp)
self.is_model_answer_revealed = reveal_rule.is_revealed(state) and \
not self.instance.is_on_lifesupport
reveal_time = reveal_rule.get_reveal_time(state)
reveal_rule_timestamp = reveal_time and reveal_time.timestamp()
self._expires_on = next_timestamp([reveal_rule_timestamp,
self.instance.lifesupport_start.timestamp()])

return {
ModuleEntryBase: [self._params[:1]],
Expand Down
7 changes: 7 additions & 0 deletions exercise/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,13 @@ def test_can_be_shown_as_module_model_solution(self):
self.assertTrue(self.base_exercise.can_be_shown_as_module_model_solution(self.user))
self.assertTrue(self.static_exercise.can_be_shown_as_module_model_solution(self.user))

self.course_instance.ending_time = self.today - timedelta(days=2)
self.course_instance.lifesupport_time = self.yesterday
self.course_instance.save()
# Model answer chapters not visible after lifesupport time
self.assertFalse(chapter.can_be_shown_as_module_model_solution(self.user))
self.assertFalse(self.base_exercise.can_be_shown_as_module_model_solution(self.user2))

def test_reveal_rule(self):
reveal_rule = RevealRule.objects.create(
trigger=RevealRule.TRIGGER.MANUAL,
Expand Down

0 comments on commit 62a4243

Please sign in to comment.