Example #1
0
    def handle_uploaded_file(self, f):
        csv_buffer = StringIO(f.read().decode())
        reader = csv.reader(csv_buffer)
        header = next(reader, None)
        parser = ParserFactory(header).create()
        """
        for row in reader:
            count += 1

            if count == 1:
                # TODO: Use CSV Sniffer
                header = row
                for header_type, headers in ParserFactory.HEADERS.items():
                    first_header = header[0].replace('\ufeff', '').replace('"', '')
                    if first_header == headers[0]:
                        parsing = header_type
                        break
                if parsing:
                    continue

            parser = ParserFactory().get(parsing)
            if not parsing or not parser:
                break

            parser.parse(row)
        """
        for row in reader:
            parser.parse(row)
        Expense.assign_categories()
Example #2
0
    def test_assign_categories(self):
        for i in range(0, 3):
            expense = Expense.objects.create(
                description='Spotify {}'.format(i + 1),
                payment=constants.CREDIT_CARD,
                amount=Decimal('9.99'),
                date=datetime.now(),
            )
            self.assertIsNone(expense.category)

        Expense.assign_categories()

        for expense in Expense.objects.all():
            self.assertEqual(expense.category.name, 'Entertainment')
Example #3
0
    def test_assign_categories(self):
        for i in range(0, 3):
            expense = Expense.objects.create(
                description='Spotify {}'.format(i + 1),
                payment=constants.CREDIT_CARD,
                amount=Decimal('9.99'),
                date=datetime.now(),
            )
            self.assertIsNone(expense.category)

        Expense.assign_categories()

        for expense in Expense.objects.all():
            self.assertEqual(expense.category.name, 'Entertainment')
Example #4
0
    def load_expenses(self):
        self.download_file()
        with open(self.output_path, 'r') as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
                if len(row) < 7:
                    continue
                try:
                    transaction_date = datetime.strptime(row[0], '%Y-%m-%d')
                except ValueError:
                    continue
                amount = Decimal(row[1]) * Decimal(-1)
                description = row[7].title()
                if Expense.objects.filter(description=description, date=transaction_date, amount=amount).count():
                    continue

                Expense.objects.create(
                    description=description,
                    payment=constants.CASH,
                    amount=amount,
                    date=transaction_date,
                )
        Expense.assign_categories()