From 6cfeb9c12602f34c1f47fbb1aca074e821abf60f Mon Sep 17 00:00:00 2001 From: Quentin Defenouillere Date: Thu, 20 Aug 2020 17:29:22 +0200 Subject: [PATCH] [cragr] Repair life insurance investments & handled Rothschild invests --- modules/cragr/browser.py | 25 +++++++++++++++++-------- modules/cragr/pages.py | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/modules/cragr/browser.py b/modules/cragr/browser.py index 3bf2312003..97e682eada 100644 --- a/modules/cragr/browser.py +++ b/modules/cragr/browser.py @@ -397,6 +397,7 @@ def iter_accounts(self): accounts_list = list(self.page.iter_accounts()) for account in accounts_list: account._contract = contract + ''' Other accounts have no balance in the main JSON, so we must get all the (_id_element_contrat, balance) pairs in the account_details JSON. @@ -453,6 +454,7 @@ def iter_accounts(self): if account.id not in all_accounts: all_accounts[account.id] = account yield account + ''' Fetch all deferred credit cards for this space: from the space type we must determine the required URL parameters to build the cards URL. If there is no card on the space, the server will return a 500 error @@ -708,23 +710,29 @@ def iter_history(self, account, coming=False): @need_login def iter_investment(self, account): - if account.balance == 0: + if account.balance == 0 or empty(account.balance): return if ( account.type == Account.TYPE_LIFE_INSURANCE and ('rothschild' in account.label.lower() or re.match(r'^open (perspective|strat)', account.label, re.I)) ): + # We must go to the right perimeter before trying to access the Life Insurance investments + self.go_to_account_space(account._contract) self.life_insurance_investments.go(space=self.space, idx=account._index, category=account._category) - # TODO - # for inv in self.page.iter_investments(): - # yield inv + if self.life_insurance_investments.is_here(): + for inv in self.page.iter_investments(): + yield inv + else: + self.logger.warning('Failed to reach investment details for account %s', account.id) + return elif ( account.type in (Account.TYPE_LIFE_INSURANCE, Account.TYPE_CAPITALISATION) - and ('vendome' in account.label.lower() or account.label.lower() == 'espace gestion') + and re.search('vendome|aster sélection|espace gestion', account.label, re.I) ): - # 'Vendome Optimum Euro', 'Vendome Patrimoine' & 'Espace Gestion' and investments are on the BGPI space + # 'Vendome Optimum Euro', 'Vendome Patrimoine', 'Espace Gestion' & 'Aster sélection' + # investments are on the BGPI space if self.bgpi_accounts.is_here() or self.bgpi_investments.is_here(): # To avoid logouts by going from Cragr to Bgpi and back, we go directly to the account details. # When there are several BGPI accounts, this shortcut saves a lot of requests. @@ -780,10 +788,11 @@ def iter_investment(self, account): } try: self.predica_redirection.go(space=self.space, data=data) + self.predica_investments.go() except ServerError: - self.logger.warning('Got ServerError when fetching investments for account id %s', account.id) + self.logger.warning('Got ServerError when fetching investments for account %s', account.id) + return else: - self.predica_investments.go() for inv in self.page.iter_investments(): yield inv diff --git a/modules/cragr/pages.py b/modules/cragr/pages.py index 164ea085ea..07dff84ec9 100644 --- a/modules/cragr/pages.py +++ b/modules/cragr/pages.py @@ -712,8 +712,36 @@ def obj_code_type(self): class LifeInsuranceInvestmentsPage(LoggedPage, HTMLPage): - # TODO - pass + @method + class iter_investments(ListElement): + item_xpath = '//div[@id="menu1"]/div' + + class item(ItemElement): + klass = Investment + + obj_label = CleanText('.//div[has-class("PrivateBank-tabsNavContentTitle")]') + obj_valuation = CleanDecimal.French('.//div[contains(text(), "Valorisation")]/span') + obj_quantity = CleanDecimal.French( + './/div[contains(text(), "Nombre de parts")]/following-sibling::div[1]', + default=NotAvailable + ) + obj_unitvalue = CleanDecimal.French( + './/div[contains(text(), "Valeur de la part")]/following-sibling::div[1]', + default=NotAvailable + ) + obj_diff = CleanDecimal.French( + './/div[contains(text(), "+/- values")]/span', + default=NotAvailable + ) + + def obj_portfolio_share(self): + portfolio_share = CleanDecimal.French( + './/div[has-class("PrivateBank-tabsNavContentTitle")]/following-sibling::div/span', + default=NotAvailable + )(self) + if not empty(portfolio_share): + return Eval(lambda x: x / 100, portfolio_share)(self) + return NotAvailable class BgpiRedirectionPage(LoggedPage, HTMLPage): @@ -750,10 +778,12 @@ class item(ItemElement): './/span[@class="box"][span[span[text()="Nombre de part"]]]/span[2]/span' ) obj_unitvalue = CleanDecimal.French( - './/span[@class="box"][span[span[text()="Valeur liquidative"]]]/span[2]/span' + './/span[@class="box"][span[span[text()="Valeur liquidative"]]]/span[2]/span', + default=NotAvailable ) obj_unitprice = CleanDecimal.French( - './/span[@class="box"][span[span[text()="Prix de revient"]]]/span[2]/span', default=NotAvailable + './/span[@class="box"][span[span[text()="Prix de revient"]]]/span[2]/span', + default=NotAvailable ) obj_portfolio_share = Eval( lambda x: x / 100, @@ -763,7 +793,7 @@ class item(ItemElement): def obj_diff_ratio(self): # Euro funds have '-' instead of a diff_ratio value text = CleanText('.//span[@class="box"][span[span[text()="+/- value latente (%)"]]]/span[2]/span')(self) - if text == '-': + if text in ('', '-'): return NotAvailable return Eval( lambda x: x / 100, -- GitLab