Last active
August 28, 2019 14:48
-
-
Save fredkingham/d4c6c569410a38b89614966b334311f0 to your computer and use it in GitHub Desktop.
parse submissions
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
import xmltodict | |
import datetime | |
from odonto.odonto_submissions import models | |
def translate(response): | |
response_text = response.content.strip()[2:][:-1] | |
response_text = response_text.replace( | |
"\\n", "").replace("\\t", "").replace("\\r", "" | |
) | |
return xmltodict.parse(response_text) | |
def parsed_message_ids(response): | |
return [int(i["@seq"]) for i in response["icset"]["ic"]["contrl"]] | |
def get_episode_from_submission(submission_id): | |
subs = models.Submission.objects.filter(claim__reference_number=submission_id) | |
if subs: | |
return subs.first().episode_id | |
def failed_episode_ids(response, old=True): | |
""" | |
We used to use the system claim number as the id for the | |
episode submission that we sent down. We now use episode id | |
this changes 28 August 2019 | |
""" | |
result = {} | |
for i in response["icset"]["ic"]["respce"]["rsp"]: | |
print("--" * 10) | |
print(i["@clrn"]) | |
print(i["mstxt"]) | |
if old: | |
episode_id = get_episode_from_submission(int(i["@clrn"])) | |
if not episode_id: | |
print("unable to find {}".format(episode_id)) | |
continue | |
else: | |
episode_id = int(i["@clrn"]) | |
if isinstance(i["mstxt"], list): | |
result[episode_id] = [y["#text"] for y in i["mstxt"]] | |
else: | |
result[episode_id] = [i["mstxt"]["#text"]] | |
return result | |
def get_submission_status(response): | |
old = False | |
if response.created.date() < datetime.date(2019, 8, 28): | |
old = True | |
response_json = translate(response) | |
submissions = models.Submission.objects.filter( | |
claim_id__in=parsed_message_ids(response_json) | |
) | |
failed_episodes = failed_episode_ids(response_json, old=old) | |
failed_submissions = submissions.filter(episode_id__in=failed_episodes) | |
failed = {} | |
for failed_episode_id, err_msgs in failed_episodes.items(): | |
sub = submissions.filter(episode_id=failed_episode_id).first() | |
if not sub: | |
print("unable to find {}".format(failed_episode_id)) | |
continue | |
failed[sub] = err_msgs | |
successes = submissions.exclude(episode_id__in=failed_episodes.keys()) | |
return successes, failed_submissions | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment