feat: Remove slug field from ResearchStudy model and update related references

This commit is contained in:
Ross
2026-05-12 21:51:44 +01:00
parent c94d2fb1a7
commit 89e89cef73
10 changed files with 68 additions and 35 deletions
+14 -14
View File
@@ -8,7 +8,6 @@ from django.db import transaction
from django.http import HttpResponse, JsonResponse, Http404
from django.urls import reverse
from django.utils import timezone
from django.utils.text import slugify
from django.utils.crypto import get_random_string
from django.contrib import messages
@@ -124,7 +123,7 @@ def _ensure_research_participant_cid(participant: ResearchParticipant) -> CidUse
created_cid = CidUser.objects.create(
cid=next_cid,
passcode=get_random_string(8),
name=f"study:{participant.study.slug}:{participant.pseudo_id}",
name=f"study:{participant.study_id}:{participant.pseudo_id}",
active=True,
)
break
@@ -194,7 +193,6 @@ def _build_study_export_rows(study: ResearchStudy):
rows.append(
{
"study_id": study.pk,
"study_slug": study.slug,
"study_name": study.name,
"pseudo_id": participant.pseudo_id,
"candidate_group": participant.candidate_group,
@@ -282,9 +280,9 @@ def study_detail(request, pk: int):
if not _user_can_manage_research_study(study, request.user):
raise PermissionDenied
arm_form = ResearchStudyArmForm() if request.method != "POST" else None
arm_form = ResearchStudyArmForm(study=study) if request.method != "POST" else None
if request.method == "POST" and "add_arm" in request.POST:
arm_form = ResearchStudyArmForm(request.POST)
arm_form = ResearchStudyArmForm(request.POST, study=study)
if arm_form.is_valid():
arm = arm_form.save(commit=False)
arm.study = study
@@ -344,7 +342,7 @@ def study_export_csv(request, pk: int):
rows = _build_study_export_rows(study)
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = f'attachment; filename="study_{study.slug}_results.csv"'
response["Content-Disposition"] = f'attachment; filename="study_{study.pk}_results.csv"'
if rows:
writer = csv.DictWriter(response, fieldnames=list(rows[0].keys()))
@@ -464,19 +462,21 @@ def study_bulk_generate(request, pk: int):
# Participant Entry & Access
# ============================================================================
def participant_entry(request, slug: str, pseudo_id: str):
def participant_entry(request, study_id: int, pseudo_id: str):
"""Participant landing page with on-site randomisation and intake form."""
study = get_object_or_404(ResearchStudy, slug=slug)
study = get_object_or_404(ResearchStudy, pk=study_id)
if not study.active and not (request.user.is_authenticated and _user_can_manage_research_study(study, request.user)):
raise Http404("Study not available")
# Check generation mode
if study.generation_mode == ResearchStudy.GenerationMode.BULK_ONLY:
if not request.user.is_authenticated or not _user_can_manage_research_study(study, request.user):
# Try to get existing participant
try:
participant = ResearchParticipant.objects.get(study=study, pseudo_id=pseudo_id)
except ResearchParticipant.DoesNotExist:
# Try to get existing participant
try:
participant = ResearchParticipant.objects.get(study=study, pseudo_id=pseudo_id)
except ResearchParticipant.DoesNotExist:
if request.user.is_authenticated and _user_can_manage_research_study(study, request.user):
participant = ResearchParticipant.objects.create(study=study, pseudo_id=pseudo_id)
else:
raise Http404("Participant not found. Contact study administrator.")
else:
# AUTO_ON_ACCESS: create if doesn't exist
@@ -523,7 +523,7 @@ def participant_entry(request, slug: str, pseudo_id: str):
messages.success(request, "Intake complete. You can now start the study.")
return redirect(
"research:participant_entry",
slug=study.slug,
study_id=study.pk,
pseudo_id=participant.pseudo_id,
)
else: