module.py 6.04 KB
Newer Older
1
# -*- coding: utf-8 -*-
2

Romain Bignon's avatar
Romain Bignon committed
3
# Copyright(C) 2010-2011 Nicolas Duhamel
4
#
5
# This file is part of a weboob module.
6
#
7
# This weboob module is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
Romain Bignon's avatar
Romain Bignon committed
9 10 11
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
12
# This weboob module is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Romain Bignon's avatar
Romain Bignon committed
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
19

20

21
from decimal import Decimal
Célande Adrien's avatar
Célande Adrien committed
22
from datetime import timedelta
Sylvie Ye's avatar
Sylvie Ye committed
23
from weboob.capabilities.bank import CapBankWealth, CapBankTransferAddRecipient, Account, AccountNotFound, RecipientNotFound
Edouard Lambert's avatar
Edouard Lambert committed
24
from weboob.capabilities.contact import CapContact
25
from weboob.capabilities.base import find_object, strict_find_object, NotAvailable
26
from weboob.capabilities.profile import CapProfile
27 28 29 30
from weboob.capabilities.bill import (
    CapDocument, Subscription, SubscriptionNotFound,
    Document, DocumentNotFound,
)
31
from weboob.tools.backend import Module, BackendConfig
32
from weboob.tools.value import ValueBackendPassword, Value
33

34
from .browser import BPBrowser, BProBrowser
35

36

Florent's avatar
Florent committed
37
__all__ = ['BPModule']
38 39


40 41 42 43 44
class BPModule(
    Module, CapBankWealth, CapBankTransferAddRecipient,
    CapContact, CapProfile, CapDocument,
):

45
    NAME = 'bp'
46
    MAINTAINER = u'Nicolas Duhamel'
47
    EMAIL = '[email protected]'
Romain Bignon's avatar
Romain Bignon committed
48
    VERSION = '1.6'
49
    LICENSE = 'LGPLv3+'
50 51
    DESCRIPTION = u'La Banque Postale'
    CONFIG = BackendConfig(ValueBackendPassword('login',    label='Identifiant', masked=False),
52
                           ValueBackendPassword('password', label='Mot de passe', regexp='^(\d{6})$'),
53 54
                           Value('website', label='Type de compte', default='par',
                                 choices={'par': 'Particuliers', 'pro': 'Professionnels'}))
55 56

    def create_default_browser(self):
57
        b = {'par': BPBrowser, 'pro': BProBrowser}
Jean Walrave's avatar
Jean Walrave committed
58

59 60
        self.BROWSER = b[self.config['website'].get()]

61
        return self.create_browser(self.config['login'].get(), self.config['password'].get(), weboob=self.weboob)
62 63

    def iter_accounts(self):
Romain Bignon's avatar
Romain Bignon committed
64
        return self.browser.get_accounts_list()
65 66

    def get_account(self, _id):
Baptiste Delpey's avatar
Baptiste Delpey committed
67
        return find_object(self.browser.get_accounts_list(), id=_id, error=AccountNotFound)
68 69

    def iter_history(self, account):
70
        return self.browser.get_history(account)
Romain Bignon's avatar
Romain Bignon committed
71 72

    def iter_coming(self, account):
73
        return self.browser.get_coming(account)
74

75 76 77
    def iter_investment(self, account):
        return self.browser.iter_investment(account)

78 79 80 81 82 83 84 85 86 87 88 89
    def iter_transfer_recipients(self, origin_account):
        if self.config['website'].get() != 'par':
            raise NotImplementedError()
        if isinstance(origin_account, Account):
            origin_account = origin_account.id
        return self.browser.iter_recipients(origin_account)

    def init_transfer(self, transfer, **params):
        if self.config['website'].get() != 'par':
            raise NotImplementedError()

        self.logger.info('Going to do a new transfer')
90 91 92 93 94 95 96
        account = strict_find_object(self.iter_accounts(), iban=transfer.account_iban)
        if not account:
            account = strict_find_object(self.iter_accounts(), id=transfer.account_id, error=AccountNotFound)

        recipient = strict_find_object(self.iter_transfer_recipients(account.id), iban=transfer.recipient_iban)
        if not recipient:
            recipient = strict_find_object(self.iter_transfer_recipients(account.id), id=transfer.recipient_id, error=RecipientNotFound)
97

Sylvie Ye's avatar
Sylvie Ye committed
98
        amount = Decimal(transfer.amount).quantize(Decimal(10) ** -2)
99

Sylvie Ye's avatar
Sylvie Ye committed
100 101 102
        # format label like label sent by firefox or chromium browser
        transfer.label = transfer.label.encode('latin-1', errors="xmlcharrefreplace").decode('latin-1')

103
        return self.browser.init_transfer(account, recipient, amount, transfer)
104

Sylvie Ye's avatar
Sylvie Ye committed
105 106 107 108
    def transfer_check_label(self, old, new):
        old = old.encode('latin-1', errors="xmlcharrefreplace").decode('latin-1')
        return super(BPModule, self).transfer_check_label(old, new)

Célande Adrien's avatar
Célande Adrien committed
109 110 111
    def transfer_check_date(self, old_exec_date, new_exec_date):
        return old_exec_date <= new_exec_date <= old_exec_date + timedelta(days=2)

112 113
    def execute_transfer(self, transfer, **params):
        return self.browser.execute_transfer(transfer)
Edouard Lambert's avatar
Edouard Lambert committed
114

Baptiste Delpey's avatar
Baptiste Delpey committed
115 116 117
    def new_recipient(self, recipient, **kwargs):
        return self.browser.new_recipient(recipient, **kwargs)

Edouard Lambert's avatar
Edouard Lambert committed
118 119 120 121 122
    def iter_contacts(self):
        if self.config['website'].get() != 'par':
            raise NotImplementedError()

        return self.browser.get_advisor()
123 124 125

    def get_profile(self):
        return self.browser.get_profile()
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

    def get_document(self, _id):
        subscription_id = _id.split('_')[0]
        subscription = self.get_subscription(subscription_id)
        return find_object(self.iter_documents(subscription), id=_id, error=DocumentNotFound)

    def get_subscription(self, _id):
        return find_object(self.iter_subscription(), id=_id, error=SubscriptionNotFound)

    def iter_documents(self, subscription):
        if not isinstance(subscription, Subscription):
            subscription = self.get_subscription(subscription)

        return self.browser.iter_documents(subscription)

    def iter_subscription(self):
        return self.browser.iter_subscriptions()

    def download_document(self, document):
        if not isinstance(document, Document):
            document = self.get_document(document)
        if document.url is NotAvailable:
            return

        return self.browser.download_document(document)

    def iter_resources(self, objs, split_path):
        if Account in objs:
            self._restrict_level(split_path)
            return self.iter_accounts()
        if Subscription in objs:
            self._restrict_level(split_path)
            return self.iter_subscription()