Skip to content

Commit f5a5943

Browse files
committed
Add fields to ImportFile
1 parent 7c69fc7 commit f5a5943

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

silverstrike/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class ImportUploadForm(forms.ModelForm):
99
class Meta:
1010
model = models.ImportFile
11-
fields = ['file']
11+
fields = ['file', 'account', 'importer']
1212
account = forms.ModelChoiceField(queryset=models.Account.objects.personal().active())
1313
importer = forms.ChoiceField(choices=enumerate(importers.IMPORTER_NAMES))
1414

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 3.1.5 on 2021-01-07 15:50
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
import django.utils.timezone
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('silverstrike', '0009_auto_20190204_1925'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='importfile',
17+
name='account',
18+
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='silverstrike.account'),
19+
),
20+
migrations.AddField(
21+
model_name='importfile',
22+
name='created_at',
23+
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
24+
preserve_default=False,
25+
),
26+
migrations.AddField(
27+
model_name='importfile',
28+
name='importer',
29+
field=models.PositiveIntegerField(null=True),
30+
),
31+
]

silverstrike/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ class Budget(models.Model):
292292
class ImportFile(models.Model):
293293
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
294294
file = models.FileField(upload_to='imports')
295+
created_at = models.DateTimeField(auto_now_add=True)
296+
account = models.ForeignKey(Account, models.SET_NULL, null=True)
297+
importer = models.PositiveIntegerField(null=True)
295298

296299

297300
class RecurringTransactionManager(models.Manager):

silverstrike/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130

131131
path('import/', import_views.ImportView.as_view(), name='import'),
132132
path('import/upload/', import_views.ImportUploadView.as_view(), name='import_upload'),
133-
path('import/process/<uuid:uuid>/<int:account>/<int:importer>/',
133+
path('import/process/<uuid:uuid>/',
134134
import_views.ImportProcessView.as_view(), name='import_process'),
135135

136136
path('import/firefly/', import_views.ImportFireflyView.as_view(), name='import_firefly'),

silverstrike/views/imports.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ class ImportUploadView(LoginRequiredMixin, generic.edit.CreateView):
3939

4040
def form_valid(self, form):
4141
self.object = form.save()
42-
account = form.cleaned_data['account']
43-
importer = form.cleaned_data['importer']
4442
return HttpResponseRedirect(
45-
reverse('import_process', args=[self.object.pk, account.pk, importer]))
43+
reverse('import_process', args=[self.object.pk]))
4644

4745

4846
class ImportProcessView(LoginRequiredMixin, generic.TemplateView):
@@ -51,7 +49,7 @@ class ImportProcessView(LoginRequiredMixin, generic.TemplateView):
5149
def get_context_data(self, **kwargs):
5250
context = super(ImportProcessView, self).get_context_data(**kwargs)
5351
file = models.ImportFile.objects.get(uuid=self.kwargs['uuid'])
54-
importer = self.kwargs['importer']
52+
importer = file.importer
5553
iban_accounts = dict()
5654
names = dict()
5755
for a in models.Account.objects.all():
@@ -102,7 +100,7 @@ def get_context_data(self, **kwargs):
102100

103101
def post(self, request, *args, **kwargs):
104102
file = models.ImportFile.objects.get(uuid=self.kwargs['uuid'])
105-
importer = self.kwargs['importer']
103+
importer = file.importer
106104
data = importers.IMPORTERS[importer].import_transactions(file.file.path)
107105
for i in range(len(data)):
108106
title = request.POST.get('title-{}'.format(i), '')
@@ -127,19 +125,19 @@ def post(self, request, *args, **kwargs):
127125
if account.account_type == models.Account.AccountType.PERSONAL:
128126
transaction.transaction_type = models.Transaction.TRANSFER
129127
if amount < 0:
130-
transaction.src_id = self.kwargs['account']
128+
transaction.src_id = file.account_id
131129
transaction.dst = account
132130
else:
133131
transaction.src = account
134-
transaction.dst_id = self.kwargs['account']
132+
transaction.dst_id = file.account_id
135133
elif account.account_type == models.Account.AccountType.FOREIGN:
136134
if amount < 0:
137135
transaction.transaction_type = models.Transaction.WITHDRAW
138-
transaction.src_id = self.kwargs['account']
136+
transaction.src_id = file.account_id
139137
transaction.dst = account
140138
else:
141139
transaction.transaction_type = models.Transaction.DEPOSIT
142-
transaction.dst_id = self.kwargs['account']
140+
transaction.dst_id = file.account_id
143141
transaction.src = account
144142
transaction.title = title
145143
transaction.date = date
@@ -154,7 +152,7 @@ def post(self, request, *args, **kwargs):
154152
amount=amount,
155153
date=book_date,
156154
transaction=transaction,
157-
account_id=self.kwargs['account'],
155+
account_id=file.account_id,
158156
opposing_account=account
159157
)
160158
models.Split.objects.create(
@@ -163,7 +161,7 @@ def post(self, request, *args, **kwargs):
163161
date=date,
164162
transaction=transaction,
165163
account=account,
166-
opposing_account_id=self.kwargs['account']
164+
opposing_account_id=file.account_id
167165
)
168166
return HttpResponseRedirect('/')
169167

0 commit comments

Comments
 (0)