-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move data stores to separate module and add helper methods
- Loading branch information
Showing
29 changed files
with
173 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Iterable, MutableMapping | ||
|
||
from ordered_set import OrderedSet | ||
|
||
from app.data_models.answer_store import AnswerStore | ||
from app.data_models.list_store import ListStore | ||
from app.data_models.metadata_proxy import MetadataProxy | ||
from app.data_models.progress_store import ProgressStore | ||
from app.data_models.supplementary_data_store import SupplementaryDataStore | ||
from app.questionnaire import ( | ||
Location, | ||
QuestionnaireSchema, | ||
placeholder_renderer, | ||
value_source_resolver, | ||
) | ||
from app.questionnaire.relationship_location import RelationshipLocation | ||
from app.questionnaire.rules import rule_evaluator | ||
from app.utilities.types import LocationType | ||
|
||
|
||
@dataclass | ||
class DataStores: | ||
# self.metadata is a read-only view over self._metadata | ||
metadata: MetadataProxy | None = None | ||
response_metadata: MutableMapping = field(default_factory=dict) | ||
list_store: ListStore = field(default_factory=ListStore) | ||
answer_store: AnswerStore = field(default_factory=AnswerStore) | ||
progress_store: ProgressStore = field(default_factory=ProgressStore) | ||
supplementary_data_store: SupplementaryDataStore = field( | ||
default_factory=SupplementaryDataStore | ||
) | ||
|
||
def rule_evaluator( | ||
self, | ||
*, | ||
schema: QuestionnaireSchema, | ||
location: Location, | ||
routing_path_block_ids: Iterable[str] | None = None, | ||
) -> rule_evaluator.RuleEvaluator: | ||
return rule_evaluator.RuleEvaluator( | ||
schema=schema, | ||
answer_store=self.answer_store, | ||
list_store=self.list_store, | ||
metadata=self.metadata, | ||
response_metadata=self.response_metadata, | ||
progress_store=self.progress_store, | ||
location=location, | ||
routing_path_block_ids=routing_path_block_ids, | ||
supplementary_data_store=self.supplementary_data_store, | ||
) | ||
|
||
def value_source_resolver( | ||
self, | ||
*, | ||
schema: QuestionnaireSchema, | ||
location: Location | RelationshipLocation | None, | ||
list_item_id: str | None, | ||
routing_path_block_ids: OrderedSet[str] | None = None, | ||
assess_routing_path: bool = True, | ||
use_default_answer: bool = True, | ||
escape_answer_values: bool = True, | ||
) -> value_source_resolver.ValueSourceResolver: | ||
return value_source_resolver.ValueSourceResolver( | ||
answer_store=self.answer_store, | ||
list_store=self.list_store, | ||
metadata=self.metadata, | ||
schema=schema, | ||
location=location, | ||
list_item_id=list_item_id, | ||
escape_answer_values=escape_answer_values, | ||
response_metadata=self.response_metadata, | ||
use_default_answer=use_default_answer, | ||
assess_routing_path=assess_routing_path, | ||
routing_path_block_ids=routing_path_block_ids, | ||
progress_store=self.progress_store, | ||
supplementary_data_store=self.supplementary_data_store, | ||
) | ||
|
||
def placeholder_renderer( | ||
self, | ||
language: str, | ||
schema: QuestionnaireSchema, | ||
location: LocationType | None = None, | ||
placeholder_preview_mode: bool | None = False, | ||
) -> placeholder_renderer.PlaceholderRenderer: | ||
return placeholder_renderer.PlaceholderRenderer( | ||
language=language, | ||
answer_store=self.answer_store, | ||
list_store=self.list_store, | ||
metadata=self.metadata, | ||
response_metadata=self.response_metadata, | ||
schema=schema, | ||
progress_store=self.progress_store, | ||
supplementary_data_store=self.supplementary_data_store, | ||
location=location, | ||
placeholder_preview_mode=placeholder_preview_mode, | ||
) | ||
|
||
def set_supplementary_store( | ||
self, supplementary_data: MutableMapping, list_mappings: dict | ||
) -> None: | ||
self.supplementary_data_store = SupplementaryDataStore( | ||
supplementary_data=supplementary_data, list_mappings=list_mappings | ||
) | ||
|
||
def deserialize_stores(self, json_data: dict, metadata: MutableMapping) -> None: | ||
self.metadata = self.metadata_from_dict(metadata=metadata) | ||
self.response_metadata = json_data.get("RESPONSE_METADATA", {}) | ||
# Type ignore: ListStore deserialize method only accepts Iterable[ListModelDictType] | ||
self.list_store = ListStore.deserialize(json_data.get("LISTS")) # type: ignore | ||
self.supplementary_data_store = SupplementaryDataStore.deserialize( | ||
json_data.get("SUPPLEMENTARY_DATA", {}) | ||
) | ||
self.progress_store = ProgressStore(json_data.get("PROGRESS")) | ||
self.answer_store = AnswerStore(json_data.get("ANSWERS")) | ||
|
||
def serialize_stores(self) -> dict: | ||
return { | ||
"ANSWERS": list(self.answer_store), | ||
"SUPPLEMENTARY_DATA": self.supplementary_data_store.serialize(), | ||
"LISTS": self.list_store.serialize(), | ||
"PROGRESS": self.progress_store.serialize(), | ||
"RESPONSE_METADATA": self.response_metadata, | ||
} | ||
|
||
@staticmethod | ||
def metadata_from_dict(metadata: MutableMapping) -> MetadataProxy: | ||
return MetadataProxy.from_dict(metadata) | ||
|
||
def delete(self) -> None: | ||
self.response_metadata = {} | ||
self.answer_store.clear() | ||
self.progress_store.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.