diff --git a/src/apps/answers/crud/answers.py b/src/apps/answers/crud/answers.py index be3420cf66c..02785f1bb9b 100644 --- a/src/apps/answers/crud/answers.py +++ b/src/apps/answers/crud/answers.py @@ -100,6 +100,7 @@ async def get_list(self, **filters) -> list[Answer]: """ @param filters: see supported filters: _AnswerListFilter """ + self.session.expire_all() query = select(AnswerSchema).join(AnswerSchema.answer_item).options(contains_eager(AnswerSchema.answer_item)) _filters = _AnswerListFilter().get_clauses(**filters) diff --git a/src/apps/answers/domain/answers.py b/src/apps/answers/domain/answers.py index 741add8c950..0139168d4bb 100644 --- a/src/apps/answers/domain/answers.py +++ b/src/apps/answers/domain/answers.py @@ -41,6 +41,10 @@ class Answer(InternalModel): activity_history_id: str respondent_id: uuid.UUID | None is_flow_completed: bool | None = False + target_subject_id: uuid.UUID | None = None + source_subject_id: uuid.UUID | None = None + input_subject_id: uuid.UUID | None = None + relation: str | None = None migrated_data: dict | None = None created_at: datetime.datetime diff --git a/src/apps/answers/service.py b/src/apps/answers/service.py index 6f101c8e3d5..a3647f22b61 100644 --- a/src/apps/answers/service.py +++ b/src/apps/answers/service.py @@ -60,6 +60,7 @@ SummaryActivityFlow, ) from apps.answers.domain.answers import ( + Answer, AnswersCopyCheckResult, AppletSubmission, FilesCopyCheckResult, @@ -1924,7 +1925,10 @@ async def is_flow_finished(self, submit_id: uuid.UUID, answer_id: uuid.UUID) -> async def create_report( self, submit_id: uuid.UUID, answer_id: uuid.UUID | None = None ) -> ReportServerResponse | None: - answers = await AnswersCRUD(self.answers_session).get_by_submit_id(submit_id, answer_id) + filters = dict(submit_id=submit_id) + if answer_id: + filters.update(answer_id=answer_id) + answers = await AnswersCRUD(self.answers_session).get_list(**filters) if not answers: return None applet_id_version: str = answers[0].applet_history_id @@ -1935,8 +1939,8 @@ async def create_report( # If answers only on performance tasks if not answers_for_report: return None - answer_map = dict((answer.id, answer) for answer in answers_for_report) initial_answer = answers_for_report[0] + assert initial_answer.target_subject_id applet = await AppletsCRUD(self.session).get_by_id(initial_answer.applet_id) user_info = await self._get_user_info(initial_answer.target_subject_id) @@ -1948,12 +1952,10 @@ async def create_report( ) encryption = ReportServerEncryption(applet.report_public_key) - responses, user_public_keys = await self._prepare_responses(answer_map) + responses = await self._prepare_responses(answers_for_report) data = dict( responses=responses, - userPublicKeys=user_public_keys, - userPublicKey=user_public_keys[0], now=datetime.datetime.utcnow().strftime("%x"), user=user_info, applet=applet_full, @@ -2019,17 +2021,18 @@ async def _get_user_info(self, subject_id: uuid.UUID): tag=subject.tag, ) - async def _prepare_responses(self, answers_map: dict[uuid.UUID, AnswerSchema]) -> tuple[list[dict], list[str]]: - answer_items = await AnswerItemsCRUD(self.answers_session).get_respondent_submits_by_answer_ids( - list(answers_map.keys()) - ) - + async def _prepare_responses(self, answers: list[Answer]) -> list[dict]: responses = list() - for answer_item in answer_items: - answer = answers_map[answer_item.answer_id] - activity_id, version = answer.activity_history_id.split("_") - responses.append(dict(activityId=activity_id, answer=answer_item.answer)) - return responses, [ai.user_public_key for ai in answer_items] + for answer in answers: + activity_id = HistoryAware().id_from_history_id(answer.activity_history_id) + responses.append( + dict( + activityId=activity_id, + answer=answer.answer_item.answer, + userPublicKey=answer.answer_item.user_public_key, + ) + ) + return responses class ReportServerEncryption: diff --git a/src/cli.py b/src/cli.py index 16b9753b983..08333ddc7c6 100755 --- a/src/cli.py +++ b/src/cli.py @@ -5,7 +5,7 @@ os.chdir(dname) -import typer # noqa: E402 +import typer # noqa: E402,I001 from apps.activities.commands import activities # noqa: E402 from apps.answers.commands import convert_assessments # noqa: E402