Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions studies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,27 @@ def has_reached_max_responses(self) -> bool:
def check_and_pause_if_at_max_responses(self):
"""Check if max responses reached and pause the study if so.

Only pauses if the study is currently active.
Only pauses if the study is currently active. Uses the state machine's
pause trigger to properly transition and run callbacks.
"""
# TODO: Implement logic to pause study when max responses reached
pass
if self.max_responses is None:
return

if self.state != "active":
return

if not self.has_reached_max_responses:
return

# Refresh from DB to ensure the in-memory study is current before
# the pause transition triggers a save (via _finalize_state_change).
self.refresh_from_db()

# Use the state machine's pause trigger to properly transition
# and run callbacks (like notify_administrators_of_pause)
# Note: no explicit save() needed here because the state machine's
# _finalize_state_change callback already saves the model.
self.pause() # No user since this is system-triggered

@property
def consent_videos(self):
Expand Down Expand Up @@ -825,12 +842,16 @@ def notify_administrators_of_activation(self, ev):
)

def notify_administrators_of_pause(self, ev):
user = ev.kwargs.get("user")
caller_name = (
user.get_short_name() if user else "System (max responses reached)"
)
context = {
"lab_name": self.lab.name,
"study_name": self.name,
"study_id": self.pk,
"study_uuid": str(self.uuid),
"researcher_name": ev.kwargs.get("user").get_short_name(),
"researcher_name": caller_name,
"action": ev.transition.dest,
}
send_mail.delay(
Expand Down Expand Up @@ -998,6 +1019,12 @@ def check_modification_of_approved_study(
):
continue # Skip, since the actual JSON content is the same - only exact_text changing
if new != current:
# For file fields (e.g. image), None and "" are equivalent empty
# values that can differ between in-memory defaults and DB-loaded
# values. Treat them as unchanged.
if hasattr(current, "name") and hasattr(new, "name"):
if (current.name or "") == (new.name or ""):
continue
important_fields_changed = True
break

Expand Down
Loading