|
7 | 7 | from flask_jwt_extended import (
|
8 | 8 | create_access_token, get_jwt, get_jwt_identity, jwt_required,
|
9 | 9 | set_access_cookies, unset_access_cookies)
|
10 |
| -from sqlalchemy import bindparam, func, or_, text, true |
| 10 | +from sqlalchemy import and_, bindparam, func, or_, text, true |
| 11 | +from sqlalchemy.orm import aliased |
11 | 12 | from sqlalchemy.orm.exc import NoResultFound
|
12 | 13 | from webargs import fields
|
13 | 14 |
|
@@ -214,37 +215,44 @@ def logout():
|
214 | 215 | return response
|
215 | 216 |
|
216 | 217 |
|
217 |
| -def _get_form_data(participant): |
| 218 | +def _get_form_data(participant: Participant): |
| 219 | + EventAlias = aliased(Event, flat=True) |
| 220 | + FormAlias = aliased(Form, flat=True) |
| 221 | + |
218 | 222 | # get incident forms
|
219 |
| - incident_forms = Form.query.join(Form.events).filter( |
220 |
| - Event.participant_set_id == participant.participant_set_id, |
| 223 | + incident_forms = Form.query.join(EventAlias, Form.events).filter( |
| 224 | + EventAlias.participant_set_id == participant.participant_set_id, |
221 | 225 | Form.form_type == 'INCIDENT',
|
222 | 226 | Form.is_hidden == False,
|
223 | 227 | ).with_entities(Form).order_by(Form.name, Form.id)
|
224 | 228 |
|
225 | 229 | # get participant submissions
|
226 |
| - participant_submissions = Submission.query.filter( |
227 |
| - Submission.participant_id == participant.id |
228 |
| - ).join(Submission.form) |
| 230 | + participant_submissions = Submission.query.join( |
| 231 | + EventAlias, |
| 232 | + and_( |
| 233 | + EventAlias.participant_set_id == participant.participant_set_id, |
| 234 | + Submission.event_id == EventAlias.id |
| 235 | + ) |
| 236 | + ).join(FormAlias, Submission.form_id == FormAlias.id) |
229 | 237 |
|
230 | 238 | # get checklist and survey forms based on the available submissions
|
231 | 239 | non_incident_forms = participant_submissions.with_entities(
|
232 |
| - Form).distinct(Form.id) |
| 240 | + FormAlias).distinct(FormAlias.id) |
233 | 241 | checklist_forms = non_incident_forms.filter(
|
234 |
| - Form.form_type == 'CHECKLIST', Form.is_hidden == False |
| 242 | + FormAlias.form_type == 'CHECKLIST', FormAlias.is_hidden == False |
235 | 243 | )
|
236 | 244 | survey_forms = non_incident_forms.filter(
|
237 |
| - Form.form_type == 'SURVEY', Form.is_hidden == False |
| 245 | + FormAlias.form_type == 'SURVEY', FormAlias.is_hidden == False |
238 | 246 | )
|
239 | 247 |
|
240 | 248 | # get form serial numbers
|
241 | 249 | form_ids_with_serials = participant_submissions.filter(
|
242 |
| - Form.form_type == 'SURVEY' |
| 250 | + FormAlias.form_type == 'SURVEY' |
243 | 251 | ).with_entities(
|
244 |
| - Form.id, Submission.serial_no |
| 252 | + FormAlias.id, Submission.serial_no |
245 | 253 | ).distinct(
|
246 |
| - Form.id, Submission.serial_no |
247 |
| - ).order_by(Form.id, Submission.serial_no) |
| 254 | + FormAlias.id, Submission.serial_no |
| 255 | + ).order_by(FormAlias.id, Submission.serial_no) |
248 | 256 |
|
249 | 257 | all_forms = checklist_forms.all() + incident_forms.all() + \
|
250 | 258 | survey_forms.all()
|
|
0 commit comments