-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
117 lines (96 loc) · 4.22 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pytz
import util
from google.appengine.api import datastore
from google.appengine.api import memcache
from google.appengine.ext import ndb
class SavingsAccount(ndb.Model):
parent_user = ndb.UserProperty()
child_first_name = ndb.StringProperty()
child_image = ndb.BlobProperty()
child_email = ndb.StringProperty()
open_datetime = ndb.DateTimeProperty(auto_now_add=True)
currency = ndb.StringProperty()
interest_rate = ndb.FloatProperty()
interest_compound_frequency = ndb.StringProperty(
choices=set(['weekly', 'monthly']))
opening_balance = ndb.IntegerProperty()
allowance_amount = ndb.IntegerProperty()
allowance_frequency = ndb.StringProperty(
choices=set(['weekly', 'monthly']))
# will determine the day of the allowance dispersal
allowance_start_date = ndb.DateProperty()
timezone_name = ndb.StringProperty()
def getOpeningBalanceForPrinting(self):
return util.formatMoney(self.opening_balance)
def getFormattedOpenDate(self):
return util.formatShortDate(self.getOpenDatetime())
def getOpenDate(self):
return self.getOpenDatetime().date()
def getOpenDatetime(self):
open_datetime = self.open_datetime.replace(tzinfo=pytz.utc).astimezone(pytz.timezone(self.timezone_name))
return open_datetime
def getAllowanceStartDate(self):
return util.formatShortDate(self.allowance_start_date)
# TODO(jgessner): move the memcache stuff to AccountList.
def getBalance(self):
cache_key = '%s_balance' % self.key
balance = memcache.get(cache_key)
if balance is not None:
return balance
balance = self.calculateBalance()
memcache.set(cache_key, balance, 30)
return balance
def calculateBalance(self, max_time=None):
transactions = AccountTransaction.getTransactionsForAccount(self, max_time)
balance = self.opening_balance
for transaction in transactions:
if transaction.transaction_type == 'withdrawal':
balance -= transaction.amount
else:
balance += transaction.amount
return balance
def getBalanceForPrinting(self):
return '%.2f' % (self.getBalance() / 1000000.0)
# TODO(jgessner): move the ForPrinting methods somewhere else.
def getAllowanceAmountForPrinting(self):
return util.formatMoney(self.allowance_amount)
class AccountTransaction(ndb.Model):
savings_account = ndb.KeyProperty(kind=SavingsAccount)
transaction_type = ndb.StringProperty(choices=set(
['interest', 'allowance', 'deposit', 'withdrawal']))
transaction_time = ndb.DateTimeProperty(auto_now_add=True)
transaction_local_date = ndb.DateProperty()
amount = ndb.IntegerProperty()
memo = ndb.StringProperty(default='')
def getAmountForPrinting(self):
return util.formatMoney(self.amount)
def FormattedDate(self):
return getYMDInCentralTime(self.transaction_time)
@staticmethod
def getTransactionsForAccount(account, max_time=None):
transactions_query = AccountTransaction.query(ancestor=account.key)
if max_time:
transactions_query = transactions_query.filter(AccountTransaction.transaction_time <= max_time)
transactions_query = transactions_query.order(AccountTransaction.transaction_time)
# TODO(jgessner): Increase this limit.
return transactions_query.fetch(1000)
@staticmethod
def hasAllowanceForDate(account, transaction_date):
return AccountTransaction.hasTransactionOnDateOfType(account, transaction_date, 'allowance')
@staticmethod
def hasInterestForDate(account, transaction_date):
return AccountTransaction.hasTransactionOnDateOfType(account, transaction_date, 'interest')
@staticmethod
def hasTransactionOnDateOfType(account, transaction_date, transaction_type):
query = AccountTransaction.query()
query = query.filter(AccountTransaction.savings_account == account.key)
query = query.filter(AccountTransaction.transaction_local_date == transaction_date)
query = query.filter(AccountTransaction.transaction_type == transaction_type)
return len(query.fetch(1)) > 0
class SaverParent(ndb.Model):
email = ndb.StringProperty()
name = ndb.StringProperty()
nickname = ndb.StringProperty()
date_added = ndb.DateTimeProperty(auto_now_add=True)
# Invitation Key will be cleared out when an invitation is accepted.
invitation_key = ndb.StringProperty()