From 697edaefa3126e5536728c5dccb309f3e3ee028e Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Thu, 29 Sep 2016 17:28:39 -0400 Subject: [PATCH] Implement Details in CapDocument --- TODO | 3 +-- capabilities/CapDocument.py | 39 +++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/TODO b/TODO index 5048c3d..83fee46 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ -* history (Detail) vs Bill? -* Bill._url? +* Bills vs Details? * Update modules? * amazon.com is buggy diff --git a/capabilities/CapDocument.py b/capabilities/CapDocument.py index e6d8431..6893c4f 100644 --- a/capabilities/CapDocument.py +++ b/capabilities/CapDocument.py @@ -6,16 +6,39 @@ def to_cozy(document): Export a CapDocument object to JSON, to pass it to Cozy instance. """ # Fetch the list of subscriptions - subscriptions = list(document.iter_subscription()) - # Return a formatted dict with all the infos - return { - "subscriptions": [ # List of subscriptions - clean_object(subscription) for subscription in subscriptions - ], - "bills": { # List of bills for each subscription + try: + subscriptions = list(document.iter_subscription()) + except NotImplementedError: + subscriptions = None + # Fetch and clean the list of bills + try: + assert(subscriptions) + bills = { subscription.id: [ - clean_object(bill) for bill in document.iter_bills(subscription) + clean_object(bill) for bill in document.iter_documents(subscription) ] for subscription in subscriptions } + except (NotImplementedError, AssertionError): + bills = None + # Fetch and clean the list of history bills (detailed consumption) + try: + assert(subscriptions) + detailed_bills = { + subscription.id: [ + clean_object(detailed_bill) + for detailed_bill in document.get_details(subscription) + ] + for subscription in subscriptions + } + except (NotImplementedError, AssertionError): + detailed_bills = None + # Return a formatted dict with all the infos + ret = { + "subscriptions": [ # Clean the subscriptions list + clean_object(subscription) for subscription in subscriptions + ], + "bills": bills, + "detailed_bills": detailed_bills } + return ret