Skip to content
Extraits de code Groupes Projets
Valider 549fe1d9 rédigé par Brieuc Dubois's avatar Brieuc Dubois
Parcourir les fichiers

Merge branch '170-download-study' into 'dev'

Resolve "Download study survey data"

Closes #170

See merge request !40
parents f5504078 8beb2441
Branches analytics
Aucune étiquette associée trouvée
2 requêtes de fusion!43Merge dev into main,!40Resolve "Download study survey data"
......@@ -164,3 +164,81 @@ def download_study(db: Session, study_id: int):
media_type="text/csv",
headers={"Content-Disposition": f"attachment; filename={study_id}-surveys.csv"},
)
def download_study_wide(db: Session, study_id: int):
output = StringIO()
writer = csv.writer(output)
data = {}
question_ids = set()
db_entries = (
db.query(models.TestEntry).filter(models.TestEntry.study_id == study_id).all()
)
for entry in db_entries:
if entry.entry_task is None:
continue
user_id = entry.user_id
code = entry.code
item_id = entry.entry_task.test_question_id
key = (user_id, code)
if key not in data:
if user_id is not None:
user = crud.get_user(db, user_id)
data[key] = {
"study_id": study_id,
"user_id": user_id,
"code": code,
"home_language": user.home_language,
"target_language": user.target_language,
"gender": user.gender,
"birthdate": user.birthdate,
}
else:
data[key] = {"study_id": study_id, "user_id": user_id, "code": code}
if entry.entry_task.entry_task_qcm:
selected_id = entry.entry_task.entry_task_qcm.selected_id
correct_id = entry.entry_task.test_question.question_qcm.correct
correct_answer = int(selected_id == correct_id)
data[key][item_id] = correct_answer
question_ids.add(item_id)
if entry.entry_task.entry_task_gapfill:
answer = entry.entry_task.entry_task_gapfill.text
correct = extract_text_between_angle_bracket(
entry.entry_task.test_question.question
)
correct_answer = int(answer == correct)
data[key][item_id] = correct_answer
question_ids.add(item_id)
# Sort question IDs for consistent column order
question_ids = sorted(question_ids)
header = [
"study_id",
"user_id",
"code",
"home_language",
"target_language",
"gender",
"birthdate",
] + question_ids
writer.writerow(header)
for (user_id, code), values in data.items():
row = [values.get(col, "") for col in header]
writer.writerow(row)
output.seek(0)
return StreamingResponse(
output,
media_type="text/csv",
headers={
"Content-Disposition": f"attachment; filename={study_id}-surveys-wide.csv"
},
)
......@@ -71,3 +71,15 @@ def download_study(
if study is None:
raise HTTPException(status_code=404, detail="Study not found")
return crud.download_study(db, study_id)
@require_admin("You do not have permission to download this study.")
@studiesRouter.get("/{study_id}/download/surveys-wide")
def download_study(
study_id: int,
db: Session = Depends(get_db),
):
study = crud.get_study(db, study_id)
if study is None:
raise HTTPException(status_code=404, detail="Study not found")
return crud.download_study_wide(db, study_id)
......@@ -38,10 +38,16 @@
title="Download"
href={`${config.API_URL}/v1/studies/${study.id}/download/surveys`}
>
<Icon src={ArrowDownTray} size="16" />
<Icon src={ArrowDownTray} size="16" /> CSV long
</a>
<a
class="btn btn-primary btn-sm"
title="Download"
href={`${config.API_URL}/v1/studies/${study.id}/download/surveys-wide`}
>
<Icon src={ArrowDownTray} size="16" /> CSV wide
</a></td
>
<td></td>
</tr>
{/each}
</tbody>
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter