summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--talerbank/app/management/commands/basic_accounts.py32
-rw-r--r--talerbank/app/migrations/0001_initial.py1
-rw-r--r--talerbank/app/models.py8
-rw-r--r--talerbank/app/startup.py59
-rw-r--r--talerbank/app/views.py4
5 files changed, 103 insertions, 1 deletions
diff --git a/talerbank/app/management/commands/basic_accounts.py b/talerbank/app/management/commands/basic_accounts.py
new file mode 100644
index 0000000..0d06aab
--- /dev/null
+++ b/talerbank/app/management/commands/basic_accounts.py
@@ -0,0 +1,32 @@
+"""
+ This file is part of TALER
+ (C) 2014, 2015, 2106 INRIA
+
+ TALER is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
+
+ @author Marcello Stanisci
+"""
+from django.core.management.base import BaseCommand
+from django.contrib.auth.models import User
+from django.db.utils import OperationalError
+from ...startup import basic_accounts
+
+
+"""
+Create bank and exchange accounts, which are
+necessary for the bank to work correctly.
+"""
+
+
+class Command(BaseCommand):
+ def handle(self, *args, **options):
+ basic_accounts()
diff --git a/talerbank/app/migrations/0001_initial.py b/talerbank/app/migrations/0001_initial.py
index 6557c10..703e17d 100644
--- a/talerbank/app/migrations/0001_initial.py
+++ b/talerbank/app/migrations/0001_initial.py
@@ -6,7 +6,6 @@ from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
-
class Migration(migrations.Migration):
initial = True
diff --git a/talerbank/app/models.py b/talerbank/app/models.py
index b2bc854..45bffdb 100644
--- a/talerbank/app/models.py
+++ b/talerbank/app/models.py
@@ -42,3 +42,11 @@ class History(models.Model):
date = models.DateField(auto_now=True)
account = models.ForeignKey(BankAccount, on_delete=models.CASCADE)
+ def set_balance(self, new_balance):
+ if isinstance(new_balance, int) or isinstance(new_balance, float):
+ self.balance = new_balance
+ else:
+ print("Not a number given for new balance")
+
+ def push_history(self, obj):
+ self.history.append(obj)
diff --git a/talerbank/app/startup.py b/talerbank/app/startup.py
new file mode 100644
index 0000000..3583b12
--- /dev/null
+++ b/talerbank/app/startup.py
@@ -0,0 +1,59 @@
+"""
+ This file is part of TALER
+ (C) 2014, 2015, 2016 INRIA
+
+ TALER is free software; you can redistribute it and/or modify it under the
+ terms of the GNU Affero General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
+
+ @author Marcello Stanisci
+ @file Container for routines to be run at startup
+"""
+
+from .models import BankAccount
+from django.contrib.auth.models import User
+from django.db.utils import OperationalError
+from .lib import get_currency
+import sys
+import logging
+
+# Creates basic accounts (bank's and exchange's) if the user didn't manually
+
+def basic_accounts():
+ accepted_options = ['--definetables', '--preaccounts', '--bareserver', '--sampledata']
+ try:
+ if sys.argv[1] not in accepted_options:
+ logging.warn("skipping basic_accounts() check")
+ return
+ except IndexError:
+ print("Got IndexError, unmanaged..")
+ return
+ try:
+ bank = User.objects.get(username='Bank')
+ print("Basic accounts found\n")
+ except OperationalError:
+ print("Please create DB before running the bank")
+ sys.exit()
+ # bank not existent, so neither other accounts are
+ except User.DoesNotExist:
+ print("Basic accounts not found..\n")
+ bank = User.objects.create_user(username='Bank', password='')
+ exchange = User.objects.create_user(username='Exchange', password='')
+ bank_ba = BankAccount(user=bank,
+ currency=get_currency(None),
+ is_public=True)
+ bank_ba.save()
+ print("Creating '" + bank_ba.user.username + "' account, with number " + str(bank_ba.account_no) + "\n")
+ exchange_ba = BankAccount(user=exchange,
+ currency=get_currency(None),
+ is_public=True)
+ exchange_ba.save()
+ print("Creating '" + exchange_ba.user.username + "' account, with number " + str(exchange_ba.account_no) + "\n")
+
diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index ca255e1..b45b4af 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -31,8 +31,12 @@ from .util import (home_page,
profile_process)
from .test import check_db_test_process
from django.http import HttpResponse
+from .startup import basic_accounts
def ignore(request):
return HttpResponse()
+
+# create basic accounts if they don't exist
+basic_accounts()