Skip to content

Commit

Permalink
Merge pull request agstrike#124 from agstrike/import-changes
Browse files Browse the repository at this point in the history
Import changes
  • Loading branch information
simhnna authored Jan 7, 2021
2 parents 1708070 + b654139 commit 78b4dad
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 48 deletions.
17 changes: 11 additions & 6 deletions .github/workflows/test_and_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.5, 3.6, 3.7]
tox: [django2.0, django2.1, django2.2, flake, codecov]
python: [3.6, 3.7, 3.8, 3.9]
tox: [django3.0, django3.1, flake, codecov]
exclude:
- python: 3.5
- python: 3.6
tox: flake
- python: 3.5
- python: 3.6
tox: codecov
- python: 3.5
- python: 3.7
tox: flake
- python: 3.6
- python: 3.7
tox: codecov
- python: 3.8
tox: flake
- python: 3.8
tox: codecov

steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python }}
Expand Down
2 changes: 1 addition & 1 deletion silverstrike/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def get_accounts(request, account_type):
accounts = Account.objects.exclude(account_type=Account.AccountType.SYSTEM)
if account_type != 'all':
account_type = getattr(Account, account_type)
account_type = getattr(Account.AccountType, account_type)
accounts = accounts.filter(account_type=account_type)

return JsonResponse(list(accounts.values_list('name', flat=True)), safe=False)
Expand Down
13 changes: 8 additions & 5 deletions silverstrike/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class ImportUploadForm(forms.ModelForm):
class Meta:
model = models.ImportFile
fields = ['file']
fields = ['file', 'account', 'importer']
account = forms.ModelChoiceField(queryset=models.Account.objects.personal().active())
importer = forms.ChoiceField(choices=enumerate(importers.IMPORTER_NAMES))

Expand All @@ -20,7 +20,8 @@ class Meta:

def clean_name(self):
name = self.cleaned_data['name']
if models.Account.objects.filter(name=name, account_type=models.Account.AccountType.FOREIGN).exists():
if models.Account.objects.filter(name=name,
account_type=models.Account.AccountType.FOREIGN).exists():
raise ValidationError(_('An account with this name already exists'))
return name

Expand All @@ -34,7 +35,8 @@ class Meta:

def clean_name(self):
name = self.cleaned_data['name']
if models.Account.objects.filter(name=name, account_type=models.Account.AccountType.PERSONAL).exists():
if models.Account.objects.filter(name=name,
account_type=models.Account.AccountType.PERSONAL).exists():
raise ValidationError(_('An account with this name already exists'))
return name

Expand Down Expand Up @@ -159,8 +161,9 @@ class DepositForm(TransactionForm):
widget=forms.TextInput(attrs={'autocomplete': 'off'}))

def clean_src(self):
account, _ = models.Account.objects.get_or_create(name=self.cleaned_data['src'],
account_type=models.Account.AccountType.FOREIGN)
account, _ = models.Account.objects.get_or_create(
name=self.cleaned_data['src'],
account_type=models.Account.AccountType.FOREIGN)
return account

def clean(self):
Expand Down
20 changes: 12 additions & 8 deletions silverstrike/importers/firefly.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def import_firefly(csv_path):
notes = 'notes'
transaction_type = 'transaction_type'

system_account, _ = models.Account.objects.get_or_create(account_type=models.Account.AccountType.SYSTEM,
defaults={'name': 'System Account'})
system_account, _ = models.Account.objects.get_or_create(
account_type=models.Account.AccountType.SYSTEM,
defaults={'name': 'System Account'})

personal_accounts = dict()
foreign_accounts = dict()
Expand Down Expand Up @@ -59,8 +60,9 @@ def import_firefly(csv_path):
if line[destination] in foreign_accounts:
line[destination] = foreign_accounts[line[destination]]
else:
a = models.Account.objects.create(name=line[destination],
account_type=models.Account.AccountType.FOREIGN)
a = models.Account.objects.create(
name=line[destination],
account_type=models.Account.AccountType.FOREIGN)
foreign_accounts[a.name] = a.id
line[destination] = a.id

Expand All @@ -72,8 +74,9 @@ def import_firefly(csv_path):
if line[destination] in personal_accounts:
line[destination] = personal_accounts[line[destination]]
else:
a = models.Account.objects.create(name=line[destination],
account_type=models.Account.AccountType.PERSONAL)
a = models.Account.objects.create(
name=line[destination],
account_type=models.Account.AccountType.PERSONAL)
personal_accounts[a.name] = a.id
line[destination] = a.id

Expand All @@ -82,8 +85,9 @@ def import_firefly(csv_path):
if line[destination] in foreign_accounts:
line[destination] = foreign_accounts[line[destination]]
else:
a = models.Account.objects.create(name=line[destination],
account_type=models.Account.AccountType.FOREIGN)
a = models.Account.objects.create(
name=line[destination],
account_type=models.Account.AccountType.FOREIGN)
foreign_accounts[a.name] = a.id
line[destination] = a.id
elif line[transaction_type] == 'Opening balance':
Expand Down
8 changes: 6 additions & 2 deletions silverstrike/management/commands/createtestdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def _initialize(self):
self.transactions = []
self.splits = []
self.counter = 100
self.work, _ = Account.objects.get_or_create(name='Work', account_type=Account.AccountType.FOREIGN)
self.work, _ = Account.objects.get_or_create(
name='Work',
account_type=Account.AccountType.FOREIGN)

self.checking, _ = Account.objects.get_or_create(name='Checking', show_on_dashboard=True)
self.savings, _ = Account.objects.get_or_create(name='Savings', show_on_dashboard=True)
Expand All @@ -56,7 +58,9 @@ def _initialize(self):
name='Supermarket', account_type=Account.AccountType.FOREIGN)
self.insurer, _ = Account.objects.get_or_create(
name='Insurnace', account_type=Account.AccountType.FOREIGN)
self.club, _ = Account.objects.get_or_create(name='Club', account_type=Account.AccountType.FOREIGN)
self.club, _ = Account.objects.get_or_create(
name='Club',
account_type=Account.AccountType.FOREIGN)

self.home, _ = Category.objects.get_or_create(name='Home')
self.groceries, _ = Category.objects.get_or_create(name='Groceries')
Expand Down
31 changes: 31 additions & 0 deletions silverstrike/migrations/0010_auto_20210107_1550.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 3.1.5 on 2021-01-07 15:50

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('silverstrike', '0009_auto_20190204_1925'),
]

operations = [
migrations.AddField(
model_name='importfile',
name='account',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='silverstrike.account'),
),
migrations.AddField(
model_name='importfile',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='importfile',
name='importer',
field=models.PositiveIntegerField(null=True),
),
]
7 changes: 6 additions & 1 deletion silverstrike/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def category(self, category):
return self.filter(category=category)

def transfers_once(self):
return self.exclude(opposing_account__account_type=Account.AccountType.PERSONAL, amount__gte=0)
return self.exclude(
opposing_account__account_type=Account.AccountType.PERSONAL,
amount__gte=0)

def exclude_transfers(self):
return self.exclude(account__account_type=Account.AccountType.PERSONAL,
Expand Down Expand Up @@ -292,6 +294,9 @@ class Budget(models.Model):
class ImportFile(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
file = models.FileField(upload_to='imports')
created_at = models.DateTimeField(auto_now_add=True)
account = models.ForeignKey(Account, models.SET_NULL, null=True)
importer = models.PositiveIntegerField(null=True)


class RecurringTransactionManager(models.Manager):
Expand Down
4 changes: 3 additions & 1 deletion silverstrike/tests/models/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
class AccountQuerysetTests(TestCase):
def setUp(self):
self.personal = Account.objects.create(name='Personal')
self.foreign = Account.objects.create(name='foreign', account_type=Account.AccountType.FOREIGN)
self.foreign = Account.objects.create(
name='foreign',
account_type=Account.AccountType.FOREIGN)

def test_personal_queryset(self):
queryset = Account.objects.personal()
Expand Down
4 changes: 3 additions & 1 deletion silverstrike/tests/models/test_recurrences.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
class RecurrenceTests(TestCase):
def setUp(self):
self.personal = Account.objects.create(name='personal')
self.foreign = Account.objects.create(name='foreign', account_type=Account.AccountType.FOREIGN)
self.foreign = Account.objects.create(
name='foreign',
account_type=Account.AccountType.FOREIGN)

self.date = date(2018, 1, 30)
self.recurrence = RecurringTransaction.objects.create(
Expand Down
8 changes: 6 additions & 2 deletions silverstrike/tests/models/test_splits.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class SplitQuerySetTests(TestCase):
def setUp(self):
self.personal = Account.objects.create(name='personal')
self.savings = Account.objects.create(name='savings')
self.foreign = Account.objects.create(name='foreign', account_type=Account.AccountType.FOREIGN)
self.foreign = Account.objects.create(
name='foreign',
account_type=Account.AccountType.FOREIGN)

self.transfer_transaction = create_transaction(
'transfer', self.personal, self.savings, 100, Transaction.TRANSFER, date(2017, 1, 1))
Expand Down Expand Up @@ -97,7 +99,9 @@ class SplitModelTests(TestCase):
def setUp(self):
self.personal = Account.objects.create(name='personal')
self.savings = Account.objects.create(name='savings')
self.foreign = Account.objects.create(name='foreign', account_type=Account.AccountType.FOREIGN)
self.foreign = Account.objects.create(
name='foreign',
account_type=Account.AccountType.FOREIGN)

def test_split_str_method(self):
transaction = create_transaction('meh', self.foreign, self.personal,
Expand Down
8 changes: 6 additions & 2 deletions silverstrike/tests/models/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
class TransactionQuerySetTests(TestCase):
def setUp(self):
self.personal = Account.objects.create(name='personal')
self.foreign = Account.objects.create(name='foreign', account_type=Account.AccountType.FOREIGN)
self.foreign = Account.objects.create(
name='foreign',
account_type=Account.AccountType.FOREIGN)

def test_last_10_returns_at_most_10(self):
for i in range(1, 32):
Expand All @@ -32,7 +34,9 @@ class TransactionModelTests(TestCase):
def setUp(self):
self.personal = Account.objects.create(name='personal')
self.savings = Account.objects.create(name='savings')
self.foreign = Account.objects.create(name='foreign', account_type=Account.AccountType.FOREIGN)
self.foreign = Account.objects.create(
name='foreign',
account_type=Account.AccountType.FOREIGN)

def test_transaction_str_method(self):
transaction = create_transaction('transaction', self.personal, self.foreign,
Expand Down
2 changes: 1 addition & 1 deletion silverstrike/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@

path('import/', import_views.ImportView.as_view(), name='import'),
path('import/upload/', import_views.ImportUploadView.as_view(), name='import_upload'),
path('import/process/<uuid:uuid>/<int:account>/<int:importer>/',
path('import/process/<uuid:uuid>/',
import_views.ImportProcessView.as_view(), name='import_process'),

path('import/firefly/', import_views.ImportFireflyView.as_view(), name='import_firefly'),
Expand Down
7 changes: 4 additions & 3 deletions silverstrike/views/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ def get_context_data(self, **kwargs):
next_month = self.current_month + relativedelta(months=1)
two_months_ago = self.current_month - relativedelta(months=2)
last_month = self.current_month - relativedelta(months=1)
splits = context['category'].splits.filter(account__account_type=Account.AccountType.PERSONAL,
date__gte=self.current_month,
date__lt=next_month)
splits = context['category'].splits.filter(
account__account_type=Account.AccountType.PERSONAL,
date__gte=self.current_month,
date__lt=next_month)
last_two_months_splits = context['category'].splits.filter(
account__account_type=Account.AccountType.PERSONAL,
date__gte=two_months_ago, date__lt=self.current_month)
Expand Down
20 changes: 9 additions & 11 deletions silverstrike/views/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ class ImportUploadView(LoginRequiredMixin, generic.edit.CreateView):

def form_valid(self, form):
self.object = form.save()
account = form.cleaned_data['account']
importer = form.cleaned_data['importer']
return HttpResponseRedirect(
reverse('import_process', args=[self.object.pk, account.pk, importer]))
reverse('import_process', args=[self.object.pk]))


class ImportProcessView(LoginRequiredMixin, generic.TemplateView):
Expand All @@ -51,7 +49,7 @@ class ImportProcessView(LoginRequiredMixin, generic.TemplateView):
def get_context_data(self, **kwargs):
context = super(ImportProcessView, self).get_context_data(**kwargs)
file = models.ImportFile.objects.get(uuid=self.kwargs['uuid'])
importer = self.kwargs['importer']
importer = file.importer
iban_accounts = dict()
names = dict()
for a in models.Account.objects.all():
Expand Down Expand Up @@ -102,7 +100,7 @@ def get_context_data(self, **kwargs):

def post(self, request, *args, **kwargs):
file = models.ImportFile.objects.get(uuid=self.kwargs['uuid'])
importer = self.kwargs['importer']
importer = file.importer
data = importers.IMPORTERS[importer].import_transactions(file.file.path)
for i in range(len(data)):
title = request.POST.get('title-{}'.format(i), '')
Expand All @@ -127,19 +125,19 @@ def post(self, request, *args, **kwargs):
if account.account_type == models.Account.AccountType.PERSONAL:
transaction.transaction_type = models.Transaction.TRANSFER
if amount < 0:
transaction.src_id = self.kwargs['account']
transaction.src_id = file.account_id
transaction.dst = account
else:
transaction.src = account
transaction.dst_id = self.kwargs['account']
transaction.dst_id = file.account_id
elif account.account_type == models.Account.AccountType.FOREIGN:
if amount < 0:
transaction.transaction_type = models.Transaction.WITHDRAW
transaction.src_id = self.kwargs['account']
transaction.src_id = file.account_id
transaction.dst = account
else:
transaction.transaction_type = models.Transaction.DEPOSIT
transaction.dst_id = self.kwargs['account']
transaction.dst_id = file.account_id
transaction.src = account
transaction.title = title
transaction.date = date
Expand All @@ -154,7 +152,7 @@ def post(self, request, *args, **kwargs):
amount=amount,
date=book_date,
transaction=transaction,
account_id=self.kwargs['account'],
account_id=file.account_id,
opposing_account=account
)
models.Split.objects.create(
Expand All @@ -163,7 +161,7 @@ def post(self, request, *args, **kwargs):
date=date,
transaction=transaction,
account=account,
opposing_account_id=self.kwargs['account']
opposing_account_id=file.account_id
)
return HttpResponseRedirect('/')

Expand Down
7 changes: 3 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
[tox]

envlist = django2.0, flake, coverage
envlist = django3.1, flake, coverage

[testenv]
usedevelop = True
basepython = python3
deps =
django2.0: Django>=2.0,<2.1
django2.1: Django>=2.1,<2.2
django2.2: Django>=2.2,<2.3
django3.0: Django>=3.0,<3.1
django3.1: Django>=3.1,<3.2
djangolatest: https://github.com/django/django/archive/master.tar.gz
ofxparse
commands =
Expand Down

0 comments on commit 78b4dad

Please sign in to comment.