def decode_response(self, responses): statement = [] for seg in responses: # Note: MT940 messages are encoded in the S.W.I.F.T character set, # which is a subset of ISO 8859. There are no character in it that # differ between ISO 8859 variants, so we'll arbitrarily chose 8859-1. try: statement += mt940_to_array( seg.statement_booked.decode('iso-8859-1')) except Exception as e: self.with_error.append((seg.statement_booked.decode('iso-8859-1'), str(e))) return statement
def get_filtered_transactions(self, account: SEPAAccount, start_date: datetime.date = None, end_date: datetime.date = None): """ Fetches the list of transactions of a bank account in a certain timeframe. MT940-Errors are catched and the statements containing them returned as a seperate list. :param account: SEPA :param start_date: First day to fetch :param end_date: Last day to fetch :return: A tuple with list of mt940.models.Transaction objects and another list with tuples of mt940-data and error messages. """ with self._get_dialog() as dialog: hkkaz = self._find_highest_supported_command( HKKAZ5, HKKAZ6, HKKAZ7) logger.info('Start fetching from {} to {}'.format( start_date, end_date)) responses = self._fetch_with_touchdowns( dialog, lambda touchdown: hkkaz( account=hkkaz._fields['account'].type.from_sepa_account( account), all_accounts=False, date_start=start_date, date_end=end_date, touchdown_point=touchdown, ), 'HIKAZ') logger.info('Fetching done.') statement = [] with_error = [] for seg in responses: # Note: MT940 messages are encoded in the S.W.I.F.T character set, # which is a subset of ISO 8859. There are no character in it that # differ between ISO 8859 variants, so we'll arbitrarily chose 8859-1. try: statement += mt940_to_array( seg.statement_booked.decode('iso-8859-1')) except Exception as e: with_error.append( (seg.statement_booked.decode('iso-8859-1'), str(e))) logger.debug('Statement: {}'.format(statement)) return statement, with_error
def fix_import_error(error_id): error = MT940Error.q.get(error_id) form = FixMT940Form() (transactions, old_transactions, doubtful_transactions) = ([], [], []) new_exception = None if request.method != 'POST': form.mt940.data = error.mt940 if form.validate_on_submit(): statement = [] try: statement += mt940_to_array(form.mt940.data) except Exception as e: new_exception = str(e) if new_exception is None: flash('MT940 ist jetzt valide.', 'success') (transactions, old_transactions, doubtful_transactions) = finance.process_transactions( error.bank_account, statement) if form.do_import.data is True: # save transactions to database session.add_all(transactions) session.delete(error) session.commit() flash(u'Bankkontobewegungen wurden importiert.') return redirect(url_for(".bank_accounts_import_errors")) else: flash('Es existieren weiterhin Fehler.', 'error') return render_template('finance/bank_accounts_error_fix.html', error_id=error_id, exception=error.exception, new_exception=new_exception, form=form, transactions=transactions, old_transactions=old_transactions, doubtful_transactions=doubtful_transactions)