-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The updated code that streamlines some of the dictionaries
- Loading branch information
1 parent
f8c4125
commit 11a85a0
Showing
5 changed files
with
78 additions
and
78 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
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,40 @@ | ||
import logging | ||
|
||
import structlog | ||
from werkzeug.exceptions import BadRequest | ||
|
||
from ras_party.models import models | ||
|
||
logger = structlog.wrap_logger(logging.getLogger(__name__)) | ||
|
||
|
||
def get_respondents_associations(respondents): | ||
associations = [] | ||
for business_respondent in respondents: | ||
respondent_dict = { | ||
"partyId": business_respondent.respondent.party_uuid, | ||
"businessRespondentStatus": business_respondent.respondent.status.name, | ||
} | ||
enrolments = business_respondent.enrolment | ||
respondent_dict["enrolments"] = [] | ||
for enrolment in enrolments: | ||
enrolments_dict = { | ||
"surveyId": enrolment.survey_id, | ||
"enrolmentStatus": models.EnrolmentStatus(enrolment.status).name, | ||
} | ||
respondent_dict["enrolments"].append(enrolments_dict) | ||
associations.append(respondent_dict) | ||
return associations | ||
|
||
|
||
def get_attributes_for_collection_exercise(model_attributes, collection_exercise_id=None): | ||
if collection_exercise_id: | ||
for attributes in model_attributes.attributes: | ||
if attributes.collection_exercise == collection_exercise_id: | ||
return attributes | ||
|
||
try: | ||
return next((attributes for attributes in model_attributes.attributes if attributes.collection_exercise)) | ||
except StopIteration: | ||
logger.error("No active attributes for business", reference=model_attributes.business_ref, status=400) | ||
raise BadRequest("Business with reference does not have any active attributes.") |
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,18 @@ | ||
from ras_party.models import model_functions | ||
|
||
|
||
def to_unified_dict(model, collection_exercise_id=None, attributes_required=False): | ||
attributes = model_functions.get_attributes_for_collection_exercise(model, collection_exercise_id) | ||
unified_dict = { | ||
"id": model.party_uuid, | ||
"sampleUnitRef": model.business_ref, | ||
"sampleUnitType": model.UNIT_TYPE, | ||
"sampleSummaryId": attributes.sample_summary_id, | ||
"name": attributes.attributes.get("name"), | ||
"trading_as": attributes.attributes.get("trading_as"), | ||
"associations": model_functions.get_respondents_associations(model.respondents), | ||
} | ||
if attributes_required: | ||
unified_dict["attributes"] = attributes.attributes | ||
|
||
return unified_dict |