Skip to content

Commit

Permalink
chore: Change report server contract (M2-7481,M2-7483) (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
vshvechko authored Oct 11, 2024
1 parent 92d601e commit 3b8cb7d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/apps/answers/crud/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/apps/answers/domain/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 18 additions & 15 deletions src/apps/answers/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
SummaryActivityFlow,
)
from apps.answers.domain.answers import (
Answer,
AnswersCopyCheckResult,
AppletSubmission,
FilesCopyCheckResult,
Expand Down Expand Up @@ -1949,7 +1950,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
Expand All @@ -1960,8 +1964,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)
Expand All @@ -1973,12 +1977,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,
Expand Down Expand Up @@ -2044,17 +2046,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:
Expand Down
2 changes: 1 addition & 1 deletion src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b8cb7d

Please sign in to comment.