class Boobpeg: """ Class that performs requests using weboob capabitilities """ def __init__(self): logger.debug("class init") self.peg_perco_names = [] self.weboob = Weboob() self.weboob.load_backends(CapBank) logger.debug('backends loaded: %s' % list(self.weboob.iter_backends())) self.investments = {} def set_peg_perco_names(self, peg_perco_names): logger.debug("set_peg_perco_names(): peg_perco_names: %s" % peg_perco_names) self.peg_perco_names = peg_perco_names def retrieve_investments(self, account): logger.debug("retrieve_investments(): account: %s" % account) account_investments = {} for investment in list(self.weboob.iter_investment(account)): account_investments[investment.code] = str(investment.unitvalue) logger.debug("retrieve_investments(): return: %s" % account_investments) return account_investments def update_investments(self): logger.debug("update_investments()") accounts = list(self.weboob.iter_accounts()) logger.debug("update_investments(): accounts: %s" % accounts) for account in accounts: if account.label in self.peg_perco_names: logger.debug('update_investments(): account "%s" is in the ' 'account list to check' % account.label) self.investments.update(self.retrieve_investments(account)) else: logger.debug('update_investments(): account "%s" is NOT in the' ' account list to check' % account.label) logger.debug("update_investments(): return: %s" % self.investments) return self.investments
def bank_create_with_weboob_module(request, pk): w = Weboob() if request.method == 'POST': form = BankForm(data=request.POST) if form.is_valid(): bank = form.save(commit=False) bank.bank_password = make_password( form.cleaned_data['bank_password']) bank.save() w.load_backend( bank.module_weboob.name_of_module, bank.name_of_bank, { 'login': form.cleaned_data['bank_login'], 'password': form.cleaned_data['bank_password'] }) # Get list of available account(s) in the bank list_of_accounts = list(w.iter_accounts()) # Get list of transactions coming from bank history context = load_transactions(request, w, bank, list_of_accounts) return render(request, 'ManageGesfi/load_transactions_from_account.html', context) else: form = BankForm() context = { 'form': form, 'create': True # TODO: ajouter un flag "create with weboob" pour rediriger vers load_transactions (et créer les comptes) } return render(request, 'banksandaccounts/bank_edit.html', context)
def load_transactions(request): ''' Function to download transactions (or any related information) from banks through backends managed by Weboob And load them in the database managed by Gesfi :param request: :return: render: to render the list of transactions got from accounts managed by Weboob Backends ''' w = Weboob() # Check if repositories where à located Backends are up to date check_weboob_repositories(w) # List of Banks in Backends with Weboob (for which we have te capability to connect with to get information) listbanks = w.load_backends(CapBank) # List of accounts got from Banks list_of_accounts = list(w.iter_accounts()) # List of accounts in DataBase manage by Gesfipe db_accounts_list = Accounts.objects.all().filter(owner_of_account=request.user) print(db_accounts_list) # list_trans = Transactions.objects.all() list_uniques = list_unique_of_numbers() # TODO: Vérifier d'abord si la liste chargée existe déjà dans la base de données (via comparaison avec unique_id_of_transaction) list_of_transactions = [] for real_account in list_of_accounts: # print(real_account) for db_account in db_accounts_list: if real_account.id == db_account.num_of_account: logger.warning("------------------------------------") logger.warning("real_account.id = {} ****** db_account.num_of_account = {}".format(real_account.id, db_account.num_of_account)) logger.warning("Type of account = {}".format(db_account.type_int_of_account)) logger.warning("------------------------------------") # TODO: Injecter la dernière date en base de donnée dans w.iter_history(real_account, date) afin de limiter la vérification transactions_of_banks_account = w.iter_history(real_account) for transaction in transactions_of_banks_account: # print(transaction) transac = {} Trans = Transactions() Trans.account = db_account # print(Trans.account) transac['date'] = transaction.date # Debit date on the bank statement Trans.date_of_transaction = transaction.date # print(Trans.date_of_transaction) # Real date, when the payment has been made; usually extracted from the label or from credit card info if transaction.rdate: transac['rdate'] = transaction.rdate Trans.real_date_of_transaction = transaction.rdate else: transac['rdate'] = transaction.date Trans.real_date_of_transaction = transaction.date # print(Trans.real_date_of_transaction) # Value date, or accounting date; usually for professional accounts if transaction.vdate: transac['vdate'] = transaction.vdate Trans.value_date_of_transaction = transaction.vdate else: transac['vdate'] = transaction.rdate Trans.value_date_of_transaction = transaction.rdate # print(Trans.value_date_of_transaction) transac[ 'type'] = transaction.type # Type of transaction, use TYPE_* constants', default=TYPE_UNKNOWN Trans.type_int_of_transaction = transaction.type # Trans.type_of_transaction = transaction.type # print(Trans.type_int_of_transaction) transac['raw'] = transaction.raw # Raw label of the transaction Trans.name_of_transaction = transaction.raw # print(Trans.name_of_transaction) transac['category'] = transaction.category # Category of the transaction Trans.type_of_transaction = transaction.category # logger.warning('transaction.category ==> ==> ==> : %s', transaction.category) # Trans.category_of_transaction = transaction.category # print(Trans.type_of_transaction) transac['label'] = transaction.label # Pretty label Trans.label_of_transaction = transaction.label # print(Trans.label_of_transaction) transac['amount'] = transaction.amount # Amount of the transaction Trans.amount_of_transaction = transaction.amount # print(Trans.amount_of_transaction) Trans.create_key_words() # print(Trans.key_words) Trans.unique_id_of_transaction = Trans.unique_id(account_id=db_account.num_of_account) # print(Trans.unique_id_of_transaction) if Trans.unique_id_of_transaction not in list_uniques: Trans.save() list_uniques.append(Trans.unique_id_of_transaction) list_of_transactions.append(transac) # print('Sauvegarde de Trans: ===>>>>>> {}\n'.format(Trans)) logger.warning('Sauvegarde de Trans: ===>>>>>> %s', Trans) context = {'list_of_accounts': list_of_accounts, 'list_of_transactions': list_of_transactions, } return render(request, 'ManageGesfi/load_transactions_from_account.html', context)
def get_list_of_available_accounts(request): ''' Function to get list of accounts of bank with local backend (cf. ~/.config/weboob/backends :param request: :return: render: to render list_of_banks and list_of_accounts (but banks and account are not linked (to be updated) ''' logger.warning("Entering in function get_list_of_available_accounts") w = Weboob() check_weboob_repositories(w) listbanks = w.load_backends(CapBank) list_of_banks = [] list_of_db_accounts = [] # db_accounts_list = db_Accounts.objects.all().filter(owner_of_account=request.user) db_accounts_list = db_Accounts.objects.all() for key in db_accounts_list: # print('db_accounts_list::::::: {}'.format(key.num_of_account)) list_of_db_accounts.append(key.num_of_account) for key, val in listbanks.items(): # print("Banque key (Dict) : {} \t\t\t ===> \t {}".format(key, val.description)) data_bank = {} data_bank['module'] = key data_bank['name'] = val.DESCRIPTION list_of_banks.append(data_bank) list_of_banks.sort(key=lambda k: k['module']) # print('************************ : {}'.format(list_of_banks)) logger.warning('list_of_banks ==> ==> ==> : %s', list_of_banks) # TODO: cela fonctionne car je n'ai qu'une banque en backends mais sinon la liste des "accounts" sera globale. # TODO: il manque donc l'association account et banque list_of_accounts = list(w.iter_accounts()) for real_account in list_of_accounts: # print('REAL ACCOUNT: {}'.format(real_account)) # print('LIST_OF_DB_ACCOUNTS: {}'.format(list_of_db_accounts)) if real_account.id in list_of_db_accounts: # print('if real_account.id in list_of_db_accounts: ==>> REAL ACCOUNT.ID: {}'.format(real_account.id)) # print('if real_account.id in list_of_db_accounts: ==>> LIST_OF_DB_ACCOUNTS: {}'.format(list_of_db_accounts)) # erase old data and replace by data coming from bank db_account = db_Accounts.objects.get(num_of_account=real_account.id) # print('if real_account.id == db_account.num_of_account en base de données: {} -- à la banque: {}'.format(db_account.name_of_account, real_account.label)) db_account.name_of_account = real_account.label db_account.type_int_of_account = real_account.type db_account.balance_of_account = real_account.balance # TODO: ajouter le nom de la banque au compte # TODO: ajouter le user en cours # db_account.bank = real_account.parent.name # u = User.objects.get(name=request.user.name) # u.db_accounts_set.add(db_account) # db_account.owner_of_account.set([request.user,]) db_account.save() else: # print('Else: ==>> REAL ACCOUNT.ID: {}'.format(real_account.id)) # print('Else: ===> LIST_OF_DB_ACCOUNTS: {}'.format(list_of_db_accounts)) # create account new_account = db_Accounts() new_account.num_of_account = real_account.id new_account.name_of_account = real_account.label new_account.type_int_of_account = real_account.type new_account.balance_of_account = real_account.balance # TODO: affecter un user + le type (en texte) # new_account.owner_of_account = request.user # print('new_account.name_of_account = {}'.format(new_account.name_of_account)) new_account.save() # new_account.owner_of_account.set(request.user) # new_account.create() # list_of_accounts.sort(key=lambda k: k['label']) context = {'list_of_banks': list_of_banks, 'list_of_accounts': list_of_accounts, } logger.warning("Entering in function get_list_of_available_accounts") return render(request, 'ManageGesfi/list_of_available_accounts.html', context)
print("Repertoires à jour \n\n") # w.repositories.update() w.load_backends(CapBank) # capApplicationDict = w.init_CapApplicationDict() # for cle in capApplicationDict: # print(cle) # applications = capApplicationDict[cap] # applications = capApplicationDict['CapBank'] # print('\n ==========> \n {}\n <==========\n'.format(application)) print('\n *************************\n {} \n *************************** \n'. format(list(w.iter_accounts()))) acc = next(iter(w.iter_accounts())) bal = acc.balance print('\n ********** \n {}\n **********\n'.format(acc)) print('\n ********** \n {}\n **********\n'.format(bal)) print('workdir : {}'.format(w.workdir)) print('repositories : {} \n'.format(w.repositories.modules_dir)) # print('get_all_modules_info :\n {} \n'.format(w.repositories.get_all_modules_info(CapBank))) listbanks = w.repositories.get_all_modules_info(CapBank) print( "========================================================================================================" )