Skip to content

Commit

Permalink
Use django choices for AccountType
Browse files Browse the repository at this point in the history
  • Loading branch information
simhnna committed Jan 7, 2021
1 parent 632d185 commit 1708070
Show file tree
Hide file tree
Showing 31 changed files with 109 additions and 110 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ __pycache__/
# C extensions
*.so

#graphql
schema.json

# Distribution / packaging
.Python
env/
Expand Down
2 changes: 1 addition & 1 deletion silverstrike/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def merge_accounts(self, request, queryset):
return
for account in accounts:
failure = False
if account.account_type != models.Account.FOREIGN:
if account.account_type != models.Account.AccountType.FOREIGN:
self.message_user(request, _(
'You can only merge foreign accounts, "{}" isn\'t.'.format(account.name)))
failure = True
Expand Down
2 changes: 1 addition & 1 deletion silverstrike/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@login_required
def get_accounts(request, account_type):
accounts = Account.objects.exclude(account_type=Account.SYSTEM)
accounts = Account.objects.exclude(account_type=Account.AccountType.SYSTEM)
if account_type != 'all':
account_type = getattr(Account, account_type)
accounts = accounts.filter(account_type=account_type)
Expand Down
24 changes: 12 additions & 12 deletions silverstrike/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Meta:

def clean_name(self):
name = self.cleaned_data['name']
if models.Account.objects.filter(name=name, account_type=models.Account.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 +34,7 @@ class Meta:

def clean_name(self):
name = self.cleaned_data['name']
if models.Account.objects.filter(name=name, account_type=models.Account.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 @@ -85,9 +85,9 @@ class Meta:
value_date = forms.DateField(required=False)

src = forms.ModelChoiceField(queryset=models.Account.objects.filter(
account_type=models.Account.PERSONAL, active=True))
account_type=models.Account.AccountType.PERSONAL, active=True))
dst = forms.ModelChoiceField(queryset=models.Account.objects.filter(
account_type=models.Account.PERSONAL, active=True))
account_type=models.Account.AccountType.PERSONAL, active=True))

def save(self, commit=True):
transaction = super().save(commit)
Expand Down Expand Up @@ -146,7 +146,7 @@ class WithdrawForm(TransactionForm):
def clean_dst(self):
account, _ = models.Account.objects.get_or_create(
name=self.cleaned_data['dst'],
account_type=models.Account.FOREIGN)
account_type=models.Account.AccountType.FOREIGN)
return account

def clean(self):
Expand All @@ -160,7 +160,7 @@ class DepositForm(TransactionForm):

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

def clean(self):
Expand All @@ -184,12 +184,12 @@ def clean(self):
super(RecurringTransactionForm, self).clean()
src = self.cleaned_data['src']
dst = self.cleaned_data['dst']
if src.account_type == models.Account.PERSONAL:
if dst.account_type == models.Account.PERSONAL:
if src.account_type == models.Account.AccountType.PERSONAL:
if dst.account_type == models.Account.AccountType.PERSONAL:
self.transaction_type = models.Transaction.TRANSFER
else:
self.transaction_type = models.Transaction.WITHDRAW
elif dst.account_type == models.Account.PERSONAL:
elif dst.account_type == models.Account.AccountType.PERSONAL:
self.transaction_type = models.Transaction.DEPOSIT
else:
raise forms.ValidationError(
Expand Down Expand Up @@ -217,7 +217,7 @@ def __init__(self, *args, **kwargs):
def save(self, commit=True):
transaction = super().save(False)
transaction.transaction_type = models.Transaction.SYSTEM
transaction.src = models.Account.objects.get(account_type=models.Account.SYSTEM)
transaction.src = models.Account.objects.get(account_type=models.Account.AccountType.SYSTEM)
transaction.dst = models.Account.objects.get(pk=self.account)

balance = self.cleaned_data['balance']
Expand Down Expand Up @@ -245,9 +245,9 @@ class Meta:
model = models.Split
fields = ['title', 'account', 'opposing_account', 'date', 'amount', 'category']
account = forms.ModelChoiceField(queryset=models.Account.objects.exclude(
account_type=models.Account.SYSTEM))
account_type=models.Account.AccountType.SYSTEM))
opposing_account = forms.ModelChoiceField(queryset=models.Account.objects.exclude(
account_type=models.Account.SYSTEM))
account_type=models.Account.AccountType.SYSTEM))


TransactionFormSet = forms.models.inlineformset_factory(
Expand Down
14 changes: 7 additions & 7 deletions silverstrike/importers/firefly.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def import_firefly(csv_path):
notes = 'notes'
transaction_type = 'transaction_type'

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

personal_accounts = dict()
foreign_accounts = dict()
for name, id, t in models.Account.objects.all().values_list('name', 'id', 'account_type'):
if t == models.Account.PERSONAL:
if t == models.Account.AccountType.PERSONAL:
personal_accounts[name] = id
elif t == models.Account.FOREIGN:
elif t == models.Account.AccountType.FOREIGN:
foreign_accounts[name] = id

categories = dict()
Expand All @@ -48,7 +48,7 @@ def import_firefly(csv_path):
line[source] = personal_accounts[line[source]]
else:
a = models.Account.objects.create(name=line[source],
account_type=models.Account.PERSONAL)
account_type=models.Account.AccountType.PERSONAL)
personal_accounts[a.name] = a.id
line[source] = a.id

Expand All @@ -60,7 +60,7 @@ def import_firefly(csv_path):
line[destination] = foreign_accounts[line[destination]]
else:
a = models.Account.objects.create(name=line[destination],
account_type=models.Account.FOREIGN)
account_type=models.Account.AccountType.FOREIGN)
foreign_accounts[a.name] = a.id
line[destination] = a.id

Expand All @@ -73,7 +73,7 @@ def import_firefly(csv_path):
line[destination] = personal_accounts[line[destination]]
else:
a = models.Account.objects.create(name=line[destination],
account_type=models.Account.PERSONAL)
account_type=models.Account.AccountType.PERSONAL)
personal_accounts[a.name] = a.id
line[destination] = a.id

Expand All @@ -83,7 +83,7 @@ def import_firefly(csv_path):
line[destination] = foreign_accounts[line[destination]]
else:
a = models.Account.objects.create(name=line[destination],
account_type=models.Account.FOREIGN)
account_type=models.Account.AccountType.FOREIGN)
foreign_accounts[a.name] = a.id
line[destination] = a.id
elif line[transaction_type] == 'Opening balance':
Expand Down
12 changes: 6 additions & 6 deletions silverstrike/management/commands/createtestdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def handle(self, *args, **options):
Split.objects.bulk_create(self.splits)

def _prune(self):
Account.objects.exclude(account_type=Account.SYSTEM).delete()
Account.objects.exclude(account_type=Account.AccountType.SYSTEM).delete()
Category.objects.all().delete()
RecurringTransaction.objects.all().delete()
Transaction.objects.all().delete()
Expand All @@ -45,18 +45,18 @@ def _initialize(self):
self.transactions = []
self.splits = []
self.counter = 100
self.work, _ = Account.objects.get_or_create(name='Work', account_type=Account.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)

self.landlord, _ = Account.objects.get_or_create(
name='Landlord', account_type=Account.FOREIGN)
name='Landlord', account_type=Account.AccountType.FOREIGN)
self.supermarket, _ = Account.objects.get_or_create(
name='Supermarket', account_type=Account.FOREIGN)
name='Supermarket', account_type=Account.AccountType.FOREIGN)
self.insurer, _ = Account.objects.get_or_create(
name='Insurnace', account_type=Account.FOREIGN)
self.club, _ = Account.objects.get_or_create(name='Club', account_type=Account.FOREIGN)
name='Insurnace', 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
38 changes: 17 additions & 21 deletions silverstrike/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

class AccountQuerySet(models.QuerySet):
def personal(self):
return self.filter(account_type=Account.PERSONAL)
return self.filter(account_type=Account.AccountType.PERSONAL)

def foreign(self):
return self.filter(account_type=Account.FOREIGN)
return self.filter(account_type=Account.AccountType.FOREIGN)

def active(self):
return self.filter(active=True)
Expand All @@ -26,17 +26,13 @@ def shown_on_dashboard(self):


class Account(models.Model):
PERSONAL = 1
FOREIGN = 2
SYSTEM = 3
ACCOUNT_TYPES = (
(PERSONAL, _('Personal')),
(FOREIGN, _('Foreign')),
(SYSTEM, _('System')),
)
class AccountType(models.IntegerChoices):
PERSONAL = 1, _('Personal')
FOREIGN = 2, _('Foreign')
SYSTEM = 3, _('System')

name = models.CharField(max_length=64)
account_type = models.IntegerField(choices=ACCOUNT_TYPES, default=PERSONAL)
account_type = models.IntegerField(choices=AccountType.choices, default=AccountType.PERSONAL)
active = models.BooleanField(default=True)
last_modified = models.DateTimeField(auto_now=True)
show_on_dashboard = models.BooleanField(default=False)
Expand All @@ -55,11 +51,11 @@ def __str__(self):

@property
def account_type_str(self):
return Account.ACCOUNT_TYPES[self.account_type - 1][1]
return Account.AccountType.labels[self.account_type - 1]

@property
def is_personal(self):
return self.account_type == Account.PERSONAL
return self.account_type == Account.AccountType.PERSONAL

@property
def transaction_num(self):
Expand Down Expand Up @@ -102,7 +98,7 @@ def get_data_points(self, dstart=date.today() - timedelta(days=365),
return data_points

def set_initial_balance(self, amount):
system = Account.objects.get(account_type=Account.SYSTEM)
system = Account.objects.get(account_type=Account.AccountType.SYSTEM)
transaction = Transaction.objects.create(title=_('Initial Balance'),
transaction_type=Transaction.SYSTEM,
src=system,
Expand Down Expand Up @@ -184,13 +180,13 @@ def is_deposit(self):

class SplitQuerySet(models.QuerySet):
def personal(self):
return self.filter(account__account_type=Account.PERSONAL)
return self.filter(account__account_type=Account.AccountType.PERSONAL)

def income(self):
return self.filter(opposing_account__account_type=Account.FOREIGN, amount__gt=0)
return self.filter(opposing_account__account_type=Account.AccountType.FOREIGN, amount__gt=0)

def expense(self):
return self.filter(opposing_account__account_type=Account.FOREIGN, amount__lt=0)
return self.filter(opposing_account__account_type=Account.AccountType.FOREIGN, amount__lt=0)

def date_range(self, dstart, dend):
return self.filter(date__gte=dstart, date__lte=dend)
Expand All @@ -199,11 +195,11 @@ def category(self, category):
return self.filter(category=category)

def transfers_once(self):
return self.exclude(opposing_account__account_type=Account.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.PERSONAL,
opposing_account__account_type=Account.PERSONAL)
return self.exclude(account__account_type=Account.AccountType.PERSONAL,
opposing_account__account_type=Account.AccountType.PERSONAL)

def upcoming(self):
return self.filter(date__gt=date.today())
Expand Down Expand Up @@ -271,7 +267,7 @@ def __str__(self):
@property
def money_spent(self):
return abs(Split.objects.filter(
category=self, account__account_type=Account.PERSONAL,
category=self, account__account_type=Account.AccountType.PERSONAL,
transaction__transaction_type=Transaction.WITHDRAW).aggregate(
models.Sum('amount'))['amount__sum'] or 0)

Expand Down
2 changes: 1 addition & 1 deletion silverstrike/rest/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class ProtectSystemAccount(permissions.BasePermission):
def has_object_permission(self, request, view, object):
if request.method in permissions.SAFE_METHODS:
return True
return object.account_type != Account.SYSTEM
return object.account_type != Account.AccountType.SYSTEM
2 changes: 1 addition & 1 deletion silverstrike/rest/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Meta:
read_only_fields = ('last_modified',)

def validate_account_type(self, value):
if value == Account.SYSTEM:
if value == Account.AccountType.SYSTEM:
raise serializers.ValidationError("You can't create system accounts")
return value

Expand Down
4 changes: 2 additions & 2 deletions silverstrike/rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def get(self, request, format=None):
class PersonalAccountsView(views.APIView):
def get(self, request, format=None):
serializer = serializers.AccountSerializer(
Account.objects.filter(account_type=Account.PERSONAL), many=True)
Account.objects.filter(account_type=Account.AccountType.PERSONAL), many=True)
return Response(serializer.data)


class ForeignAccountsView(views.APIView):
def get(self, request, format=None):
serializer = serializers.AccountSerializer(
Account.objects.filter(account_type=Account.FOREIGN), many=True)
Account.objects.filter(account_type=Account.AccountType.FOREIGN), many=True)
return Response(serializer.data)
2 changes: 1 addition & 1 deletion silverstrike/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ def create_transaction(title, src, dst, amount, type, date=date.today(), categor
return t


def create_account(name, account_type=Account.PERSONAL):
def create_account(name, account_type=Account.AccountType.PERSONAL):
return Account.objects.create(name=name, account_type=account_type)
4 changes: 2 additions & 2 deletions silverstrike/tests/forms/test_account_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_unique_name(self):
self.assertIn('name', form.errors)

def test_unique_name_foregin_accounts(self):
models.Account.objects.create(name='foo', account_type=models.Account.FOREIGN)
models.Account.objects.create(name='foo', account_type=models.Account.AccountType.FOREIGN)
form = forms.ForeignAccountForm({
'name': 'foo'
})
Expand All @@ -39,7 +39,7 @@ def test_unique_name_foregin_accounts(self):
self.assertIn('name', form.errors)

def test_unique_allows_different_types(self):
models.Account.objects.create(name='foo', account_type=models.Account.FOREIGN)
models.Account.objects.create(name='foo', account_type=models.Account.AccountType.FOREIGN)
models.Account.objects.create(name='bar')
form = forms.AccountCreateForm({
'name': 'foo', 'initial_balance': 100, 'active': True
Expand Down
8 changes: 4 additions & 4 deletions silverstrike/tests/forms/test_recurringtransaction_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_transfer(self):

def test_withdraw(self):
personal = Account.objects.create(name='foo')
other = Account.objects.create(name='bar', account_type=Account.FOREIGN)
other = Account.objects.create(name='bar', account_type=Account.AccountType.FOREIGN)
form = RecurringTransactionForm({
'amount': 100, 'date': '2100-01-01', 'multiplier': 1,
'src': personal.id, 'dst': other.id, 'interval': RecurringTransaction.MONTHLY,
Expand All @@ -60,7 +60,7 @@ def test_withdraw(self):

def test_deposit(self):
personal = Account.objects.create(name='foo')
other = Account.objects.create(name='bar', account_type=Account.FOREIGN)
other = Account.objects.create(name='bar', account_type=Account.AccountType.FOREIGN)
form = RecurringTransactionForm({
'amount': 100, 'date': '2100-01-01', 'multiplier': 1,
'src': other.id, 'dst': personal.id, 'interval': RecurringTransaction.MONTHLY,
Expand All @@ -71,8 +71,8 @@ def test_deposit(self):
self.assertEqual(transaction.transaction_type, Transaction.DEPOSIT)

def test_two_foreign_accounts(self):
first = Account.objects.create(name='foo', account_type=Account.FOREIGN)
other = Account.objects.create(name='bar', account_type=Account.FOREIGN)
first = Account.objects.create(name='foo', account_type=Account.AccountType.FOREIGN)
other = Account.objects.create(name='bar', account_type=Account.AccountType.FOREIGN)
form = RecurringTransactionForm({
'amount': 100, 'date': '2100-01-01', 'multiplier': 1,
'src': other.id, 'dst': first.id, 'interval': RecurringTransaction.MONTHLY,
Expand Down
Loading

0 comments on commit 1708070

Please sign in to comment.