def custom_exception_handler(err, context): """ Custom exception handler for Django Rest Framework. https://www.django-rest-framework.org/api-guide/exceptions/ """ IGNORE_ERRORS = (NotAuthenticated,) if isinstance(err, IGNORE_ERRORS): log.warning(err) return Response({'detail':str(err)}) log.exception(err) return Response({'detail':str(err)})
def _build_chunks(self): try: chunkstrs = shlex.split(self.searchstr) for chunk in [c for c in chunkstrs if c in STOPWORDS]: self.errors.append('Part of the search is being ignored: %s' % chunk) return [ SearchChunk(self, c) for c in chunkstrs if c not in STOPWORDS ] except Exception as err: self.errors.append('Invalid query: %s' % err) log.exception(err)
def queryset(self): try: queryset = self.search.basequeryset.all() if self.error: return queryset elif not self.field: return queryset & self._queryset_generic() elif isinstance(self.qvalue, datetime.datetime): return queryset & self._queryset_datetime() return queryset & self._queryset_advanced() except Exception as err: log.exception(err)
def import_qfx(self, user, filename, handle): """ Import transactions from a qfx file. """ try: self.files += 1 transactions = [] log.info('Importing transactions qfx file: %s' % filename) qfx = OfxParser.parse(handle) fid = int(qfx.account.institution.fid) if fid not in self._accounts: raise Exception('Not tracking account fid: %s' % fid) account = self._accounts[fid] # Update transactions for trx in qfx.account.statement.transactions: trx = trx.__dict__ trx['trxid'] = trx['id'] trx['accountfid'] = fid if not self._transaction_exists(trx, addit=True): transactions.append( Transaction( user=user, account_id=account.id, trxid=trx['id'], payee=trx[account.payee or 'payee'], amount=trx['amount'], date=trx['date'].date(), # original values original_date=trx['date'].date(), original_payee=trx[account.payee or 'payee'], original_amount=trx['amount'], )) self.label_transactions(transactions) self.categorize_transactions(transactions) log.info('Saving %s new transactions from qfx file: %s' % (len(transactions), filename)) Transaction.objects.bulk_create(transactions) self.status.append('%s: added %s transactions' % (filename, len(transactions))) self.transactions += len(transactions) # Update account balance statementdt = timezone.make_aware(qfx.account.statement.end_date) if account.balancedt is None or statementdt > account.balancedt: account.balance = qfx.account.statement.balance account.balancedt = statementdt account.save() except Exception as err: log.exception(err) self.status.append('Error %s: %s' % (filename, err))
def get_album(): try: return PhotosFrom500px().get_photos() except Exception as err: log.exception(err)